Deborah R. Fowler

Updated on March 22  2013

back to MEL Resources



Very Brief Overview of Syntax of MEL

Disclaimer: This page is to get you started and is by no means complete or extensive. It's intention is to compare the concepts learned in VSFX 705 in python as they would relate to MEL.

Where do I run MEL?

Single command:
At the bottom of the display you see a tiny window labeled MEL. If you don't see it, select:
Display->UI Elements->Command Line


Mel Script:


Windows-> General Editors -> Script editor Window

or click the icon in the far lower right corner
Be sure to look at the script editor - this is where it will give you error messages when debugging even single commands



Variables

Much of the syntax you will see is like C, with assignment being = as well as the usual shortcuts (+=, -+, /= ++,--)
MEL is strongly typed (just like C, unlike Python). Here are two facts to make your life easier:

V
ariable names start with a $ in MEL
== means equals and = means assignment

Variable types are int, float, string, vector (unlike C's vector - this is a triple of floats), arrays (list with all elements the same type), matrices (2D table of floats).

So for example:
int $a = 5;
float $ar[] = {1.2, 3.4, 4.5};  // An array of floats
matrix $mLx[3][2];


Array of arrays are not allowed in MEL. (They are in C).



Truth Statements

if statements

if ($a == $b)
{
}
else
{
}

switch statements
switch ($a)
{
    case 1:
                break;
    case 2:
                break;
    default:
                break;
}



Looping

while ($a < 3)
{
}

for ($i = 10; $i  < 0; $i++)
{
    print($i+"...\n");
}

do
{
} while ($a < 3);



Functions

Also called procedures (the distinction is that a function returns a specific value and procedure acts on data).
// global proc return type name ( parameter list )
global proc float squareAndAdd(float $x, float $y)
{
    return $x * $x + $y;
}

If you don't use the word global, the procedure is available only int he script file it is defined.
If you leave out the return statement, leave out the return type keyword as well.



Comments

Same as C++ with // for a single line and /* */ for multi lines



MELisms

Every statement in MEL, must end with a semi-colon even at the end of a block (not so in C)
In MEL, you often use the seam command to create, edit and query and it is distinguished by a flag
ie.
sphere -radius 5 -name "Kermit";
sphere -edit -radius "Kermit";

Very useful are setAttr and getAttr and can be written using function or command syntax
setAttr("Kermit.translateX",10);     // Written with function syntax
setAttr Kermit.translateX 10;        // Written with command syntax

$a = getAttr("Kermit.translate.X")    ;// function syntax
$b = `getAttr Kermit.translateY`;     // command syntax