Introduction
Have you ever been building an After Effects template, wanting to use expressions to automate your motion, but found it tricky to write your script accounting for the ever changing values your template may contain? For instance, a constantly changing minimum and maximum slider value. Or an array, or a .csv file with a distinctly different number of entries/rows.
One of the hardest things about building template projects is accounting for these mystery parameters. The more limited the template, the shorter its lifespan and functionality. So it's always in our best interests to make it as efficient as possible.
For this reason, I find myself using for
loops.
What Are for
Loops?
for
loops allow After Effects to run the same code a set amount of times, each time using a different value.
for (arg 1, arg 2, arg3){
//execution
};
The first argument runs before the loop begins, the second argument sets the condition when the loop should run, and the third argument runs every time after the loop is executed. For a more in depth analysis of the function itself, you can read more here where w3school breaks down the function.
Example Of for
Loop In Action
So, what can for
loops be used for in After Effects? They can be used effectively to cycle through array values, but on top of that they can be used to cycle through keyframes, and other such data we may want to access.
Take this example from my character lip sync project:
var audio = thisComp.layer("Audio Amplitude").effect("Both Channels")("Slider");
let maxV = 0;
let minV = 0;
for (let i = 1; i <= audio.numKeys; i++){
maxV = Math.max(maxV, audio.key(i).value);
minV = Math.min(minV, audio.key(i).value);
};
Here, I list my first argument establishing the variable i
, and set it to 1. My second argument, which defines when the loop runs, is set to i <= audio.numKeys
. This means the loop will only run when i
is less than or equal to the number of keyframes present on my audio slider. Finally, the third argument is set to i++
, adding 1 to i
at the end of every loop.
While the loop is active, it affects the value of my maxV
and minV
variables. As i
increases, the loop checks the value of each keyframe in turn, and adjusts the variables depending on whether or not the value is higher or lower than the current maxV
or minV
values. Then, once i
is more than the number of keyframes present on my audio slider, the loop stops. This leaves my maxV
and minV
variables equal to the highest and lowest keyframe values on the audio slider respectively.
No matter how many keyframes or how long the timeline is, this loop is able to work out the maximum and minimum values inputted into the audio slider without any errors. This is ideal for this template, as it is designed to be used for many different audio tracks, with varying volume and track lengths.
Using for
Loops With time
for
loops can also be used in timers. Take this example:
var copy = ["apple", "banana", "pear", "peach", "melon", "grapes"];
var copyLength = copy.length;
var dur = time;
let num = 0;
for (i = 0; i <= dur; i++){
if (true) num++;
}
copy[clamp(num, 0, copyLength-1)]
When inputted into the source text parameter, this example will cycle through the text in the copy
array, displaying a new entry every second, and stopping on the last entry. Let's go through how it does this.
First, I set up the array. Create the array and input the text to be shown on screen. Make sure the text is inside ""
so that After Effects knows it is text, and not a variable. I then create another variable, copyLength
, to determine how long the array is. Doing this instead of using a static integer will allow the expression to update, no matter the number of entries in the copy
array. I created the variable dur
, aka my line duration. Set to time
, the text will change every second. Finally, I set my last variable, num
, to 0, because array index numbers start at 0.
Now that the variables are all set up, I can create the for
loop. First, I create the variable i
and set it to 0 (to match the num
variable). For the second argument, I specify i <= dur
, meaning the loop will be active only when i
is less than the specified duration time. My third argument is once again i++
, adding 1 to the i
variable at the end of every loop.
While the loop is active, it will add 1 to the num
variable. Effectively, creating a counter which will increase in value by 1, every second of the composition timeline.
Now that I have my counting variable, I can finish the expression.
However, if I left the expression at:
copy[num]
it could cause an error once the num
variable counts higher than the number of values in the copy
array. To avoid this, I clamp()
the value. clamp()
allows me to set the minimum and maximum values of a variable. So, by setting the num
variable like so:
copy[clamp(num, 0, copyLength-1)]
it cannot go lower than 0, or higher than copyLength-1
. Because array index's start at 0, and the length of a variable is measured starting at 1, I need to -1 from the copyLength
to match the last index value of the array.
And there you have it: a way for After Effects to cycle through text (completely without keyframes or sliders).
By changing the dur
variable you can adjust the amount of time each array entry is on screen. For instance, setting dur = time / 2
will double the time each entry is on screen, so that each entry is on for 2 seconds.
You can also connect this to a .csv file instead of manually typing an array. However this is slightly more complex, and a topic worthy of its own article.
Conclusion
for
loops can be tricky, but are worth understanding when writing your own After Effects expressions. They can enable powerful flexibility when creating templates, and allow us to work around ever changing variables.
Find this article helpful? Have any questions? Know a better way to use for
loops in After Effects that I missed? Let me know in the comments.
Top comments (0)