MEL for programmers
 
 
 

As a language, MEL is descended from UNIX shell scripting. This means MEL is strongly based on executing commands to accomplish things (like executing commands in a UNIX shell), rather than manipulating data structures, calling functions, or using object oriented methods as in other languages.

Most commands you use to control Maya act like UNIX command-line utilities: little stand-alone programs with many options that modify their behavior.

Keeping the shell scripting origins of MEL in mind will help you understand some of its quirkier aspects.

Quick overview

Assignment and values

The assignment operator in MEL is the equal sign (=). MEL also has shortcut assignment operators like C and Java (+=, -=, /=, *=, ++, --, etc.).

MEL is a strongly typed language, however it allows implicit declaration and typing in most instances. When you declare a variable you also declare its type and can optionally assign an initial value.

Variable names start with a $, followed by a letter, then any combination of letters, numbers, and underscores. Unlike PERL, all types of variables (scalar and compound) start with $.

MEL has the usual integer (int), floating point (float) and string data types. It also has a vector data type which is a triple of floats (which can be useful when working with 3D data), arrays (a variable-sized list, in which all elements are of the same type), and matrices (matrix, a fixed-size two dimensional table of floats). Items in an array must all be of the same type.

int $a = 5;
float $b = 3.456;
vector $v = <<1.2, 3.4, 6.5>>;
float $ar[] = {1.2, 3.4, 4.5}; // An array of floats
matrix $mtx[3][2]; // A 3x2 matrix of floats

You cannot make an array of arrays in MEL.

MEL automatically converts types whenever possible.

Control and looping statements and operators

MEL’s control statements are very similar to C and Java.

if ( $a == $b) {
	...
} else if ($a > $b) {
	...
} else {
	...
}
$a = ($b > 10) ? $c : ($c - 10);
switch ($color) {
 case "blue":
 ...
 break;
 case $c1:
 ...
 break;
 default:
 ...
 break;
}
while ($a < size($arry)) {
	...
}
do {
	 ...
} while ($a > 0);
int $i;
for ($i = 10; $i > 0; $i--) {
	print($i+"...\n");
}
print("Blastoff!!!");
string $arry[3] = {"red","green","blue"};
for ($k in $arry) {
	...
}

Defining and calling procedures

You create user-defined procedures using the following syntax:

global proc <return type> <name>(<arg list>) {
	...
	return <exp>;
}
global proc float squareAndAdd(float $x, float $y) {
	return $x * $x + $y;
}
square(5.0, 2.0);
27

If you leave out the global keyword the procedure is only available in the script file in which it is defined.

If the procedure does not return a value, leave out the return type keyword and do not include a return statement.

global proc msg() {
	print("Hello world\n");
}

Comments

MEL uses C++ style single-line comments preceded by // and freeform comments surrounded by /* and */.

MELisms

There are some aspects of MEL programming that will trip up experienced programmers as well as beginners.

Every statement in MEL must end with a semi-colon (;).

if ($a > $b) {print("Hello");};
// Both semicolons are required!

Unlike some scripting languages/environments (but like the Logo language), stating an expression that returns a value does not automatically print the value in MEL. Instead it causes an error.

3 + 5;
// Error: 3 + 5; //
// Error: Syntax error //
print(3+5);
8

In MEL, you often use the same command to create things, edit existing things, and query information about existing things. In each case, a flag controls what (create, edit, or query) the command does.

// Create a sphere named "mySphere" with radius 5
sphere -radius 5 -name "mySphere";
// Edit the radius of mySphere
sphere -edit -radius 3 "mySphere";
// Print the radius of mySphere
sphere -query -radius

MEL allows you to type commands in command syntax (similar to UNIX shell commands) and function syntax. In command syntax you can leave off quotation marks around single-word strings and separate arguments with spaces instead of commas.

setAttr("mySphere1.translateX",10); // Function syntax
setAttr mySphere1.translateX 10; // Command syntax

Function syntax automatically returns a value. To get a return value using command syntax, you must enclose the command in backquotes.

$a = getAttr("mySphere.translateX"); // Function syntax
$b = `getAttr mySphere.translateY`; // Command syntax