Introduction to MEL Scripting

A basic tutorial in using the Maya Embedded Language, MEL for creating objects.

This introduction to the Maya Embeded Language (MEL) introduces some simple MEL commands as well as the basic programming concepts of variables and loops. Variables are abstract signifiers that can be used to represent numbers, words (strings), decimals or just about anything else. The power of variables is that they are just that - variable. By using variables you are able to treat general conditions, instead of the specific. For example you can write a program that will create a building of any height, instead a specific height like 10 feet. Using variables means that you can use your MEL scripts in variety of different situations,


Make the number of floors variable

Loops are what makes programs and scripts really powerful, they

are used to repeat an number of functions (like making spheres and cubes) an indefinite number of times. This is what computers are good at, repetitive tasks. Instead of modeling 10,000 spheres, you can write a script with a loop that makes 10,000 spheres. But enough concepts, they are hard to understand without concrete examples. This tutorial walks you through a simple example of using MEL to make some objects, use some variables an make a loop. The end product will be a MEL script that can make a 'skyscraper' with multiple floors, the script will be able to make skyscapers of variable heights.


--------------------------------------------------------------------------------


Step 1


Create a Floor

The first step is to use MEL to make a 'floor'. A 'floor' in our case will be a simple Polygon Cube that is scaled to be a rectangular slab.

To make a cube, first open your Script Editor (click on the little icon in the bottom left of your Maya Window, or select Script Editor from Windows->General Editors->Script Editor in the Hot Box). In the bottom part of the Script Editor window type the following command:

polyCube;

After typing the command hit Control+Enter. This will execute the command and you should have a Polygon Cube now in your modeling window. Since you want to scale this to look like a slab, type the next command into the Script Editor and hit Control+Enter:

scale 20 .5 20;

This command scales the object that is currently selected 20 times in the X direction, 1/2 in the Y direction and 20 times in the Z direction. You should have a shape that looks somewhat like the one on the left.

If you delete your new object you can easily make it again by typing both of the commands into the Script Editor and then hitting Control+Enter. You can put as many commands as you want into the Script Editor at once. So to recap, if you type the following into the Script Editor it will make a cube an scale it all in one step:

//make a cube
polyCube;
//scale it wider and thinner
scale 20 .5 20;

The '//' command is a little extra, it is called a comment in programming languages. Comments are arbitrary notes that don't really do anything in the progam except act as reminders to you as to what it is you are doing. When you start writing larger scripts you will find you will need to put in lots of comments to remember what you are doing and not get lost. All comments begin with '//', and anything after the '//' until the end of the line is ignored by Maya when the code is executed.

Onto step 2...


--------------------------------------------------------------------------------


Create a loop to make multiple floors

Step 2

In step two we are going to take the basic commands from Step 1 and put them inside of a loop so that we can make multiple floors. The loop will repeat the polyCube command 10 times.

The loop starts by using a variable, variables in MEL all begin with a '$'. The flowing code creates a variable called '$i' and sets it equal to 0.

$i = 0;

Now, wherever we use '$i' it will be the same as if we are using the number '0'.

The next step in making the loop is where the magic is, it is the while command. It looks like this

//start a counter at 0
$i = 0;

while($i++ &t; 10){

//do stuff

}

It is the progamming way of saying "Repeat this while $i is less than ten, and while you are repeating do stuff". The '{' and '}" surround all of the MEL code that is going to get done each time there is a loop, also while it is going through the loop $i gets incremented by one each time the loop repeats. So in our case, we want it to make a cube each time it goes through the loop. So lets put it all together:


//start $i at 0
$i = 0;

//loop while $i is less than 10
while($i++ < 10){

//make a cube
polyCube;
//scale it wider and thinner
scale 20 .5 20;

}

If you type this into the Script Editor and run it you will probably be a little disappointed. It will make 10 cubes, but they will all be in the same location, at (0,0,0). To have them be spaced out like floors (as seen to the left), you must use the MEL move command:

move 1 1 1;


