MEL Commands
 
borrowed from www.arch.columbia.edu
 
Duane Palyka
October, 2002
         
         
Set 1: Basics                                 
Set 2: Declaring Variables            
Set 3: Arithmetic                         
Set 4: Loops & Arrays                 
Set 5: Conditionals & Procedures
Set 6: Executing Commands       
 
 
 
MEL Commands - Set 1
     
   
.
//    
Description Comment : Everything after this on the line is not read by MEL
     
Example // THIS IS A LINE NOT READ BY MEL
     
Tips Use Plenty of comments in your scripts. You always feel that you will remember how your script is working. But when you return even a day later you will be suprised how hard it is to remember what you were doing.
.
print    
Description Prints text to the MEL disply window
     
Example

print "Hello, World!\n"
The \n creates a carraige return. You must have quotes around strings but not around variables.

$a = 5;
print "The number is " +$a;

This prints out: The number is 5

     
Tips Printing is essential for debugging. It is the best way to figure out the state of your variables
.
$    
Description Prefix for a variable. Used in conjunction with varible name as an identifier for a variable.
     
Example int $a
Declares a variable 'a' of the type integer.
$a = 5;
This assigns the value of 5 to a variable 'a';
print($a);
This will print the value of the variable 'a'
     
Tips Variable names cannot contain spaces or 'funny' characters (ie. $%^&*!@#).Variable names can contain numbers, however the number cannot be the first character of the variable name.

It is good procedure to declare variables before they are used the first time.
.
=    
Description Assigns a value to a variable.
     
Example int $a = 5;
Assigns a value of 5 to the variable 'a'

string $name = "Bobby";
Assigns the text string 'Bobby' to the variable 'name'
     
Tips When assigning string values to variables they must be surrounded in double-quotes ("). You can assign values to undeclared variables and they will be declared implicitly.


 
 
MEL Commands - Set 2
     
   
.
float    
Description Declares a variable as a floating point or decimal number
     
Example float $x;
     
Tips You cannot recast a variable as different type once it has been declared
.
int    
Description Declares a variable as an integer
     
Example int $x;
     
Tips You cannot recast a variable as different type once it has been declared
.
string    
Description Declares a variable as a string
     
Example
string $n = "Isaac";
string $txt = "My name is " + $n;
print $txt;
// Result: My name is Isaac
     
Tips You cannot recast a variable as different type once it has been declared
.
vector    
Description Stores three floating point values.
     
Example
sphere;
vector $p = << 10.0, 5.0, 2.5 >>;
move -absolute ($p.x) ($p.y) ($p.z);
     
Tips When using variables you must be conscious of their current state.


 
 
MEL Commands - Set 3
     
   
.
*    
Description Multiplies two numbers or variables
     
Example $x * 5;
5 times the value of $x
     
Tips
.
/    
Description Divides one number by another
     
Example $z = $x / $y;
Divides the value of $x by the value of $y and sets $z equal to the result
     
Tips When using division if you want your result to be a decimal it is important to make sure that the variable you are assigning the result to is of type float
If you attempt to divide by zero MEL will give an error
.
+    
Description Adds to variables
     
Example $x + $y;
     
Tips Not much needs to be said about this one
.
-    
Description Subtracts two variables
     
Example $z = $x - 5;
     
Tips
 
 
MEL Commands - Set 4
     
   
.
while    
Description A while loop executes a set of statements as long as a certain condition is true
     
Example $i = 0;
while ($i < 10) {
print($i + "-");
$i = $i + 1;
}


Result:
1-2-3-4-5-6-7-8-9-;

This will go through a loop that prints the value of $i and increments $i while $i is less than 10
     
Tips Make sure that whatever condition for the while statement will eventually be false, otherwise the loop will never end and Maya will crash
.
do    
Description Used in conjunction with while. A do-while loop executes a set of statements as long as a certain condition is true, but evaluates the truth of the condition only after the statement is executed
     
Example $i = 0;
do {
$i = $i + 2;
print("I am " + $i + " years old\n");
} while ($i != 6)
     
Tips
.
array[];    
Description Arrays are an alernative method of declaring variables. Arrays allow for multiple variables to be stored under a single name and indexed
     
Example $x[1] = 5;
$x[2] = 22;
$x[6] = 44;

An array titled $x has been declared. the 1st item in the array [1] is set to 5. The second item [2] is set equal to 22.
     
Tips
 
 
MEL Commands - Set 5
     
   
.
if(){
}
   
Description An if conditional statment. This command takes a conditional test as an argument (between the '(' and ')'), if the condition is true then the functions between the '{' and '}' will be executed.
     
Example if ($i == 10) {
sphere -p 1 1 1;
}

The example tests whether the value of the variable $i is equal to 1, if it is equal to one then a sphere will be created. If it does not equal one nothing will happen.
     
Tips Conditional tests use the following syntax:

== Equal to
!= Not equal to
> Greater than
< Less than
.
else{
}
   
Description Used in conjunction with if. An else statment can follow an if statment. If the condition for the if statment proves false, then the commands between the '{' and '}' following the else statement.
     
Example if ($i == 10) {
sphere -p 1 1 1;
}
else {
nurbsCube -p 1 1 1;
}

In this example the if statment tests whether $i equals 1, if it does it creates a sphere. If it is NOT equal to one then a cube is created.
     
Tips
.
elseif(){
}
   
Description Used in conjunction with if. The elseif statment allows for mutliple conditions to have different results all following one if statment. elseif takes a conditional statment as an argument and will execute the commands between '{' and '}' if it is true, however the elseif test is only run if the if statment that precedes it is false.
     
Example if ($i == 10) {
sphere -p 1 1 1;
}
elseif($i < 10) {
nurbsCube -p 1 1 1;
}
elseif($i > 10) {
cone -p 1 1 1;
}
The example tests to see if $i is equal to 10, if it is equal to ten it will create a sphere. If it is NOT equal to ten it, the first elseif statment will test to see if it is less than 10, if it is less than 10 a cube will be created. If the $i is NOT less than 10 and NOT equal to ten the final elseif statment will test if $i is greater than ten, if that is true then a cone will be created. If $i is not equal to, greater than or less than 10 (such is the case if $i is not a number) then nothing will be created.
     
Tips
.
proc(){
}
   
Description Creates a procedure.
The proc command uses the following syntax:
global proc (arguments){

commands to be executed
}


The declaration of global is optional, this will declare the procedure as a global procedure, leaving out 'global' will create a temporary(local) procedure. The arguments are the values that will be passed to the procedure for it to use. The syntax for declaring the arguments is type variable name. In between the '{' and '}' any mel commands or other procedures can be called.
     
Example global proc sphere2(int $scale){
    sphere;
    scale -r $scale $scale $scale;
}


The example will create a procedure called sphere2 this procedure will take a single integer argument, that of scale. The procedure will create a sphere and scale it $scale times in the x,y and z directions. Once the procedure has been created it can be called using the following syntax sphere2(45); This would create a sphere it to 45.
     
Tips Procedures have a concept of variable space, variables created within a procedure will not go outside of a procedure. This means if you have a variable called $i that you create in a procedure, it will exist only within the procedure and cannot be accessed by other procedures. If you are using the variable $i outside of the procedure it will not affect the value of $i inside the procedure. The exception to this is if a variable is declared as global
 
 
 
MEL Commands - Set 6
     
   
.
getAttr    
Description Returns the value of an objects named attribute. This is used to query object so that function can be performed upon them.
     
Example getAttr nurbsSphere1.controlPoints

This will return a count of the number of control points in an object.
     
Tips To learn the names of specific attributes of an object check the maya online help system under MEL commands
.
eval    
Description The eval commands executes a command or procedure contained in a variable string
     
Example $cmd = "sphere -p " + $xpos + " 0 0 ";
eval($cmd);

This will create a sphere with an x position determined by a variable
     
Tips
.
`ls -sl`    
Description List the objects currently selected
     
Example $list = `ls -sl`

This will list the selected objects and put that list into a variable $list.
     
Tips