Using runtime expressions

 
 
 

There is one other type of expression you can create for a particle object (shape node)—a runtime expression. By default, a runtime expression executes each frame during playback. A runtime expression does not execute when you go to the start time (or at the time in which a particle is emitted).

You’ll often create both types of expressions for a particle object—a creation expression that initializes an attribute value in the first frame, and a runtime expression that controls the attribute value in subsequent frames.

The following steps show how to create a runtime expression to change particle color during playback.

To create a runtime expression

  1. In the Per Particle (Array) Attributes section of the Attribute Editor, right-click the RGB PP box and select Runtime Before Dynamics Expression from the pop-up menu.
  2. Enter this runtime expression:
    BubblesShape.rgbPP = sphrand(1);
    
  3. Click Create to compile the expression.
  4. Play the animation.

    The particles flicker in random colors as the animation plays back. The runtime expression controls the RGB PP attribute during playback. Because RGB PP is a per particle attribute, the runtime expression executes for each particle in the object for each frame. For each particle, the expression assigns the RGB PP attribute the output from the execution of the sphrand function with an argument of 1. The sphrand function is one of Maya’s many built-in mathematical functions that are useful in expressions.

    The sphrand function with an argument of 1 assigns each particle’s RGB PP color a random vector. The vector represents a random point in a spherical region of radius 1. The left, middle, and right RGB PP color components get a value no less than -1 and no greater than 1. (R, G, and B values less than 0 are treated as 0—a black color).

    The sphrand function returns a different random vector each execution, so each particle receives a different random RGB PP value, and therefore, a different color. The color changes each frame.

  5. Rewind and play the animation again.

    The particles become red when you go to the start time, and random colors during playback.

    Note

    You can use only one creation and one runtime expression per particle object (shape node) in your scene. To control multiple attributes of a single particle object, you must do so within one creation expression and one runtime expression. You can’t create a separate expression for each attribute as you can for other types of objects. You therefore don’t need to select an attribute from the Expression Editor’s Attributes list.