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.

By default, an expression also 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. To stop this default evaluation, set the expression node Evaluation mode in the expression editor to On Demand.

An expression also executes when an attribute referenced directly in an expression changes value. 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.

pCube1.tx = pSphere1.tx * 3.0

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 About particle expressions.)

When writing an expression, you can access object attributes either directly or with MEL (see Note, below).

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.
Note: It is not recommended to use a MEL script to access an object's attributes. Using a MEL script creates difficulties as doing so obscures connections between nodes in the evaluation graph, which are required to function properly. For example, a status-related expression such as the size of a cache file or the current time of day will not evaluate automatically. As these events are not driven by animation, Maya won't evaluate them until you toggle the "animated" attribute. (Using MEL scripts works occasionally because, in a few cases, the expression is in the evaluation graph due to one or more of its input attributes being animated.) However, we recommend using the direct method, described here, for consistent results, instead of MEL Scripts.

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 forces evaluation of 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.