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 is the mass-changing of object properties. This can save tremendous amounts of clicks and time.

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

  1. Get the correct objects to change

  2. Assign a value to the correct property

The simplest situation imaginable is 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 applies 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 that does not have a property called radius such as, a Box primitive or some EditableMesh object, the above code causes an error:

ERROR EXAMPLE

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

In such a case, you need to make sure that the collection of objects is filtered by your script before an assignment is made. See the For Loop topic for more information. A possible way is:

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 a property instead of selecting objects manually, you can let MAXScript filter the scene for you. This affects all Spheres, GeoSpheres, Cylinders, and so on, but ignores any Boxes 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 can check the class of the objects instead. In this case, you do not have to check for a property named "radius" because 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