Structure Definition

You can create your own compound values in MAXScript using Structure definitions. Structure definitions let you define the layout of new 'classes' of values that you can then create and work with in your code. The syntax for a structure definition is:

struct <struct_name> ( <member> { , <member> } )

where each <member> can be one of:

<name> [ = <expr> ] -- name and optional initial value

<function_def>

Example:

struct person (name, height, age, sex)

The above example defines a new 'person' class. You create values of this class using the 'person' constructor:

bill = person name:"Bill" height:72 age:34 sex:#male

creates an instance of the person class, storing the instance in variable bill. Member name is initialized to the string value "Bill", height to the integer value 72, age to the integer value 34, and sex to the name value #male.

joe = person name:"Joseph" sex:#male

creates an instance of the person class, storing the instance in variable joe. Member name is initialized to the string value "Joseph" and sex to the name value #male. Since the height and age members are not assigned values, and do not have optional initial values supplied in the structure definition, they default to a value of undefined.

You access structured values using the standard property accessing syntax in MAXScript:

bill.age    -> 34

joe.age     -> undefined

joe.age = bill.age - 4

Structure definitions are useful as an alternative to arrays when you have a fixed, usually small number of independent components and the code to work with them is much clearer when they can be referenced by property name, rather than by index number.

As with function definitions, a structure definition actually creates a value to represent the definition and stores it in a variable of the same name as the structure. You can therefore store the structure definitions in other compound objects or pass them as function arguments The classOf() function returns this structure definition value when applied to these values, so you can use it to test the definition of structure instances at runtime. For example:

classOf bill    -> person

The struct value constructors are just like function calls and take positional argument initializers as well as keyword initializers. The elements of the new struct are filled-in in order from any positional arguments and then by name from any keyword arguments.

For example, given the following struct definition,

struct foo (name, age, height, hair="brown")

you can create instances, in several ways:

f1 = foo "bill" 23 72       -- fills in name, age, height in order

f2 = foo age:45 name:"sam"  -- random named initializers

f3 = foo "mary" hair:"red"  -- first name, then keywords

There are several methods associated with struct values:

getProperty <struct>

Allows you to access properties of the structure.

getPropNames

Returns the properties of the structure as an array.

copy <struct>

Creates a copy of the structure. The copy made is what is called a shallow copy - only a copy of the upper-level value itself (that is, the struct value) is created. Copies aren't made of compound values stored in the structure, rather the same compound value is stored in both struct values. This can be seen in the following example, where changing a component value of a compound value in a copy of a struct value also changes value in the original struct value:

Script:

Struct test (pos1, pos2) -- define a structure

testval = test [1,2,3] [4,5,6] -- create struct value

testval_copy=copy testval      -- copy the struct value

testval_copy.pos1.x=10         -- change component value of a compound

                               -- value in the copy

testval                        -- look at original struct value

Output:

#Struct:test( -- result line 1

  pos1:<data>,

  pos2:<data>)

#test(pos1:[1,2,3], pos2:[4,5,6])  -- result line 2

#test(pos1:[1,2,3], pos2:[4,5,6])  -- result line 3

10                                 -- result line 4

#test(pos1:[10,2,3], pos2:[4,5,6]) -- result line 6

Methods