About Loops and If Statements


A simple MEL script may perform a set of specific tasks once and perform them in the same way every time it's run, but you can also create more complex scripts using loops and if statements. Loops allow you to execute a group or block of commands repeatedly, and if statements allow you to execute a block of commands only under specified conditions. Blocks of commands are contained within curly braces, and each command should be followed by a semicolon as usual.

To use a for-in loop:

1.

Declare an array, and set its values:

 string $array[ ] = {"firstSphere", "secondSphere", "thirdSphere"};. 


2.

Declare a variable of the same type as the array: string $sphere;.

This variable will be used to hold the current element of the array.

3.

Add the for-in statement that will control the loop: for($sphere in $array){.

The code after the opening brace ({) and before the closing brace (}) will repeat as many times as there are elements in $array, and $sphere can be used to access the current element of the array within this block.

4.

Add the commands that you want to be executed within the loop.

5.

Type the closing brace (}) at the end of the script to close the for-in loop.

The script should look like Figure 17.15.

Figure 17.15. The for-in script should look like this.


6.

Press on the numeric keypad to execute the script.

Each of the spheres in the array is translated one unit on the y axis, moving them up to rest on the grid (Figure 17.16).

Figure 17.16. When the script is executed, it creates three spheres, all of which are placed at the origin. They're named using the strings in $array, as seen in the Outliner panel on the left.


Tips

  • You can loop through all selected objects by using a for-in loop with the MEL command 'ls -sl' instead of an array name. For example: for($object in 'ls -sl').


To use a while loop:

1.

Declare a new integer variable, and set its value to one: int $i=1;.

This variable is used to count how many times the loop has been executed.

2.

Add the while statement that will control the loop: while($i<10){.

As long as $i is less than (<) the value 10, the code after the opening brace ({) and before the closing brace (}) will repeat. When $i reaches 10, the loop will end, and any code after it will be executed.

3.

Add the commands that you want to be executed within the loop. Be sure to put a semicolon after each command.

4.

Type $i++; after the other commands. This increments $i, adding 1 to its value.

Because the loop will continue to repeat until $i is equal to 10, it's important to increase the value of $i within the loop. Otherwise, the script will loop infinitely, and Maya will stop responding.

5.

Type the closing brace (}) at the end of the script to close the while loop.

The script should look like Figure 17.17.

Figure 17.17. The while script should look like this.


6.

Press on the numeric keypad to execute the script.

Nine cubes are placed randomly in the view (Figure 17.18).

Figure 17.18. When the script is executed, it creates nine randomly placed cubes.


To use an if statement:

1.

Using the script you created in the previous task, "To use a while loop," add the following before the move statement:

if ($y < 0) { $y = -$y; } 


If $y contains a negative value, it will be set to the positive equivalent instead.

2.

Press on the numeric keypad to execute the script.

The new randomly placed cubes are all positioned at or above the grid (Figure 17.19).

Figure 17.19. With the if statement, all the cubes are placed at positive y values, above the grid.


Tips

  • To edit and reuse a script you've already executed, select it in the upper pane of the Script Editor and then drag it into the lower pane.





Maya 7 for Windows and Macintosh(c) Visual Quickstart Guide
Maya 7 for Windows & Macintosh
ISBN: 0321348990
EAN: 2147483647
Year: 2006
Pages: 185

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net