MEL includes a wide variety of commands for all aspects of using Maya. Some typical examples of using MEL commands include quickly creating objects, precisely moving objects, and working more efficiently with objects.
For example, you can use a MEL command to create a sphere named bigBoy with a radius of exactly 27.5 units:
sphere -radius 27.5 -name "bigBoy";
You can then enter this MEL command to rotate bigBoy 90 degrees around the Z-axis:
rotate -relative 0 0 90 "bigBoy";
As another example, if you are creating a joint with the joint tool and you want to move the joint 5 units in an X-axis direction, you can execute the following MEL command without having to interrupt the joint creation:
move -relative 5 0 0;
By convention, most commands operate on an object if you specify its name, otherwise they operate on the current selection.
You can use MEL commands in two ways: imperative syntax and function syntax.
The imperative command syntax looks like a command in a UNIX or DOS shell, with optional flags and arguments after the command name:
sphere -name "martha" -radius 10;
The imperative style is a complete statement and should end with a semicolon. If you want to use a command’s imperative syntax as part of an expression, you need to backquote the command. See Using return values: function syntax and backquotes below.
When you use this imperative command syntax (as opposed to the function syntax explained below), you can optionally leave off quotation marks around single-word strings. So you could write the sphere command like this:
sphere -name martha -radius 10;
You will often see this in scripts, especially for strings such as node and attribute names which are always one word. However, as a beginner, you may want to avoid using this feature because it makes strings less distinct from keywords and commands.
Function syntax looks like a standard function call in a computer language.
Imperative syntax | Function syntax |
---|---|
attributeExists visibility mySphere; |
attributeExists("visibility","mySphere"); |
abs -50; |
abs(-50); |
Flags modify how a command works. A flag comes after the command name, is proceeded by a dash (-), and is followed by a parameter.
sphere -radius 5;
Many commands have different behavior based on a pair of special flags: -edit and -query.
sphere -name "george";
sphere -edit -radius 10 "george";
sphere -query -radius "george";
// Result: 10 //
The Maya Help for each command lists which flags are available in create, edit, and query modes.
Using return values: function syntax and backquotes
When you use the function syntax of a command, the command returns a value. When you use the imperative syntax, the command simply prints its return value to the Script Editor, it does not provide a usable return value. Using imperative syntax in an expression will cause a syntax error:
if (size($word)) print("Not empty.\n");
// Function syntax of size returns a value.
// This is OK.
if (size $word) print("Not empty.\n");
// Can't use imperative
// This is a syntax error.
To use imperative command syntax in an expression you must surround the command with backquotes:
if (`size $word`) print("Not empty.\n");
You will use backquotes often to use the return value of a command in query mode inside an expression:
if (`sphere -query -radius "mySphere"` == 5)
print("This sphere has a radius of 5!";