How do I change a property in multiple objects at once?

MAXScript FAQ > Accessing Object Properties > How do I change a property in multiple objects at once?

One of the most often used applications of MAXScript in the daily work is the mass-changing of object properties. This can save tremendous amounts of clicks and time.

To change a property in multiple objects, you obviously need to:

  1. Get the correct objects to change,

  2. Assign a value to the correct property.

The simplest situation imaginable would be a selection of similar objects, for example, a large number of sphere primitives. MAXScript lets you easily access the current selection in the scene. In addition, an object property assignment is "mappable". This means that MAXScript will apply the value on the right side of the assignment operator '=' to every single object in the selection automatically without the need of an explicit MAXScript for loop. (See What Are Mapped Operations in MAXScript? for more details on mapped operations).

FOR EXAMPLE

 -- select a large number of spheres in the scene with the mouse, -- then change the radius of all selected objects to 10.0 units -- by typing in the Listener:
selection.radius = 10
OK

In case you have selected some object which does not have a property called radius, like a Box primitive or some EditableMesh object, the above code will cause an error:

ERROR EXAMPLE

selection.radius = 10
-- Unknown property: "radius" in $Box:Box01 @ [0. 000000, 0. 000000,0.000000]

In such case you would have to make sure the collection of objects is filtered by your script before an assignment is made. See the For Loop topic for more info. A possible way would be

EXAMPLE

 -- select any number of objects in the scene with the mouse, -- then change the radius of all objects that have a .radius property -- by typing in the Listener:
for i inselection where hasProperty i "radius" do i.radius = 10
OK

If you want to change the radius of any object in the scene that has such property instead of selecting objects manually, you can let MAXScript filter the scene for you. This will affect all Spheres, GeoSpheres, Cylinders etc. and ignore any Boxes etc. that do not have a "radius" property.

EXAMPLE

 -- Go through all geometry objects, filter those that have a .radius property -- and change the radius to 10 units by typing in the Listener:
for i inGeometry where hasProperty i "radius" do i.radius = 10
OK

If you want to affect only the GeoSpheres in the scene and exclude all other classes, you could check the class of the objects instead. In this case, you don't have to check for a property named "radius" since any GeoSphere has this property! The classOf function returns the object's class so you can compare it to the class you want to affect.

EXAMPLE

 -- Go through all geometry objects, filter the GeoSpheres -- and change their radius to 10 units by typing in the Listener:
for i in Geometry where classOf i == GeoSphere do i.radius = 10
OK

If you have no idea what the correct MAXScript name of a property is, you can either consult this Help File or use the showProperties inspector function in the Listener to print a list of all exposed object properties.

EXAMPLE

 -- Select a single sphere in the scene using the mouse and -- type in the Listener:
showProperties $
.smooth : boolean
.radius : float
.mapCoords : boolean
.segs : integer
.slice : boolean
.hemisphere : float
.sliceFrom : angle
.sliceTo : angle
.chop : integer
.recenter : boolean
false

See Also