Procedures


The MEL procedure is similar to variables in many respects. Both must be declared before being used. Both must have a unique name . In addition, both variables and procedures can be declared as either local or global.

The basic procedure declaration is relatively simple, but it can have multiple levels of complexity. The simplest form of procedure declaration is almost identical to the declaration of a variable, seen in Example 4.42.

Example 4.42: The basic procedure syntax.

 proc procedure_name ()         {             statements;         } 

The statements contained within the confines of the curly braces of the procedure are executed whenever the procedure is executed. However, if we create a procedure in such a manner, Maya does not seem to recognize it. The reason for this is quite simple, and actually a useful and well thought-out feature of MEL. In order for a command to be readily available to Maya, it must be declared as a global procedure . In Example 4.43, we see that a global procedure is declared in much the same way as a global variable is.

Example 4.43: The syntax for a global procedure.

 global proc  procedure_name  ()         {             statements;         } 

Now, whenever our procedure name is executed, whether at the command line or from within another script, Maya knows what group of commands we want to evaluate.

What we are essentially doing is creating our own MEL commands. And like any command, we can both pass information using arguments and receive information back from the execution of a procedure. This allows us to create scripts in a modular fashion by writing smaller procedures, which eliminate redundant typing and allow for easy reuse of code.

To pass values to a procedure we put variable declaration statements within the parentheses that follow the procedure name, seen in Example 4.44.

Example 4.44: Declaration of variables in the procedure declaration.

 global proc  procedure_name  (  variable declaration  )         {             statements;         } 

We can pass as many variables as we want, but whenever the procedure is executed, it must be passed all the values for those variables. The values must also be passed in the correct order. In Example 4.45, we create a procedure to quickly hide or unhide objects by name.

Example 4.45: Passing multiple variables.

 global proc setObjectVis (string $obj, int $vis)         {             setAttr ($obj + ".visibility") $vis;         } 

Here we have declared the procedure, and stated that every time we execute it, we will pass it two values. First will be the name of an object, passed as a string. Second will be an integer that will control the visibility of the passed object. We issue the command in Example 4.46.

Example 4.46: Issuing the command from Example 4.45.

 setObjectVis persp 1; 

If we do not pass setObjectVis both an object name and an integer, we receive an error similar to that seen in Example 4.47.

Example 4.47: The error from making an improper script call.

  // Error: Wrong number of arguments on call   to setObjectVis  

The other major feature of the standard MEL command that we might want to add to our procedures is the ability to return information. We add this capability to a procedure declaration by simply adding the type of information we want to pass back right before the name of the procedure. The return type can be any of the various variable types. The syntax model for this is seen in Example 4.48.

Example 4.48: The syntax for adding a return statement.

 global proc return_type  procedure_name  ()         {             statements;         } 

When you want to return an array, place square brackets right after the type of return value. In the two procedures in Example 4.49, the first procedure returns a single integer, while the second returns a string array.

Example 4.49: Making the returned value an array.

 global proc int isLightRed ()         {             statements;         }     global proc string[] listAllSkinnedObjects ()         {             statements;         } 



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