Basic MEL Scripting Tutorial

I find it easier to learn by example, so I’ll start off by going through a short script and describe the bits that make it up.

The following script aligns the selected object with the rotation angles of the ‘persp’ camera.

proc alignWithCam() {     // lists the selected to an array variable  string $selected[] = `ls -sl` ;    // get the rotation values of the persp camera  float $camRotX = `getAttr persp.rotateX` ;  float $camRotY = `getAttr persp.rotateY` ;  float $camRotZ = `getAttr persp.rotateZ` ;    // set selected to the same rotation as the persp camera  setAttr ($selected[0] + ".rotateX") $camRotX ;  setAttr ($selected[0] + ".rotateY") $camRotY ;  setAttr ($selected[0] + ".rotateZ") $camRotZ ;       alignWithCam() ; // calls the above procedure

Some General Rules

  • Mel is case sensitive, setAttr is a valid command whereas SetAttr will cause a syntax error. This applies to variables and procedure names.
  • Commands end with a semi-colon but this doesn’t include program flow commands like ‘If’ statements and Procedures.
  • Variable names start with a dollar sign $.
  • Comments start with //

Defining Procedures

Procedures are the building blocks of scripts. They break the code down into re-usable sections.

Procedures are defined using the proc keyword. Procedures are like variable in that if you want the return a value back the command that called it you need to specify it’s type. The name of the procedure has brackets for passing variables into it. The code inside a procedure is contained within the {} brackets. The global keyword is used before proc if you need to call the procedure from outside the script.

global proc float MyProcedure() {  ..Your code goes here..  return $Afloat ; // example of returning a value }

Defining Variables

Variable Types:
float – Holds a float value
string – Holds a string value
int – Holds and integer value
vector – Holds a vector (3 floats)
arrays – Hold strings, ints and floats

Variables are preceded by the ‘type’ and defined using the $ character. When you put [] on the end this makes it an array. Variables can also be assigned a value when they are initially defined as well as later on if required.

Executing MEL commands in a script

There are a few ways to execute a Mel command in a script, this may seem strange at first but each method has a different use.

Imperative syntax
This style is like a command in DOS and looks like this
setAttr ($selected[0] + ".rotateX") $camRotX ;

Function syntax
This style is like a call to a function in other program languages:
setAttr(($selected[0] + ".rotateX"),$camRotX) ;

Both do the same thing.

Enclose a command in these ‘backticks’ when you want to capture the result to a variable using Imperative style (not needed when using Function style):

$a = `setAttr $attrib "hello world"` ;

The other way is using the eval() command but this is more advanced and is only necessary for building commands on the fly. I won’t go into detail about it here.

The ‘ls’ command
‘ls’ is useful for gathering scene information to a variable. It lists names of scene nodes depending upon the flags you set.

The ‘getAttr’ command
Reads an attribute from a node into a suitable variable.

The ‘setAttr’ command
Sets an attribute of a node to a specified value.


Read the Mel commands list beforehand, this will give you an idea of what commands you have at your disposal even if you don’t know how to use them all yet. You’ll constantly be referring to the commands list while scripting.


When iterating through a list of components use the -fl flag to ‘flatten’ the ls output. This prevents grouping of concurrent elements e.g. object.f[2:4] appearing in the list and causing unexpected results.

In the next tutorial I’ll cover program flow statements.


One thought on “Basic MEL Scripting Tutorial

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s