Loops

 

Conditional statements execute statements when a certain test condition is true. Loop statements repeatedly execute statements while a certain test condition is true.

 

Although looping allows you great control and power in writing scripts, it can also be a source of problems. Loops that endlessly repeat execution of statements will do just that-never end. For example, executing "while (1);" will loop nothing forever. Maya will never stop executing this line; don't try it unless you are prepared to quit Maya.

 

In MAXScript, you can usually break the execution of an endless loop by holding down the Esc key.

 

This section describes the three main loop statements, while, do-while, and for. The loop interruption statements, continue and break, are also described. For information on what determines a true condition, see Conditional actions.

 

while

 

A while loop continues to execute some designated statements, as long as a certain condition is true. It has the following format:

while ( test condition )

statement;

 

The MAXScript form of the while loop is

while (test_expression) do

expression

 

 

If test condition is true, Maya executes the statement. Maya then evaluates the test condition again. If the test condition is true, it executes the statement again. This process continues until the test condition is false. At that time the loop exits.

 

MAXScript behaves the same way.

 

 

Example

int $files = 5;

while ($files > 2)

print("There are " + $files-- + " files left.\n");

 

The above example produces the following output:

There are 5 files left.

There are 4 files left.

There are 3 files left.

 

When the condition is first evaluated in the above example, files has the value of 5 which creates the true condition that 5 is greater than 2. Therefore, the print statement is executed, which also decrements files to a value of 4.

 

At this point, the test condition is evaluated again with a value of 4 for files. The test condition is again true so the print statement is executed, decrementing files to a value of 3. Again the test condition is evaluated to be true, and so the print statement is executed. Finally, with a value of 2 for files, the test condition is false, since 2 is not greater than 2. The while statement exits.

 

 

The same example in MAXScript would look like:

 

files = 5

while files > 2 do

format "There are % files left.\n" files; files -= 1

 

Note that in MAXScript, files -=1 is always evaluated first, before the result is used in the next expression. Thus, saying

format "There are % files left.\n" files -=1

would print values from 4 to 2 instead of 5 to 3 as in MEL. To match MEL's behavior, the decrementing must be performed after the printing.

 

 

To have more than one line of MEL script executed when the test condition is true, use grouping (see Grouping operations). For information on the decrement operator (--), see Increments and decrements.

 

Example

int $t = -10;

while ($t < 0)

{

print("t minus " + (-$t) + " seconds and counting.\n");

$t = $t + 1;

}

print("Houston, we have lift-off!\n");

 

 

MAXScript:

t = -10;

while t < 0 do

(

format "t minus % seconds and counting.\n" -t

t = t + 1

)

format "Houston, we have lift-off!\n"

 

do-while

 

A do-while loop is very similar to a while loop because both execute some specified statement as long as a certain test condition is true. However, the do-while loop first executes the statement and then checks whether or not to repeat the loop. This ensures that the statement is executed at least once.

 

A do-while loop has the following format.

do

statement;

while (test condition);

 

Note that the do-while loop must terminate with a semicolon (;) in MEL.

 

 

do

expression

while test_condition

 

In MAXScript, there is no need for termination.

 

Example

do

print("This is randomization kid.\n");

while (rand(2));

 

In the above example, rand(2) is a float value between 0 and 2. This number is converted to an int of value 0, 1, or 2 (see Conditional). A value of 1 or 2 will repeat the loop; a value of 0 will exit the loop. Each time the test condition is evaluated, the rand(2) can return a different number. Therefore, the test condition has about a 50 percent chance of being true and about a 50 percent chance of being false.

 

Here is the same example in MAXScript:

do

print "This is randomization kid."

while (random 0 2) != 0

 

In the case of MAXScript, random 0 2 will returns an Integer of 0, 1 or 2, because both the lower and upper limit is Integer. Because the condition must be of BooleanClass in MAXScript, you have to compare the result to 0 to get True or False. Chances are around 33.3333 % to get 0 and 66.6666% to get a positive number.To get 50-50 chance, you should use random 0 1

 

 

Here is a similar example using grouping:

do {

print("Here comes the rain again.\n");

print("Raining on my head like a memory.\n");

} while (rand(2));

 

And this is the MAXScript version of the example using a block expression:

do (

format "Here comes the rain again.\n"

format "Raining on my head like a memory.\n"

) while (random 0 1) != 0

 

 

for

 

Often you may want a loop control that has initialization, test, and a change of condition statement. For this you can use the for loop. The for loop has the following format:

for (initializer; test condition; change of condition)

statement;

 

