Титульная страница
ISO 9000 ISO 14000
GMP Consulting
 
Mastering 3D Studio MAX R3

Previous Table of Contents Next


This script will return the result immediately because you used IF... THEN... ELSE, covering all conditions. It also demonstrates that you do not need to make a comparison for a temperature lower than 40, because it’s the only option that remains after all other comparisons.

Now select all the lines in this script and drag them to a toolbar, creating a Macro Script. Edit the button so it reads “Temperature”. Once you press this Temperature button (see Figure 13.7), MAX will ask you the temperature, you type it, and you’ll be told whether it’s hot or cold.


FIGURE 13.7  Temperature script

Repeating Tasks

The biggest advantage of any programming language is the ability to repeat tasks automatically. This can also be done in MAXScript, and will be useful when dealing with objects, materials, etc.

Arrays

To know more about repeating tasks, you need to learn about arrays. An array is a variable made of a series of nested variables. Arrays can hold any type of variables and can even mix types.

An array variable is defined by assigning it and the values of its members, all at once. For instance, a = #(10,20,30) will create an array of three variables, and will set 10, 20, and 30 as the values of each member of the array. Then, to access any element of this array, you use variable[i], where i is the index of this element. For instance, a[1] will return 10, a[2] is 20, and a[3] is 30.

An array, like a string, has a property to show you how many elements are in the array. This can be done by typing array.count.

Adding Elements to an Array

To add elements to an array, simply type array[index]. This index is any integer number. For instance, if you create an empty array, using a = #(), and add the third element of this array using a[3] = 5, you will create an array with two undefined values, a[1] and a[2].

To automatically add a value to the next available element of an array, you can use the APPEND command. Continuing from the example above, you could use append a “hot” and “hot” would be added as the fourth element of this array.

Sequential Tasks

By using the FOR command, you can make the script repeat the same steps for as long as you want. FOR is essential when manipulating objects, as you will see in Chapter 14.

The FOR command counts from one number to another, and repeats a series of commands within this interval. For instance,

for x in 1 to 6 do print x

will repeat the PRINT command six times, printing “1 2 3 4 5 6”.

FOR is used many times with arrays, accessing the different elements of an array. For instance,

for i in 1 to a.count do print a[i]

will step through all elements of the array and print them.

Non-Sequential Tasks

Sometimes you don’t know how many times you will need to repeat an action; it must be repeated until you meet a condition, then the script can move on to something else. This can be done using WHILE.

The WHILE command will run while a particular condition is true, and will stop running when that condition has changed. For instance, you can use WHILE to ask the user to enter data to be used in a script; when the user types Exit, the script stops entering data. This can be done easily like this:

test = 0
data = #()
while test != “Exit” do
 (
 test = getkbline prompt:”Enter data (type Exit to stop): “
 if test != “Exit” then append data test
 )

This example shows how parentheses define a series of commands to be executed inside the WHILE command. You can use parentheses every time an action needs more than one command, in IF... THEN... ELSE or in FOR statements. The parentheses also help you organize and understand the script better.


NOTE The WHILE command evaluates all lines before it checks for the condition again. That’s why you added if test != “Exit”, because the WHILE will only exit the loop after the loop ends. This way you will not append “Exit” to the array.

WARNING Use the WHILE command with caution because it may freeze a script. If you ask the script to check for a variable and forget to change the result of this variable, the WHILE will loop infinitely. To exit this loop, you must press the Esc key, which will interrupt any script being processed.

Creating Functions

Functions are used to create new commands in MAXScript. These commands will process some data and return an answer. Functions are useful when you need to repeat actions frequently in a script; instead of repeating a series of commands, you create a function and use it, making the script smaller and faster.

To define a function, you need to define what data will be needed by the function. For instance, let’s create a function to convert integer numbers to digit string numbers, like “0001” for 1 and “0100” for 100, used when you add a suffix to a bitmap that will be saved to disk. This function will take the number and return the converted string.

First, let’s define the function:

function nth_number number =
 (
 if number < 10 then
 return_value = (“000” + number as string)
 else
 if number < 100 then
 return_value = (“00” + number as string)
 else
 if number < 1000 then
 return_value = (“0” + number as string)
 else
 return_value = number as string
 return_value
 )

In this case, the function is named nth_number, and it requires one argument, number, that will be processed. This number will be checked within the function and a return_value variable will be created. This variable will return the formatted number; this function can be used as

formatted_number = nth_number number

where you would substitute any integer for number.

You can test this script by typing a = nth_number 27; it will return “0027”.

Organizing Complex Data

To organize complex data in MAXScript you can use structures. Structures are useful to help you understand variables and their properties. A structure is a compound variable where you can define properties; to define a structure, simply type

struct variablename (property1name, property2name, ...)

For instance, you can create a structure to manage a project. This structure will have three properties: the name of the person who is working, starting time, and end time. The following code defines a structure type and then creates one variable using that type:

struct project (name, start, end)
oct2599 = project (name:”John”, start:11.5, end:14.25)


Previous Table of Contents Next

© 2000, Frol (selection, edition, publication)

 
Rambler's Top100