by Johan Borgström
In this post I will give you a brief introduction to how you can make use of MEL in your Maya workflow. Since I am a designer and my approach to maya has always been artistic rather than technical I will try to explain it in less technical terms.
What is MEL ?
MEL is an abbreviation for Maya Embedded Language. Mel is a scripting launguage. This means that you can type a Mel command and execute that command as soon as you hit enter. This differs from a compiled language, a compiled language would require you convert your code into commands the computer understands. Since Mel is a scripting language, it makes Mel extremely fast to use. It is deeply integrated with Maya, and allows you to do anything from open a window or perform a simple action with a command, to total customization of the Maya interface, to writing an entirely new application on top of Maya. Practically everything that Maya can do can be accomplished through MEL (and what can’t can be done in another language with the Maya API). The great benefit of using MEL is that it helps you to perform tedious and or complex tasks. And it is REUSABLE. It will be of great help to you, that is a promise !
Mel Command, Procedure and Script
A Mel command performs one action. The mel command sphere creates a nurbs sphere. A MEL procedure is a group of MEL commands that can be executed by a single command. A MEL script can contain one or more MEL procedures. Procedures can be local or global. Local procedures are visible only within their script file. Global procedures are visible outside their file. Typically, you create procedures in a script with a text editor. You can also enter procedures in the Script Editor.
Where are MEL scripts stored ?
When you install Maya a folder called maya is created in the my documents folder. In that folder you will find a folder called 2009, if that is the version of Maya you are running. In that folder there is a folder called scripts, i.e C:\Documents and Settings\user\My documents\maya\2009\scripts. This is the default folder were you can put MEL scripts you downloaded or created yourself. There are ways to use other folders but for know we will use the default folder.
How do I use MEL ?
When maya is launched it searches all the defined script folders and read the scripts in to memory. Before you execute a procedure, you must declare it. Declaring a procedure loads it into Maya’s memory so it can be executed. In the command line or script editor you type source followed by the name of the script and hit enter. When the script is in memory you simple type the name of the procedure you want to execute.
- Note if you added a script to the script folder after you launched maya you won´t be able to source the script. To make Maya re-read the scripts folder you use the command rehash.
- If a global procedure has the same name as the script and is in a Maya scripts folder it will be sourced when maya launches.
How to write a MEL script !
When you learn to speak a new language you must learn the grammar to be able to express yourself correctly. The same is true when you learn how to write scripts. When you write something that the computer doesn´t understand you will get a syntax error or it simply won´t work. So lets have a look at how a Mel script is constructed. First of all if the “debugging” featutres of mel is really bad. If you´ve got an error in your script, the help you get from maya to try to understand what is causing the error is sometimes not to helpful, to say the least. When I write scripts I have an itterative process and test my script in Maya while I am writing it, to make sure it works. Sometimes it can be hard to figure out what´s causing an error. So my tip is that when you´ve written a bit of code, try it out and see if it works, write some more and check again. This way you narrow down the potential sources of errors. So lets start out really simple. The example below is a pretty useless script in terms of production but it helps to illustrate the construction of a mel script. All the script below does is to print out the text Hello You !. You will see the text in the command line as well in the script editor. Open the script editor and write the following lines in the input area.
print “Hello You!\n”;
Select all the text you´ve written and hit ctrl+enter on your keyboard. The text Hello You! is printed out in the command line and the script editor. The print command is really helpful when you write or debug a script and is also useful when you want to give feedback to a user of your script. The script editor lets you write scripts and or simple commands. If you for instance write sphere a nurbs sphere is created. Altough you can create new tabs in the script editor and keep scripts written there it is a good idea to save your scripts as an external script file.
Global procedure
Lets create an external script file with the same simple function as above. You can write scripts in a simple text editor but I would reccomend that you get a proper script editor. It will be easier. But for know we will use notepad. Create a new file and save it in your scipts folder as jb_testing_mel.mel. Make sure that the file suffix is .mel. Copy the text below and paste it in your document. Save the file. Open Maya.
global proc jb_testing_mel()
{
print “Hello You!\n”;
}
Note: If you had maya open before you placed the script in the scripts folder type rehash in the command line and execute the command.
To execute our script we first need to source it. Type source jb_testing_mel; in the command line and hit enter. Now the script is ready to use. So lets use our amazing script ! Type jb_testing_mel; in the command line and hit enter. That is how you execute a mel script.
The use of a global procedure makes it possible to execute a number of MEL command at one time. The script above is a simple example of a global procedure. After the word global proc is the name of the procedure. Inbetween the two parenthesis we would declare some argument to be used by the proc if we have any. The text between the two curly braces is what is going to be executed when we run the script.
Argument & variables
So know that we know how to execute a script lets make the script a little more flexible. To do this we will introduce the use of arguments. An argument is a way to pass information to be used by the script. It makes the script more reusable and flexible. Replace the text in your script file (jb_testing_mel.mel) with the text below and save it.
global proc jb_testing_mel(string $text)
{
print ($text+”\n”);
}
Source the script again. Maya will give you a warning : // Warning: New procedure definition for “jb_testing_mel” has a different argument list and/or return type. You get this because you´ve added the arguments to the procedure. Now type the text: jb_testing_mel “Just an Example” ; ” in the command line and hit enter. This will output Just an Example. This is how you use an argument to pass in information to be used by your script.
The argument in this case is a variable of the data type string. That means it is a text variable. When you declare a variable you start by declaring the type of the variable. Before you give the variable a name you must use the $ sign. So to declare a string variable named text you would write: string $text. The variable types that you can use in MEL is as follows:
- string $aString = “a string variable”;
- string $aStringArray = {“a”,”string”,”array”,”variable”};
- float $aFloat = 3.14;
- float $aFloatArray = {0.1,0.2,0.3}
- int $anInteger = 1;
- int $anIntegerArray = {1,2,3};
- vector $aVector = <<0,1,0>>;
- matrix $aMatrix[3][4] = <<1,2,3,4; 5,6,7,8; 9,10,11,12>>;
Return Value
A return value is a value that is returned by the procedure. For instance if you have a procedure that measure the distance between two points and you want to be able to use the distance calculated by one procedure in another procedure you would return the value and then use it. In our example we will return the name of the created sphere.
global proc string jb_testing_mel(string $text)
{
string $sphere[] = `sphere -n $text`;
return $sphere[0];
}
If we source the proc above and enter the text jb_testing_mel “jb_Sphere”; in the script editor we will see // Result: jb_Sphere in the command line. We just created a sphere, given it a name by our argument and returned the name of the created object.