A layer’s index attribute can be
used as a simple but powerful tool that allows you to create expressions
that behave differently depending on where the layer is situated in the
layer stack. The index attribute corresponds exactly to the number assigned to the layer in the Timeline window. So, the index for the layer at the top of the stack is 1, and so on.
Time Delay Based on Layer Index
Suppose you keyframed an animation for one layer. Now
you want to create a bunch of identical layers, but you want their
animations to be delayed by an amount that increases as you move down
the layer stack. You also want to rotate each
copy by an amount proportional to its position in the layer stack. To
do so, you first apply an expression like this to the top layer’s
animated properties:
delay = 0.15;
valueAtTime(time - (index-1)*delay)
Then you apply an expression like this to the Rotation property:
offsetAngle = 3;
value +(index-1)*offsetAngle
Finally, duplicate the layer a bunch of times. The
animation of each layer will lag behind the layer above it by 0.15
seconds and the rotation of each layer will be 3 degrees more than the
layer above (Figure 1).
What’s going on here? In the first expression, the first line defines a JavaScript variable named delay
and sets its value to 0.15 seconds. The second line is where all the
action is, and it’s packed with new things. For example, notice the use
of time. It represents the current composition time, in seconds. In other words, time represents the time at which the expression is currently being evaluated.
You use valueAtTime() to access a property’s
pre-expression value at some time other than the current comp time (to
access the pre-expression value at the current comp time, use value() instead, as in the Rotation expression). The parameter passed to valueAtTime() determines that time:
Subtracting 1 from the layer’s index and multiplying that result by the value of the delay variable (0.15) gives the total delay (in seconds) for this layer. Subtracting 1 from index
means that the delay will be 0 for the first layer. So, for Layer 1,
the total delay is 0, for Layer 2 it is 0.15, for Layer 3 it is 0.30,
and so on. You then subtract the total delay from the current comp time.
The result of this is that Layer 1’s animation runs as normal (not
delayed). Layer 2’s animation lags behind Layer 1 by 0.15 seconds, and
so on.
Tip
Remember, if you don’t specify a
comp and layer when referencing a property or attribute, After Effects
assumes you mean the layer with the expression. When you reference an
attribute of the property housing the expression, After Effects makes a
similar assumption, allowing you to specify only the attribute name
(without the entire comp/layer/property path). One side benefit of not
having to specify the entire path is that you can apply the same
expression to any property, without having to modify it at all. |
The Rotation expression is very similar except that it doesn’t reference time.
The reason for this is that the first expression is used to offset a
keyframed animation in time, while the second expression simply creates a
static (not animated) offset for the Rotation property. The first line
of the expression defines a variable named offsetAngle. This
variable defines the rotation amount (in degrees) by which each layer
will be offset from the layer above it. The second line tells After
Effects to calculate the layer’s offset and add it to the pre-expression
value of the property.
You’ll see other ways to use index in later examples.