Procedures can be looked at as a custom set of structured MEL commands that perform a specific action. If you have programmed before you could make the connection that a MEL procedure is much like a function in another languages. A nice aspect of a procedure is that you can feed it a variable and have it return data. There is one stipulation when using procedures in Maya, you can not use a procedure within a procedure.
Simple procedure
Hit enter and then type hello();
|
Simple procedure with user input
Hit enter and type in hello("Walter the Great");This procedure takes user input. When specifying a string value you need to place the string in quotes. |
Passing Values through a procedure
When using a procedure that returns a value specify what data type the return value is the declaration of the procedure.
|
Now that we understand the basics of a procedure, it is important understand the scope of variables.
Variables are only seen within the braces that they reside { }, and their children. Braces are also known as blocks.
Take the following code as an example.
|
What if we, the user, want a variable or procedure that can be seen by any given Melscript after its initilization? This is where global and local variables come into play. Prior to this point we have been using local variables and proceedures. If we want a variable or procedure to be global we must specify this at creation time.
To initialize a global variable you must declare it at the top most block structure of your code. You will need to reinitalize it in
any blocks that you want to use it in after initialization. If you fail to do this, the block structure will assume the varaible is a
new local variable and not associate it with the global variable.
|
Type this in and hit enter. This will initialize our global procedure.This is a global proc that returns a value. To make it a global procedure that does not return a value simply write the procedure as follows.
Lastly when saving a global procedure you want to say your script into your scripts directory.Run this command to find out where your
user script dirctory resides.string $dir = `internalVar -userScriptDir`; |