Defining Custom Functions

MAXScript's built-in functions are very powerful. They provide access to objects, allowing you to read and set their properties. However, scripting in 3ds max normally entails going beyond simple object creation. Fortunately, MAXScript allows you to make your own functions, giving you the ability to streamline and customize your work.

Local and Global Variables

In MAXScript, there are two classes of variables that you can create: "local" and "global" variables. When a variable is global, it means that the variable can be used anywhere in the program. The only place you cannot use a global variable is before you have defined it. For example, the following lines will produce an error:

sphere radius:rad

global rad = 10

The variable "rad" does not exist until it has been defined. The following is an acceptable form of the previous lines:

global rad = 10

sphere radius:rad

Now that "rad" has been defined, MAXScript was able to use it in the definition of a sphere. In fact, since "rad" is a global variable, it can be used throughout the rest of the script.

There are several global variables that can be used without defining them first. They are the system global variables, which have already been created and assigned to a default value. Most of these variables can be reassigned to different values. For more information on the system global variables, see the 3ds max System Globals topic in the MAXScript reference.

Unlike global variables, local variables can be used only within the block in which they are defined. A block is a grouping of MAXScript code, such as the statements within loops and conditional statements. Here is an example of a for loop:

for i = 1 to 3 do

(

local rad = 10

s = sphere()

s.pos.x = i * 10

s.radius = rad

)

Local variables can only exist within a block. For this reason, they must be created in a block. If you try to define a local variable from the top level of MAXScript, you will receive an error message.

In the previous example, you defined a local variable named "rad". This variable is active only within the parentheses inside the for loop; if you attempt to use the variable "rad" elsewhere, you will get an error.

Although rad is initialized to a specific value, this is not necessary. When you define variables using a "local" or "global" identifier, you can do so without assigning an initial value. The name of the variable will be recognized. However, the software will not know the type of the variable (name, number, color, etc.). MAXScript automatically assigns the variable a special value of "undefined". The first time you assign something to the variable, it will become defined by the data type that you have assigned it to. In practice, it is always best to define variables with an initial value.

It is important to note that rad is not the only local variable in the previous example. The variable "s" is also a local variable, and cannot be accessed outside of the parentheses. It is not necessary to declare every variable with a "global" or "local" statement. You learned previously that you can implicitly create a variable by simply assigning a value to it. If you do not make the "global" or "local" distinction upon your variable, MAXScript will create the appropriate association, depending on where the variable is defined in the script. For example if you create a variable inside a block, MAXScript automatically treats it as a local variable. On the other hand, any variable created at the top level of MAXScript (outside of all blocks) is always a global variable. As mentioned previously, MAXScript does not allow for the creation of local variables outside of a block. These concepts are presented in the following example:

for i = 1 to 5

(

global rad = 10

cyl_height = 25

c = cylinder()

c.radius = rad

c.height = cyl_height

c.pos.x = i * 25

)

s = sphere radius:rad

In this example, four variables are created: rad, cyl_height, c, and s. Because they were implicitly created within a block, c and cyl_height are both local variables. The other two variables are both global; s, because it was created outside of a block, and rad, because it was declared as a global variable. For this reason, you can use rad again to define the sphere, s.

Defining Custom Functions

In MAXScript, you can use functions to perform almost any of the software's operations. Start with a simple function definition that subtracts two variables:

MAXScript returns subtract() to let you know that it has defined the function, subtract.

The definition contains the keyword function (you can also use the word fn in place of function), followed by the name of the function (subtract), followed by the list of variables (x and y). The equal sign signifies the start of the body of the function. The body is the series of statements that the function executes. Even if there is only one statement, the body of the function must be in parentheses.

Note: Any variables created in the body of a function are local variables unless otherwise specified.

Once a function has been defined, it can be used anywhere throughout the script. Therefore, it is useful to place function definitions at the beginning of your scripts.

In order to use a function, you simply enter the function name and the values for its variables:

This example uses what are known as positional arguments. When you call the function, you must supply both arguments, and you must supply them in the proper order for the function to execute properly.

The second type of arguments you can use are keyword arguments. This is what is used for all of the object constructors in MAXScript. The function definition is the same, with the exception of declaring the keyword arguments, or default values for the variables in a function. The following example determines whether a number is positive, negative, or equal to zero:

function sign val:0 =

(

if val == 0

then messagebox ("Equal to 0")

else if val > 0

then messagebox ("Greater than 0")

else messagebox ("Less than 0")

)

The messagebox command causes a message box to pop up on the screen, displaying the argument in the parentheses that follow the command.

In this example, a parameter called val has been declared in the function definition. The value after the colon is the parameter's default value. When the function definition contains parameters like this, they are optional in the function call. If you enter sign(), without specifying any parameters in the function call, MAXScript uses the default value:

The "Equal to 0" message box appears, since the default value for val (0) will be used. However, if you do define val:

sign val:-5

the "Less than 0" message appears. You can include as many keyword arguments in a function as you need, and they can be entered in any order when calling the function. This is useful when you consider functions such as object-creation functions, where the new object might have 20 or 30 parameters associated with it. If you wanted to use positional argument functions to create the same objects, you would have to provide all of these parameters, in the right order, each time you wanted to call the function.

It is also possible to use both positional and keyword arguments in the same function call. In this case, you must use positional arguments first, followed by the keyword arguments:

function mycube side position:[0, 0, 0] =

(

box length:side width:side height:side pos:position

)

This function requires you to enter the size of the cube, but the position is optional.

Next Topic

Structure Definitions