Maya MEL Introduction – part 3

Maya MEL Introduction - part 3 MEL stands for Maya Embedded Language, it is a scripting language used in Maya, it is similar to Perl.

Previous parts of this MEL Introduction described you how to import a script, how to set-up the environment, viewports and other very important issues about scripting, how to move the cubes  using the variable matrix for the animation. This last part of MEL Introduction shows how to move the ball over the cubes in a random manner.

The Sphere- Conditional and Random Movement on previously Animated Objects

This part of the script creates yellow material for the sphere. Later this sphere is created. The sphere then moves in a random manner on animated chessboard-like object. This random movement is not unconditional- the sphere cannot travel “uphill”. At the end the animation is played.

Shiny Yellow Material:

renderCreateBarCB -asShader "surfaceShader" phong;
rename matShinyYellow;
setAttr "matShinyYellow.color" 10 10 0;

These lines create a phong material, rename the material to matShinyYellow and set the color of newly created material. Notice, that you can assign values greater than 1, this is not possible with the sliders in Color chooser.

Creation of the Sphere:

sphere -r .45 -ch on -o on -po 0 -ax 0 1 0 -nsp 4 ;
rename RSphere;
hyperShade -assign matShinyYellow;

Creates a sphere with radius .45 with number of spans (-nsp) 4.
It is renamed and the yellow material is assigned to the sphere.

The sphere

The sphere

Keyframing Translate Channels:

move 2 1.5 2;
setKeyframe -e -at translateX;
setKeyframe -e -at translateY;
setKeyframe -e -at translateZ;

Moves the sphere to its starting position. Remember we are back in the time 0, last pause dialog set the current time equal to 0.
Keyframes for selected object (the sphere) are set only for channels translateX, Y, Z, this works just as right clicking on the selected channel in the Attribute Editor or in the Channel Box.


int $nextX;
float $nextY;
int $nextZ;

Variables $nextX, $nextZ, $nextY contain the next position of the sphere- coordinates of the next cube, where the sphere will possibly move.

int $dirX;
int $dirZ;

The direction, in which the sphere will be moving, either +/- X or +/- Z.

int $currentX,$currentZ;
float $currentY;

Current position of the sphere.

int $time;

Current time.

string $nextSq,$currentSq;

Name of the current and the next cube where the sphere will try to move.

int $condition;

Condition variable.

Sphere- directions

Sphere- directions

Moving the Sphere:

for ( $time=2 ; $time<=200; $time+=2 ) {

In this loop we will make the newly created yellow sphere move on the top of our animated objects. The movement will be in a random direction. There will be a condition that the sphere cannot go “uphill”- it cannot climb to a spot that has higher “altitude”, than the current spheres’ spot.

    currentTime $time;

Time is set to the value of $time variable. It ranges from 2 to 200 and step here is 2, that means times 2,4,6… and so on are calculated. We have already set keyframes for $time 0.


When we want to get some information about an object in the scene, we are using command getAttr. In parenthesis there is a name of the object, followed by a decimal point and the name of the attribute. In this case variables $currentX and $currentZ will contain X and Z coordinate of the current location of RSphere.


This is start of another type of loop. Besides for-loop, there is another type of loop – it is  do … while loop. It starts with do (and of course braces {) and ends with } and while (condition). The loop is executed  while the condition is true. Because the condition is at the end, the loop is always executed at least once.


We start with the $condition being true, this will decide, whether to continue in loop, or exit the loop.

    do    {
        $dirX=floor(rand (-1, 1)+.5);
        $dirZ=floor(rand (-1, 1)+.5);
        if ($nextX>=0) if ($nextX<=9) if ($nextZ>=0) if ($nextZ<=9) $condition=false;
    while ($condition==true);

This is another do-while loop, it is inside the previous do-while loop. Similar to the two for-loops above, except here it is not set how many times the loop will be executed. Instead, there is a condition at the end that decides, whether to continue or exit loop. Watch out for possible endless loops- they must be avoided in scripts!
$dirX and $dirZ will generate random numbers, that will be either -1, 0 or 1. Direction will decide next possible movement of the sphere. $nextX and $nextZ will be next possible location (X, Z coordinates) of the sphere.
if decides, whether the next location is within the boundary of “chessboard”. In the case it is inside (all the ifs are true at once), the $condition is false; the loop is not executed any more. If the next location would be outside, $condition stays true and another attempt to find next possible location proceeds. This will go on until a suitable direction and thus next location within the chessboard is found.
while will either let the loop execute again, ($condition is true), or it will let the script continue (when $condition is false).


We must set $condition equal to true again, because we still are in the outer do-while loop, that in our script ends with the same condition, as the previous loop did.


These two lines will get us height (= y coordinate  of one of the point of the upper face) of the next cube, where will the sphere try to move.


These two lines will get us height of the current cube, using exactly the same way as the previous commands.

    if ($currentY>=$nextY) $condition=false;
while ($condition==true);

Command if decides, whether the sphere can roll down from the current cube to the next one. If current cube is higher then the next one, sphere can continue, $condition is set to false and the loop ends. Otherwise next direction is tried out. In case the sphere is “cornered” or “surrounded” and cannot go anywhere the loop is executed until both X and Z direction are equal to 0, meaning, that sphere does not move in this time anywhere and stays on the current cube.

$nextY+=1 ;

This will raise the value of $nextY (next spheres’ Y position) by 1. Simplified version of $nextY=$nextY+1.
It is done, because $nextY is the height of the cube, but we want the sphere to be above the cube.

//select NPlane;
//$nextY+=1.5 ;

A “hidden” feature, the NURBS plane is unhidden and the sphere will more or less travels on this wavy smooth surface. To include these lines in the run of the script, remove “//”.

Hidden animated NURBS plane

Hidden animated NURBS plane

move $nextX $nextY $nextZ RSphere;
The sphere is finally moved to its new position.

setKeyframe -e -at translateX;
setKeyframe -e -at translateY;
setKeyframe -e -at translateZ;

This creates keyframes for the three translate channels of the sphere.

setFocus modelPanel2;

Closing by } ends the for-loop, that goes from start time 2 to end time 200.
In our case perspective panel is selected.
And the final animation is played for you:-)

A few Words at the End.

Suggestion for Use:
As with every recipe we will come with suggested serving. The principle of this script can be used for visualization of other sources of data. For example transferring music, grayscale picture or topological data into 3D pixel-like scene. Or putting a piece of cloth on top of the animated structure can make nice effect. Or instead of separate polygonal cubes use single NURBS plane with many CVs and you will end up with a smooth surface (like the hidden NURBS plane).

End of the Way:
With MEL you can achieve the same things you would do with the “traditional” clicking. But you can get much more.
Little example at the end – how about a calculator in Maya? Well not exactly in Maya, but if you are familiar with Windows calculator, why don’t you map this command to your favorite key: system (“start calc”);

system command lets you run your system commands.
MEL gives you many possibilities to extend your work beyond the usual creations. You can explore and try it out for yourself. It is a very helpful option for your work. So you can do things that would not be possible without some programming.
In the future, there is now another new and promising alternative. Autodesk now introduces another possibility for use in Maya- it is Phyton programming language. This opens another new whole chapter for us.

Maya HELP for MAYA, Autodesk
Maya 6 The Killer Tips, New Riders- Chapter 9, “Getting under the hood”