Automatically assign a material when creating an object

posted in: Blog | 0

Ever wanted to have a material assigned by default to every object you create!? Fear not, the solution is here 🙂 It’s nothing fancy but shows up how you can easily use NodeEventCallback to perform actions on nodes using this callback system. The code is pretty simple and self-explanatory so I won’t go into much detail. You can read more about it here on the Maxscript help.

We’re using the added callback, which happens everytime a new object is created in the scene, we pass over the function we want to use in this callback with two parameters, ev and nd, first one is the event calling the function, the second one is a list of AnimHandles.

We can use GetAnimByHandle nodeHandle to get back the node object for that handle, after this, we’re ready to go! In this case I’m just double checking if the node we’re creating is geometry, otherwise do nothing!

For viewing purposes I’m using a completeRedraw() which I recommend not using in heavy scenes, or just don’t use at all. The only difference if you take it out is that redraw only occurs after you do something in the viewports, but the material assignment is done, no worries about that 🙂

I’m using two persistent variables that get saved with the scene, so we avoid having multiple “default” standard materials every time we create an object, load/reload scenes.

Long story short, here’s the code, you can place it in the startup scripts folder or just assign it to a macro. Enjoy!

	if acMat == undefined then (
		persistent global acColor = color 30 30 30
		persistent global acMat = StandardMaterial()
		acMat.diffuse = acColor

	fn setStandardMaterial ev nd = (	
		local acObj = GetAnimByHandle nd[1]
		if superclassof(acObj) == GeometryClass then (
			acObj.material = acMat
			acObj.wireColor = acColor

	callbackItem = NodeEventCallback mouseUp:true added:setStandardMaterial


If you want to remove the callback, doing callbackItem = undefined in the Macroscript editor should be enough, same goes for acMat and acColor.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.