Abilene Harper

View Original

Intro To Animation MEL Expressions

INTRO

Autodesk defines animation expressions as “Expressions are instructions you give Maya to control attributes over time […] they’re ideal for attributes that change incrementally, randomly, or rhythmically over time. Expressions are also useful for linking attributes between different objects - where a change in one attribute alters the behavior of the other […]. Expressions [also] offer an alternative to difficult keyframing tasks.”

ACCESS THE EXPRESSION EDITOR

  • Rigging menu set > Animation Editors > Expression Editor.

  • Go to the Select Filter menu and check the Expression Name option to make it easy to find your expressions.

  • Add the expression name to the Expression Name box and click the New Express button.

    • Leave Convert Units to All, Particle to Runtime before dynamics, Evaluation to Always, and Editor as Expression Editor.

  • Write an expression in the Expression section of the editor.

    • Use the $ to create a new variable. A variable is a way of storing information in a computer program. Think of a variable like a container, and the name of the variable is the label on the container.

    • Insert the terminator semicolon (;) to indicate that the variable statement is complete.

      • Example: $mult_value = 5;

  • In the next line, copy and paste the name of the object you want this variable to apply, from Channel Box, and add an attribute such as .tx for translateX.

    • Press the Create button.

      • Example: pSphere1.tx = $multi_value;

  • Open the Script Editor to confirm the result of the expression.

  • If there’s an error in the expression, the MEL line (bottom right of Maya’s screen) turns red.

Using Keywords

The words time and frame are special keywords that don’t require the $ prefix. We can use these keywords in animation expressions as if they were variables.

  • Time returns the current time position in the timeline.

  • Frame returns the current frame position in the timeline.

    • Example: pSphere1.translateY = frame / 2;

    • Example: pSphere1.rotateZ - time;

      • Scrub the timeline to see how each of these expressions affects the object.

WHEEL SPIN EXPRESSION

There are many ways to spin a wheel, but I’ll cover two here. Spin the wheel over time by frame or by translating its position. First, create a new object, create a control to move the object, then create a switch to toggle the spin between frame and translation.

Create a new cylinder.

  • Rigging menu set > Create > Polygon Primitives > Cylinder
  • Rename cylinder as wheel

Create a new controller.

  • Rigging menu set > Create > NURBS Primitives > Circle
  • Rename circle as ctrl

Create two switches - new attributes - inside the controller.

  • Select wheel controller > Channel Box > Edit > Add Attribute > name it frame_switch > Data Type is Boolean > Attribute Type is Scalar > Numeric Attribute Property is 0 for Minimum and 1 for Maximum > click Add
  • Select wheel controller > Channel Box > Edit > Add Attribute > name it wheel_speed > Data Type is Float > Attribute Type is Scalar > Numeric Attribute Property is 0 for Minimum and 1000 for Maximum > click Add

Make the wheel rotate over time by frame.

To see real-time results, adjust the playback speed to match the framerate in the Animation Settings.

  • Create a new expression.

  • Select the object and click on the attribute item to be changed. In this case, it’s the rotate Z.

  • With the attribute selected, go to Windows > Animation Editors > Expression Editor to create the expression
  • Under Objects, select wheel, and under Attributes, select rotateY
  • Under Expression Name add wheel_spin_exp, set Convert Units to All, and Evaluation as Always.
  • In the Expression area, write:

    if (ctrl.frame_switch > 0){
    wheel.rotateZ = -(frame) * ctrl.wheel_speed;
    }

    The wheel rotates when the switch is on by pressing play in the timeline.

Make the wheel rotate over time by translating the control.

Edit the expression and add the following after the curly brackets:

  • else {
       wheel.rotateZ = -(ctrl.translateX) * ctrl.wheel_speed;
       wheel.translateX = ctrl.translateX;
    }

To test them out, go to keyframe 0 and set a key, then move the object and controller to another frame and key it. Set the frame switch to On and press play to see the wheel moving and rotating at the same time.