Monday 28 January 2013

Using Markers


Using Markers

The expression language gives you access to the attributes
of layer (and composition) markers. This can be extremely
useful for synchronizing or easily establishing timing relationships
between animated events.
The marker attributes that appear most frequently in
expressions are time and index. As you might guess, the
time attribute represents the time (in seconds) where the
marker is located on the Timeline. The index attribute represents
the marker’s order on the Timeline, where 1 represents
the left-most marker. You can also retrieve the marker
nearest to a time that you specify by using nearestKey().
For example, to access the layer marker nearest to the current
comp time use
marker.nearestKey(time)
This can be handy, but more often you’ll want to know
the most recent previous marker. The code necessary to
retrieve it looks like this



n = 0;
if (marker.numKeys > 0){
n = marker.nearestKey(time).index;
if (marker.key(n).time > time){
n--;
}
}
Note that this piece of code by itself is not very useful.
When you do use it, you’ll always combine it with additional
code that makes it suitable for the particular property
to which the expression will be applied. Because it’s so
versatile, and can show up in expressions for virtually any
property, it’s worth looking at in detail.
The fi rst line creates a variable, n, and sets its value to 0. If
the value is still 0 when the routine fi nishes, it means that
at the current time no marker was reached or that there
are no markers on this layer.
The next line, a JavaScript if statement, checks if the layer
has at least one marker. If there are no layer markers,
After Effects skips to the end of the routine with the variable
n still set to 0. You need to make this test because the
next line attempts to access the nearest marker with the
statement
n = marker.nearestKey(time).index;
If After Effects attempted to execute this statement and
there were no layer markers, it would generate an error
and the expression would be disabled. It’s best to defend
against these kinds of errors so that you can apply the
expression fi rst and add the markers later if you want to.
If there is at least one layer marker, the third line of the
expression sets n to the index of the nearest marker. Now
all you have to do is determine if the nearest marker
occurs before or after the current comp time with the
statement
if (marker.key(n).time > time){
n--;
}


This tells After Effects to decrement n by 1 if the nearest
marker occurs later than the current time.
The result of all this is that the variable n contains the
index of the most recent previous marker or 0 if no maker
has yet been reached.
So how can you use this little routine? Consider a simple
example.


No comments:

Post a Comment