Mel - Maya Embedded Language

<HOME>   <PREVIOUS>   <NEXT>

Procedures

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
proc hello()
{
	print ("\nHello World");
}
Hit enter and then type hello();


Simple procedure with user input
proc hello(string $name)
{
	print ("\nHello World, let us clap our hands for "+$name);
}

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
proc int myVal(int $a, int $b)
{
	int $c = $a + $b;
	return $c;
}
int $returned = myVal(10,20);
print $returned;
When using a procedure that returns a value specify what data type the return value is the declaration of the procedure.

Globalization

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.

{
int $a = 5;
	{
	int $b =6;
	print $a;
	}
print $b;
}
variable $a will print a value of 5 because the print command is in the scope of variable $a.
When print $b is reached we recieve an error because variable $b was initialized in a different block. Variable $a was able to print because the print command resided in a child block.

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.

Global Variables
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.
{
global string $myGlobalString;
	{
	global string $myGlobalString = "Oh, I get it";
	print $myGlobalString;
	}
}
An important complication of global variables is to realize that they are only initilized once. This could be a good thing or a bad thing depending on how you look at it. If you are dependent on the global variable to have a constant start value, you may want to have a procedure that reassigns the global variables value after it is use.
Global Procedures
Global Procedures are not much different than global variables. Once declared they are visible throughout Maya.
{
global proc float myGlobalProc(float $myInput)
	{
	float $myNewValue = $myInput * 3.25;
	return $myNewValue;
	}
}


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.
{
global proc myGlobalProc2(float $myInput)
	{
	float $myNewValue = $myInput * 3.25;
	print $myNewValue;
	}
}
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`;

<Top>