Monday 28 January 2013

Using a Layer’s Index


Using a Layer’s Index

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 fi rst 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.
What’s going on here? In the fi rst expression, the fi rst line
defi nes 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:
time – (index-1)*delay
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 zero for the fi rst layer.
So, for Layer 1, the total delay is zero, 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.
The Rotation expression is very similar except that it
doesn’t reference time. The reason for this is that the fi rst
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 fi rst line of
the expression defi nes a variable named offsetAngle. This
variable defi nes 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.


No comments:

Post a Comment