Variable Assignment

The syntax for general assignment, <assignment>, is:

<destination> = <expr>

where <destination> can be one of:

<var_name>

<property>

<array_index>

Each of these destination type is described below.

MAXScript provides a swap() method that takes two valid assignment destinations as arguments and swaps their values. Its syntax is:

swap <destination> <destination>

Assignment to Variables

A variable is identified with a MAXScript <var_name>, as described in Names. If <destination> is a var_name, it identifies the variable to be assigned a value, as shown in the following examples:

x = 2 + 2 * 3 -- assign to variable x

y = [1, 2, 3] + pos -- assign to variable y

z = #(1, 2, "foo", "bar") -- assign to variable z

z = #oops -- oops, changed my mind

Assignment to Accessors

If <destination> is a <property> or an <array_index>, the assignment is to an accessor. Accessors are constructs used to access the components in compound values, such as arrays and 3D points. There are two kinds of accessors corresponding to the two kinds of compound values in MAXScript:

All 3ds Max objects, such as boxes, spheres, bend modifiers, or bitmap textures are treated as compound values in this sense. All their parameters, such as height and angle, or map file name, are accessed as named components. In MAXScript, these named components are referred to as properties. For more information, see Properties, Methods, Operators, and Literals.

Accessors have one of two forms, an <array_index>:

<operand> [ <expr> ] -- an indexed element

 

or a <property>:

<operand>.<var_name> -- a named property - property var_name of value in operand

 

Examples of assignment to accessors are:

$box01.pos = baseObj.pos + (baseObj.radius*[0,0,1])

Set the position property of object box01 based on the position and radius properties of the object whose value is stored in variable baseObj.

z[2] = d.rotation as eulerangles

Convert the rotation property value of the value stored in variable d to an eulerAngles class value, and store this value in element 2 of the array stored in variable z.

Because MAXScript is an origin 1 language, all sequencable collections in MAXScript start at index position 1. This is an important convention to remember when using the indexing operator.

The syntax definitions allow you to use any <operand> before the . or []. Because accessors are themselves operands, this definition is recursive, which means you can string accessors together to query nested arrays or properties:

my_things[i][j]

my_objects[i].target.position.x

Accessors are evaluated left-to-right. The first example evaluates the i’th element of my_things, which is presumably a nested array or an array of strings, before yielding the j'th element of that element.

The second example retrieves an object from an array of my_objects, then its target, then the position of the target (a 3D point), and finally the x coordinate of that point.

An <operand> can also be a block-expression, allowing you to compute the target object to be accessed.

For example:

(instance myNode).pos=myNode.parent.pos

Create an instance of the node whose value is stored in variable myNode, and set its position to the position of the myNode’s parent object.

(find_table "foo")[n - 2] = "fred"

Call function find_table with a parameter value of "foo". The return value from the function is presumably an array. Set element n-2 of this array to the value "fred".

The named properties defined for the MAXScript values and classes are documented with the value and class descriptions.

Order of Evaluation

In an assignment, <expr> is evaluated before <destination>, as can be seen in the following example:

Script

a=#() -- create an empty array to play with

a[(print "in <destination>";1)]=(print "in <expr>";10)

Output

#() -- result line 1

"in <expr>" -- output from <expr> side of assignment

"in <destination>" -- output from <destination> side of assignment

10 -- result line 2

Assignment in MAXScript is itself an expression and yields the value just assigned. Therefore, you can use assignments inside other expressions or cascade assignments, as in:

x = y = z = 0

#(1, 2, a = [0,0,1], 4)