About Procedures


Procedures provide a method of reusing blocks of code. At minimum, a procedure consists of a name and a block of code, or body, that is executed when the procedure is called. You must define a procedure before you use it, similar to the way you define a variable. A procedure's definition begins with the keyword proc, followed by the procedure's name and then a bracketed code block.

Procedures may also have one or more arguments. Arguments are variables that must be defined in parentheses after the procedure's name. You can then use these variables within the body of the procedure, without assigning values to them. Their values are determined when you call the procedure.

To call a procedure you've defined, simply use its name in a script, followed by the values for its arguments (if any) in parentheses.

Procedures are local by default. Local procedures can only be accessed from the same script they're defined in. You can also define a procedure as being global by adding the global keyword before its proc definition. This allows it to be accessed from any script.

To define a procedure:

1.

In the Script Editor, enter proc followed by the name of the procedure. For example: proc cubeStack.

2.

If you want the procedure to have arguments, declare their variables in a list separated by commas and enclosed in parentheses. For example: (int $height, float $x, float $z). Otherwise, enter a pair of parentheses: ( ).

3.

Add a brace ({) to begin the procedure's body, and then press and enter the commands to be executed by the procedure. When you're done, add a closing brace (}) to end the procedure's body.

The script should look like Figure 17.20.

Figure 17.20. The procedure should look like this.


To call a procedure:

1.

Define a procedure by following the steps in the previous task.

2.

After the procedure's definition, enter the name of the procedure followed by parentheses enclosing its arguments, if any, and then a semicolon. For example: cubeStack(3, 0, 0);.

3.

Repeat step 2 as many times as you want to call the procedure. If the procedure has arguments, you can use different values for them with every call (Figure 17.21).

Figure 17.21. You can call a procedure with different arguments to get different results.


4.

Press on the numeric keypad to execute the script.

Three stacks of cubes are created, each with a different position and height (Figure 17.22).

Figure 17.22. Here the cubeStack procedure has been called three times, with different values for $height and $x.





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