Mel - Maya Embedded Language


Examples of Expressions

Animating geometry via Expressions:
Open the Script Editor and type in this code:
int $x;
for ($x = 0; $x < 15; $x++)
     polyCube -n ("Cube" + $x);
     move -x $x;
Examine this code for a minute and figure out how it works
You should see 15 cubes in the Persp window all lined up next to each other

Open the Script Editor and type in this code:

int $i;
for ($i = 0; $i < 15; $i++)
     print ("Cube" + $i + ".sy = sin((frame + " + $i + ")/5); \n");
Notice what this code is doing: It is generating a tedious expression for us, so we don't have to do the typing dirtywork.

Copy the lines in the History panel that this code just generated, they should look like this: = sin((frame + 0)/5); = sin((frame + 1)/5); = sin((frame + 2)/5); = sin((frame + 3)/5); = sin((frame + 4)/5); = sin((frame + 5)/5); = sin((frame + 6)/5); = sin((frame + 7)/5); = sin((frame + 8)/5); = sin((frame + 9)/5); = sin((frame + 10)/5); = sin((frame + 11)/5); = sin((frame + 12)/5); = sin((frame + 13)/5); = sin((frame + 14)/5);

Now paste this code into the Expression editor

Click Edit and then click Play on time slider

Controlling and animating material attributes via MEL Expressions:
  • Create a nurbs sphere and name it myBall
  • Open the Hypershade window, drag a new material (blinn) and a ramp node into the work area.
  • Open the attribute editor for the ramp node. Under the Type, choose "U Ramp".
  • Change the colors to whatever pleases you, but notice what commands are being shown in the Script Editor, you will use these.
  • Next connect the ramp node to the material node in the hypershade window, and assign the blinn1 material to your sphere.
  • In the Maya perspective window hit "6" on the keyboard to switch to texture view. You should notice your myBall object is shaded with your ramp shader.
  • Now for the expression:

  • Open the Expression window, copy and type the code below and hit Edit.
  • // declares 3 variables for the R, G, and B values of the Ramp color
    float $color1 = rand(.5,1);
    float $color2 = rand(0,1);
    float $color3 = `clamp 0 1 (sin(time))`;
    // Assigns the three R, G and B values to the various colorEntryLists
    ramp1.colorEntryList[0].colorR = $color1;
    ramp1.colorEntryList[0].colorG = $color2;
    ramp1.colorEntryList[0].colorB = $color3;
    ramp1.colorEntryList[1].colorR = $color1 * -1;
    ramp1.colorEntryList[1].colorG = $color2 * -1;
    ramp1.colorEntryList[1].colorB = $color3 * -1;
    Click through your timeline to see the results. Due to limitations in Maya, in order to see the results you made animate, you need to batch render. Playback and playblast will not show your work.
    Animating geometry based on Math functions and a Control locator:
  • Create a new scene in Maya
  • Create a sphere
  • Name the sphere "mySphere"
  • Create a locator and name it "control"
  • Open the Channel Control editor under Window:General Editors:Channel Control
  • Highlight all the attribues in the left window and click Move >>
  • With the locator still selected go to Modify > Add Attribute
  • Under Attribute Name type in xControl and click Add
  • Again under Attribute Name type in ZControl and click Add
  • Now you have two custom attributes on the control locator which can be keyframed, we will now add an expression to animate the mySphere object based on these keyframes.

  • Open the expression editor and type in this code:
  • mySphere.translateX = (sin(time) * control.xControl);
    mySphere.translateZ = (cos(time) * control.zControl);

    Experiment with different values of xControl and zControl. Note that when both set at 1 the ball will move in a circle. Also, experiment with keyframing these values and see what kind of movement you can get.