Search
Powered by SquareSpace
Powered by Squarespace
« Flash & ActionScript 3.0 Workflow Recommendations | Main | Recommended Board Games »
Friday
May012009

Maya - MEL Cheat Sheet

This Cheat Sheet is not intended as a subsititute for learning programming concepts or the MEL language. Here you will find a condensed overview of MEL's core syntax and language structure, plus a few tips & tricks. For additional MEL resources see:

Language Elements

Data Types:

int         //whole number
float       //decimal number
string      //text
vector      //container for three ordered floats
matrix      //two dimensional array

Variables:

int $intVar = 56;
float $floatVar = 1.5;
string $stringVar = "Hello World!";
vector $vectorVar = <<0,1,0>>;
matrix $myMatrix[2][4] = <<1,2,3,4 ; 5,6,7,8>>;
int $intArray[] = {55, 66, 77};
float $floatArray[] = {1.0, 2.0, 3.0};
string $stringArray[] = {"foo", "bar", "snafu"};

Operators:

=   //assignment, int $x = 5;
+   //addition
-   //subtraction
*   //product (multiplication)
/   //quotient(division)
%   //modulus (remainder)
++  //increment by 1
--  //decrement by 1
+=  //assignment by addition
-=  //assignment by subtraction
*=  //assignment by multiplication
/=  //assignment by division
pow //power, pow $x 2 (raise $x to the 2nd power)
==  //equality, if ( $x == 5 ) {}
!=  //inequality, if ( $y != 3 ) {}
<   //less than, if ( $z < 1.0 ) {}
>   //greater than, if ( $x > 0 ) {}
<=  //greater than or equal to, if ( $y <= 8 ) {}
>=  //less than or equal to, if ( $z >= 4.5 ) {}
||  //logical or, if ( $x == 1 || y == $2 ) {}
&&  //logical and, if ( $x == 1 && y == $2 ) {}
!   //logical not, if ( $z ! 3.0 ) {}

Conditionals:

if (frame == 1) {
  print("Hello frame 1");
}

if (time > 4) {
  print("time is greater than 4 seconds");
} else {
  print("time is less than 4 seconds");
}

while ($a < 10) {
     print ($a + "\n");
     $a += 1;
}

for ($i=0; $i<10; $i++) {
  print($i);
}

for ($item in `ls -sl`) {
     print ($item + "\n");
}

Procedures & Functions:

proc runCommands () {      //define procedure
     print("Hello Procedure!");
}

runCommands();             //run procedure

proc int runProcedure (int $input) {
     int $added = $input + $input;
     return $added;
}

runProcedure(5);           //returns 10

global proc runCode () {   //accessible anywhere
     print("Hello Global Procedure!");
}

Tips and Tricks

Braces {} define a local scope:

{int $i = 45;};

Backticks `` return the output of a command:

float $f = `getAttr pCube1.scaleX`;

Pipes | permit access into namespace and transform hierarchies:

setAttr pCube1|pCube2.translateX 5;

Array access is base 0:

$floatArray[0]; //read or write first item in array

Load external script file:

source "awesomeScript.mel";

Rescan script directories for new files:

rehash;

Get list (array) of nodes in scene:

$allNodes = `ls`;           //all nodes
$selectedNodes = `ls -sl`;  //selected node

Expressions

Timeline Drivers:

frame //returns current frame
time //returns current time in seconds

Access Keyable Attributes:

pCube1.translateX = 5;  //read or write attribute
pCube1.scaleY = 2.4;
pCube1.rotateZ = 89;
pCube1.customAttr = 10.8;

Randomness:

rand(5.0);          //random float
rand(3.3, 15.2);
noise(time);        //random float from -1 to 1
sphrand(1.0);       //random vector
gauss(2.5);         //random float
seed(1);            //random functions repeat

Trig and Limits:

sin(time);                  //sine of arg
cos(time);                  //cosine of arg
abs(sin(time));             //absolute value of arg
min(0, sin(time));          //lesser of two numbers
max(0, sin(time));          //larger of two numbers
linstep(0, 1, sin(time));   //proportional value

GUI & Windowing

Basic Column Window:

window;                     //create window
columnLayout;               //define layout
button -label "button1";    //buttons
button -label "button2";
showWindow;                 // show the window

Basic Confirm Dialog:

{
  string $result = `confirmDialog
    -title "Decide"
    -message "You are the Decider!"
    -button "YES!"
    -button "Nope"
    -defaultButton "YES!"
    -cancelButton "Nope"
    -dismissString "Nope"`;

  if ($result == "YES!") 
  {
    print "You Now Own The WORLD\n";
  }
  else 
  {
    warning "Fail!\n";
  }
};

...