Sunday, 26 April 2015

Change an attribute on multiple objetcs

That's something pretty common, you sometime want to change something on multiple objects, but 3dsMax doesn't support this through the interface. Generally you end up changing the parameter on each object... when you could write a single line of script that does it for you !

There's a very common keyword, in most programming languages, that goes through a collection of things (like the list of the selected objects): for

Now for the basic example. Let's say you want to change the radius of multiple spheres ! In MAXScript it would look like that:
 for i in selection do i.radius = 5  
Just open the MAXScript Listener and paste this in. Once you press enter your selected spheres should have a radius of 5, and Max should gratify with a satisfying blue OK. Max only gets more verbose if things don't work as planned.

How to know the name of the parameter I hear? Well there are a few ways.

You can enable the MacroRecorder in the Listener, which is supposed to show you in real time the actions that you make. For example if I change the number of segments on a sphere, the Recorder will display
 $.segs = 10  
From there you just replace radius by segs and you're set! As you can see the parameter name in MAXScript isn't always the same as the one displayed in the interface, so you can't always just guess.

Another way is to ask Max to show you the list of the attributes of the selected object with the function show:
 show $  
  .smooth : boolean  
  .radius : float  
  .segs : integer  
  .mapcoords : boolean  
  .slice : boolean  
  .hemisphere : float  
  .sliceFrom : angle  
  .sliceTo : angle  
  .chop : integer  
  .recenter : boolean  
  .realWorldMapSize : boolean  
That can come in handy! The $ sign stands for the current selection.

Of course you'll also find all the properties of objects in Max Help, with some info about them if you're lucky!

Note that for a lot of basic attributes like radius ou segs, you can directly type:
 $.segs = 10  
That's called a Mapped Function and can be faster than a for loop, but doesn't always work. For example, if you try to assign an animation controller on several objects, you'll have to resort to using a loop.

That's it for today, it's fairly simple but can save your life from time to time !

No comments:

Post a Comment