Loops


Loops

The ability to repeat commands over and over, while not the most exciting aspect of programming, is one of the most common uses of scripting. There are two basic loops, although each has a subset that alters its behavior slightly.

The while Loop

The while loop sends MEL into a loop until a specific test condition is met. Like conditional statements, the condition is Boolean, and continues to execute as long as the test condition returns true. The syntax for the while statement is similar to the if statement, as we can see in Example 4.29.

Example 4.29: The syntax for the while statement.

 while (condition)         statement; 

When MEL encounters a while statement, it evaluates the condition, and if true, executes the statement. It then re- evaluates the conditional statement, and if it still returns true, repeats the process. This can be dangerous. If the test condition is never satisfied, the loop will never end. The code in Example 4.30 will, for example, never self-terminate, continually printing out ever-increasing numbers . If this code is executed, the current session of Maya has to be terminated . (Please refer to your OS documentation to determine how best to terminate a process.)

Example 4.30: An endless while loop.

 int $i = 1;     while ($i > 0)         print (($i++) + "\n"); 

The do-while Loop

The do-while loop is similar to the while loop, but differs in one important way. The statement is evaluated before the test condition is evaluated. After the first execution, the behavior of the do-while loop is identical to the while loop. However, that first execution is an important difference, because the while loop has no guarantees that the statement will be evaluated even once. The syntax for the do-while loop is laid out in a manner similar to the while loop, but with the statement before the condition, seen in Example 4.31.

Example 4.31: The do-while syntax.

 do         statement;     while (condition); 

An important difference is the semicolon after the closing parentheses encompassing the test condition. Neglecting this semicolon will result in an error.

Both the while loop and the do-while loop are useful when you cannot predict how many times you will have to execute a command. For example, if you wanted to move an object called ball by 5 units along X until it is beyond the 100-unit mark, you could use Example 4.32. You don t need to know what the starting position of ball is; MEL will just move the object in 5-unit increments until the translate X value is larger than 100.

Example 4.32: The while loop in use.

 while (`getAttr ball.translateX` < 100)         move r 5 0 0; 

The for Loop

The other basic type of loop is the for loop. The for loop is almost a version of the while loop, and we could actually build the same functionality of a for loop with the while statement, and vice versa. The for statement s conditional argument contains three aspects: the initialization , the test condition, and the condition change . Each of these elements is separated by a semicolon, seen in Example 4.33.

Example 4.33: The basic for loop syntax.

 for (initialization; condition; update condition)         statement; 

The initialization step is optional, but is often useful. It is used to set the value that will be evaluated by the test condition the first time. Note that it is possible to implicitly declare a variable in the initialization step. Next comes the actual test condition, which is the same as you would use in any conditional statement. Finally comes the update action, which is simply a statement that is executed after the commands in the for loop. Many programmers use for loops to iterate through arrays. In Example 4.34, we iterate through the array $selList , which holds the selection list captured in the first line of code. We also use the variable $i to stand in for the index value referenced in the array in the print statement;

Example 4.34: Using a for loop to parse an array.

 string $selList[] = `ls selection`;          for ($i = 0; $i < `size $selList`; $i++)         print ($selList[$i] + "\n"); 

As you can see, because we can use the integer $i within the actual loop itself, it gives us easy access to the value of the current index of an array. Also notice that the test condition continues the for loop as long as $i is a value less than the value returned by the size command. Because Maya uses a 0-based index system, the last index number will be 1 less than the total number of items in an array. It is a somewhat confusing concept, but one that is important to understand.

Any of the three elements of the for loop ”the initialization, the test condition, and the condition change ”can be left out. Of course, eliminating the test condition will cause the loop to never end, causing Maya to hang. Even if you don t want to use any particular element of the for statement, the parentheses must contain three semicolons. These semicolons signify to Maya which element is which, seen in Example 4.35.

Example 4.35: Omitting unnecessary elements of the for statement.

 for (; $i != 15;)         print $++i; 

In Example 4.36, we see that we can also initialize and update multiple items in the for statement; simply separate the items with commas.

Example 4.36: Multiple initializations.

 for ($i = 0, $e = 15; $e >= $i; $e--)         print "This is fun!\n"; 

The for-in Loop

As we discovered , programmers across many languages use the for loop to iterate through arrays. While this structure works fine, MEL actually provides a separate loop that requires less typing and is more readable than the standard for loop. This loop, called the for-in loop, works only with arrays. Its structure is seen in Example 4.37.

Example 4.37: The for in loop syntax.

 for (  $variable  in  $array  )         statement; 

Using the for-in loop provides no speed benefit during execution, but it does make the loop both shorter to write and easier to read. For these reasons, the for-in loop is used the majority of the time to loop through an array. In Example 4.38, we duplicate the functionality of Example 4.34, but in a more elegant fashion.

Example 4.38: Using a for-in loop to parse an array.

 string $selectionList[] = `ls selection`;       for ($eachObj in $selectionList)     print ($eachObj + "\n"); 

As with regular for loops, you can implicitly declare a new variable in the for-in statement. In both types of for loops, if we implicitly declare the variable in this manner, it is visible only within that loop.

While for-in loops are generally more effective for iterating through arrays, if we need to know the index value of a current array element, such as if we are referring to the corresponding elements of multiple arrays, we can of course use the regular for loop, as we do in Example 4.39.

Example 4.39: An example of when the for loop is superior to the for-in loop.

 for ($i = 0; $i < `size $listOfJoints`; $i++)         print ($listOfJoints[$i]                 + " : "                 + $skinnedObject[$i]                 + "\n"); 



The MEL Companion
The MEL Companion: Maya Scripting for 3D Artists (Charles River Media Graphics)
ISBN: 1584502754
EAN: 2147483647
Year: 2003
Pages: 101

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