Flow Control


Within any loop grouping, you can use flow control statements. The two flow control terms not yet fully covered ( technically, all loops and conditional statements are flow control statements ) are break and continue . The command break was covered briefly within our discussion of the switch statement, but it can be used within any loop. When the break command is evaluated, it immediately exits out of the current group of commands. It can be useful for avoiding possible math errors, like dividing by 0, which is demonstrated in Example 4.50.

Example 4.50: Using flow control statements within a script.

 for ( $eachObj in $selectionList )         {         $objTX = `getAttr ( $eachObj + ".translateX" )`;         if ( $objTX == 0 )             break;         setAttr ( $eachObj + ".translateX" ) ( 5 / $objTX );         } 

The other flow control statement used within loops is continue. When the continue statement is evaluated, MEL skips the rest of the statements in the group, and proceeds to the next iteration of the loop.

It is sometimes hard to figure out when it is best to use continue rather than use other flow control statements, like the if statement. There is no hard-and-fast rule; rather, it is a skill that develops with time and experience.

Variable Visibility

The very last issue to be aware of when dealing with groups is that of variable visibility . A variable is only visible to the group in which it is declared, and groups that are nested within that group. This is quite useful when trying to manage memory and keep track of variable names . If we declare a local variable inside a group and attempt to call on it from outside that group, Maya will issue an error. Once a particular group has finished evaluating, whether it is a loop, conditional statement, or procedure, all local variables declared within that group are cleared from memory. It is best to declare a variable at the lowest level possible to carefully preserve memory.

One obvious exception is global variables. While global variables carry their information across procedures, it is necessary to re-declare their existence before using them in any way. Be careful not to combine declaration and assignment statements with global variables, because you will overwrite any information stored within those variables.




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