How often an expression executes
 
 
 

After you’ve typed an expression in the Expression Editor, you click the Create or Edit button to compile the expression. Compiling the expression checks it for syntax errors and converts it to a form Maya can execute when you rewind or play the animation. After being compiled, the expression executes for the current frame.

An expression executes whenever the current animation time or frame changes. For example, an expression executes when you rewind or play the animation. The expression executes once for each time the animation frame or time changes.

An expression also executes when Maya makes use of an attribute in an expression. For example, if your expression assigns a sphere’s translateX attribute to another attribute and you move the sphere in an X-axis direction, the expression executes upon each increment of the sphere’s movement. This is only true if you reference the sphere’s attributes directly.

Evaluation

When you select an object other than a particle shape node, you can use the Evaluation drop-down menu in the Expression Editor to set when an expression executes. (If you select a particle shape node, the Expression Editor dims this menu. For details on particle shape node expressions, see Particle expressions.)

When writing an expression, you can access object attributes either directly or with MEL.

Directly:

pCube1.tx = pCube2.ty * 3.0

MEL:

setAttr pCube1.tx (`getAttr pCube2.ty`* 3.0)

When an object’s attributes are accessed directly, Maya makes connections between nodes for those attributes. This means that Maya’s directed graph evaluation process automatically recognizes a change in the value of pCube2.ty and marks pCube1.tx as dirty (in need of recalculation) regardless of whether or not the Evaluation option is set to Always.

When an object’s attributes are accessed via a MEL script, Maya does not make connections for the relevant attributes of the nodes, so the directed graph evaluation process is bypassed. In order to ensure that the value of pCube1.tx is consistently up to date, the Evaluation option must be set to Always so that the evaluation can be triggered when necessary.

Occasionally, it’s useful to set Evaluation to On demand in order to diminish redundant expression execution and speed Maya operation. Before doing this, it’s best to understand the details of expression execution.

When Evaluation is set to On demand, the expression calculates only when the input connection of your expression changes (in the example above, when the value of pCube2.ty changes), or when you click Edit in the Expression Editor. When Evaluation is set to Always, every frame change triggers an evaluation even if nothing in the scene has changed.

Note

Setting Evaluation to Always only evaluates the expression when time changes. Thus, using a solution that includes a MEL expression and Evaluation set to Always does not evaluate when manipulating the objects in the scene view unless it is during Interactive Playback.