The move command is like the scale command but instead of scaling in x,y and z it moves the object in x,y and z. In our case we want each floor to only move up, in the Y direction, so we are going to use 0 for the X and 0 for the Z;

move 0 1 0;


But one other thing, we don't want all of the cubes to just move to the location (0,1,0), we want each one to move up more than the last. We want the first cube at (0,0,0), the second at (0,1,0), the third and (0,2,0), the fourth at (0,3,0) and os on. So each time it goes through the loop we want it to move the cube more that the previous, luckily we have a variable that is increasing each time we go through the loop, that variable is $i. So all we need to add is this:

move 0 $i 0;


That is what is great about variables, you can use them inside of MEL commands. With the 'move 0 $i 0' each time in the loop it $i will be more than the previous and the cube will get moved more. Here is the whole thing, type it into the Script Editor and hit Control+Enter:


//start $i at 0
$i = 0;

//loop while $i is less than 10
while($i++ < 10){

//make a cube
polyCube;
//scale it wider and thinner
scale 20 .5 20;
//move it
move 0 $i 0;

}


If you get any 'Error' warnings, make sure everything is typed exactly as you see it above. The MEL scripting language is case-sensitive, meaning the word 'MOVE' is different that 'Move' and 'move', so be careful.

Onto step 3...


--------------------------------------------------------------------------------


Make the number of floors variable

Step 3

Step 3 is easy after the last step, in this step all we are going to do is to use a variable to make the number of floors the script makes easily changable.

To make the number of a floors a variable first make a variable and give it a simple name you can remember, I chose '$number_of_floors'. Variables must start with a '$' and also cannot have any spaces in their name. Set that variable equal to 20 like so:

$number_of_floors = 20;

Now go the code from the last step and change one thing, the while statement. The while statement was looping while $i was less than 10, now we want to make 10 a variable, so replace the number 10 with $number_of_floors. You script should now look like this:


//make the number of floors variable. Change this to make more or less floors
$number_of_floors = 20;

//start a counter at 0
$i = 0;

//loop while $i is less than number of floors
while($i++ < $number_of_floors){

//make a cube
polyCube;
//scale it wider and thinner
scale 20 .5 20;
//move it
move 0 $i 0;


}

Since you are going to be constantly editing your scripts and testing them out in the Script Editor, I recommend using a text editor like 'Notepad' or 'BBEdit' (Mac) to write and edit your scripts, then you can just cut-and-paste them into the Script Editor and execute them.


Onto the last step...


--------------------------------------------------------------------------------


Step 4

You have now worked a little with loops and variables, and should have some understanding of what they can offer. The last step is to alter our script a little by adding more variables so that we can have more control over the 'skyscraper' it creates. We are going to add a variable, and do a little math with it, so that we can have the spacing between each floor be variable.

So far the spacing between each floor has be 1 unit, because we are using the counter in he while loop ($i) to position the cube in the Y direction. Lets make a new variable called '$size' and use that to make the spacing between floors.

//make the height variable
$size = 4;

With the new variable '$size' we can go inside of our loop and make yet another new variable called '$height', this should be equal to our '$size' times '$i'.

$height = $i * $size;

Now if we use the variable '$height' instead of '$i' in the move command each floor will be spaced out 4 units. If we change the variable '$size' to 7 units and run the script again, all the spacing will 7 units. Here's what the entire script should look like:

//make the number of floors variable. Change this to make more or less floors
$number_of_floors = 20;
//make the height variable
$size = 4;

//start a counter at 0
$i = 0;

//loop while $i is less than number of floors
while($i < $number_of_floors){

//make a cube
polyCube;
//scale it wider and thinner
scale 20 .5 20;
//move it
$height = $i * $size;
move 0 $height 0;

//do not forget to increase the counter or it will get stuck!
$i++;

}

Try other variables and other transformations.

Congratulations, you know have some basic MEL programming knowledge. Now that you have the basic script and knowledge of variables you can start to play with making it do diffferent things. If you make some additional variables and use the 'rotate' command:

rotate $x $y $z;

You can start to get other behaviors like the one shown to the right.