The initializer is executed only once, before the rest of the for loop is executed. The initializer is a statement that is generally used to assign a value to a variable in the test condition. You can use the initializer to execute any statement or you can omit the initializer entirely. However, it cannot be used to declare a variable.

 

The for loop evaluates test condition before executing statement. This part of the for loop is exactly like the while loop. In fact, a for loop is just a while loop with an initializer and change of condition statement. However, if the test condition is missing, the test condition is assumed to always be true, which would create an error in a while loop. While the test condition is true, the statement is executed; as soon as it is evaluated to be false, the loop is exited.

 

The change of condition statement is executed at the end of each iteration of the loop, as if it were added to the end of the loop. Typically, the change of condition statement is used to change some loop-control variable, although you can use it for any statement or omit it entirely.

 

Examples

int $Index;

for ($Index = 0; $Index < 2; $Index++)

print("Give me a beat!\n");

for ($Index = 0; $Index < 3;)

{

print("Can I get some?\n");

$Index = $Index + 1;

}

for (; $Index--;)

print("Give me all your lovin'.\n");

for (;;)

if (rand(5))

print("Forever yours.\n");

else

break;

 

These examples show some different possibilities when using the for loop. In the last for loop, the if else statement is considered a single statement. For information on the decrement or increment operators, see Increments and decrements. For more information on what determines a true condition, see Conditional actions.

 

Although the initializer cannot be used to declare a variable, it can be used to implicitly declare a variable.

 

Example

for ($xxx = 0; $xxx < 3; $xxx++)

print("Hit me three times.\n");

 

The for loop also allows you to use multiple initializers or multiple incrementors. To do this, separate each initializer and change of condition statement with a comma (,).

 

Examples

int $nx1, $nx2;

for ($nx1 = 0, $nx2 = 10; $nx1 < $nx2; $nx1++, $nx2--)

print("nx1 = " + $nx1 + ", nx2 = " + $nx2 + "\n");

float $cg = 25.3;

for (; $cg < 93.8; $nx1--, $nx2 = rand(11), $cg *= 1.7)

{

print("nx1 = " + $nx1 + ", nx2 = " + $nx2 + "\n");

print( "CG levels currently at " + $cg + "\n\n");

}

 

For information on the shortcut operator, *=, see Shortcuts.

 

for-in

 

The for-in loop is a derivative of the for loop. It is specialized for working with arrays. Often you might want to iterate through all the elements of an array. While a for loop requires an index variable, initializer, test condition, and incrementor, the for-in loop requires only an element variable. The for-in loop has the following format:

for (element in array)

statement;

 

Example: for-in loop

string $car;

string $cars[3] = {"n NSX", " Porsche", "n Acura"};

for ($car in $cars)

print("I have a" + $car + ".\n");

 

The result is:

I have an NSX.

I have a Porsche.

I have an Acura.

 

Example: for loop

int $carIndex;

string $cars[3] = {"n NSX", " Porsche", "n Acura"};

for ($carIndex = 0; $carIndex < size($cars); $carIndex++)

print("I have a" + $cars[$carIndex] + ".\n");

 

Both of the above for loops are equivalent but the for-in loop requires less typing.

 

Example

vector $coordinates[2] = {<<1, 2, 3>>, <<4, 3, 7>>};

for ($coordinate in $coordinates)

{

print("Coordinate: " + $coordinate + ".\n");

print("You sank my battleship!\n");

}

 

continue

 

The continue statement works inside all loops. It forces the next iteration of the loop to occur, skipping any remaining statements in the loop.

 

Example

float $fox = 5;

for ($fox = 0; $fox < 5; $fox++)

{

print("$fox equals: " + $fox + "\n");

if ($fox > 2)

continue;

print(" Got here\n");

}

 

The output is:

$fox equals: 0

Got here

$fox equals: 1

Got here

$fox equals: 2

Got here

$fox equals: 3

$fox equals: 4

 

Without the if statement, the loop would also output Got here after the lines $fox equals: 3 and $fox equals: 4. Maya ignores the continue statement until $fox increases to a value greater than 2. When $fox becomes 3 or greater, the continue statement executes and skips the remaining statement in the loop, the statement that prints got here.

 

break

 

The break statement exits a loop immediately. You can use a break statement inside all loops.

 

Example

float $free = 0;

while ($free < 10)

{

print("$free equals: " + $free + "\n");

if ($free++ == 3)

break;

}

print("I'm free!");

 

The output is:

$free equals: 0

$free equals: 1

$free equals: 2

$free equals: 3

I'm free!

 

Without the if statement, the loop would execute 10 times and display the numbers 0 through 9. The break statement terminates the loop after $free is equal to 3, so the print statement in the loop is only executed four times.