As you work with expressions, you’ll sometimes see attribute values you didn’t expect. The following topics describe a few common causes of confusion.
Always examine the Script Editor for error messages after you edit an expression and click the Create button. If you alter a previously successful expression and a syntax error occurs, Maya executes the previous successful expression when you play the animation. This might lead you to believe your editing changes took effect.
When you rewind a scene, an expression executes with the last settings made for attribute values. This sometimes gives unexpected results.
Ball.tx = $distance; $distance = time;
Assume for this example you’ve set the starting frame of the animation to frame 0.
The first statement sets Ball.tx to the variable $distance. The second statement sets $distance to the value of time.
When you play the animation, Ball moves along the X-axis with the increase in time. Ball’s X-axis position is 4 grid units, for example, when animation time equals 4 seconds.
When you rewind the animation, Ball’s position along the X-axis doesn’t return to 0 as you might assume. The previous execution of the expression at time equals 4 set the $distance variable to 4. So rewinding sets Ball.tx to 4, then sets the value of $distance to 0, the value of time upon rewinding.
If you rewind again, Ball’s position along the X-axis returns to 0 as desired. Because the previous execution of the expression upon rewinding set the $distance to 0, the expression now correctly sets Ball.tx to 0.
To fix this problem, reverse the order of the statements and compile the expression:
$distance = time; Ball.tx = $distance;
After you play and rewind the expression, the first statement executes and assigns the time to $distance. The next statement assigns Ball.tx the value of $distance, which the first statement set to the value of time. Because $distance is set to 0 as the first statement after rewinding, Ball returns to the desired translateX position.
If you increment an attribute or variable during animation, you might be confused by its behavior.
Ball.ty = 0; Ball.ty = Ball.ty + 1;
Ball’s translateY position stays at 1 unit along the Y-axis. Ball’s translateY position doesn’t increase by 1 each frame as the animation plays.
Ball.ty = Ball.ty + 1;
Ball’s translateY position increases by 1 each frame as you play the animation. When you rewind the animation, translateY increases by 1 again.
When you play the animation again, the translateY position increases by 1 each frame. If you rewind the animation or drag the current time indicator, the translateY position continues to move up the Y-axis. The attribute never returns to its original position.
To return Ball to a starting position each time you rewind, you must initialize the attribute to a starting value. For example, you could use the following expression:
Ball.ty = Ball.ty + 1; if (frame == 1) Ball.translateY = 0;
This returns Ball to a Y position of 0 when you rewind to frame 1. When you drag the current time indicator, though, Ball doesn’t return to its Y position of 0.
The if statement resets the value of translateY to 0 only when frame 1 plays. Frame 1 is the default frame that plays when you rewind an animation. You would need to use a different frame number in the if statement if you’ve set your animation to start at a different frame.