Execution
When you execute a script, it runs until all instructions have been executed in the order specified by the sequence and the control structures. As long as a script runs, there is nothing else you can do in Maya. If, because of a mistake, a script keeps running, you can only stop the execution by closing Maya.

Expressions
Expressions are tiny scripts that can be attached to an object attribute or to an event, and that will execute each and every time the value of this attribute is modified or this event occurs. Thus, expressions allow you to specify the behavior of an object while you can still manipulate this or other objects through the Maya interface or through a script.

For example, you can link an attribute to another attribute, such that the value of the first attribute is defined by the value of the second attribute. Unlike connections made through the Connection Editor, the values of both attributes don't need to be exactly the same, but can be related through an expression.

Given two objects named pCube1 and nurbsSphere1, you can relate their respective translateX attributes as follows:

  • Open the Expression Editor (choose Window → Animation Editors → Expression Editor)
  • Select the sphere such that it is listed under Objects
  • Select its translateX attribute under Attributes
  • Type the following expression under Expression and press the Create button
  • Move the cube over the X-axis and see the result
     
    nurbsSphere1.translateX = pCube1.translateX * 2;
     
    Animations
    Expressions are also executed in an animation, allowing you to alter the scene over time. Unlike a script, you can pause or stop an animation and move back and forward into time.
     
    You can let the value of the translateX attribute of the sphere be equal to the frame number as follows:
  • Retrieve the existing expression in the Expression Editor
  • Alter the expression as given below and press the Edit button
  • Start the animation and see the result

    nurbsSphere1.translateX = frame / 10;

    frame and time are special keywords in MEL that allow you to retrieve the current frame and time of an animation.

    A wave
    You may alter more than one attribute in a single expression, though the expression is always linked to a specific object and attribute.

    nurbsSphere1.tx = time * 2;
    nurbsSphere1.ty = sin(time) * 2;

    An irregular wave
    The function noise provides a random continuous distribution of values between -1 and 1

    nurbsSphere1.tx = time * 2;
    nurbsSphere1.ty = noise(time) * 2;

    Expression scripts
    Since an expression is a tiny script you can use anything you can use in scripts (including variables and control structures). The only difference is that you do not need to use getAttr and setAttr when reading and modifying attributes.