DEV Community

Cover image for After Effects: While Loops
Kat
Kat

Posted on

After Effects: While Loops

Contents

Introduction

Full disclosure: I hate while loops. It has taken me a long time to get my head around them, but I think I finally understand their purpose.

Starting out, everytime I tried to write my own while loop, I would crash After Effects immediately. Now that I have a bit more knowledge on what went wrong, I want to write up what a while loop is, when to use it, and how you can incorporate it in your After Effects expressions without crashing your project (remember to save regularly, folks).

while Loops Vs if Statements

A while loop, like the name suggests, is a loop which executes as long as the expression is true.

"Executes as long as the expression is true" - that sounds a little bit like an if statement, doesn't it?

if (time < 2) "Text On"
Enter fullscreen mode Exit fullscreen mode

If you add this expression to the Source Text property of a text layer, you will find that the text reads "Text On" for the first 2 seconds of your timeline, and displays no text after that time. An if statement checks whether or not an expression is true, and if it is, it executes the command once, and only once.

However, if you changed it to this expression, you would find that After Effects crashes almost immediately:

while (time < 2) "Text On"
Enter fullscreen mode Exit fullscreen mode

This is because the while loop is a loop, so it executes the command repeatedly if the statement is true. As long as your cursor is in the first 2 seconds of your timeline, the while loop will loop infinitely. Not exactly ideal, and certainly not what we were trying to achieve! It is important to make sure that your while loop will, eventually, become false, to avoid these infinity loops.

For more on if statements, see my previous article here.

while Loops Vs for Loops

Okay, so it's important to note not to use while loops like if statements. But there are other types of loops we can use to make expressions. How does a while loop differ from a for loop, for instance?

A for loop works with 3 arguments, and is ideal when you know how many times you need to loop your commands.

Take this example, generating random numbers:

//variables
let counter = 0;
let num = 0;
let numArray = [];

//For loop
for (let i = 0; time >= i/2; i++) {
    seedRandom(counter, timeless = true);
    num = Math.floor(random(1, 6));
    numArray.push(num);
    counter++
}

//return
numArray[counter - 1]
Enter fullscreen mode Exit fullscreen mode

After setting up my variables, the loop starts with let i = 0, setting the first argument which executes once. Then, time >= i/2 establishes how often the loop should execute. Setting the loop this way will mean the loop executes twice every second, because time needs to be more than or equal to half of i. Finally, the third argument i++ will run everytime after the loop is executed. In this case, the value of i increases by 1.

Running this expression again in the Source Text property of a text layer will display a random number between 1 and 5 twice every second.

For more information on for loops, see my previous article here.

A while loop is best used when you do not know how many times your loop will need to execute. Because of this, I've found that they don't work well with expressions which involve time like for loops do so well, as that traps you in those pesky infinite loops.

So when should we use while loops?

When To Use while Loops

Keeping all of this in mind, a while loop should be used in these conditions:

  • We need to execute the expression more than once
  • We do not know how many times we need to execute our loop
  • We are able to break the loop, to avoid it executing to infinity

I recently found a use for this within After Effects, while working on my random number generator project.

Let's look at the following code:

//variables
let counter = 0;
let num = 0;
let numArray = [];

//For loop
for (let i = 0; time >= i/2; i++) {
    seedRandom(counter, timeless = true);
    num = Math.floor(random(1, 6));

//While loop inside of For loop
        while (num == numArray[counter - 1]) {
        num = Math.floor(random(1, 6));
        }

    numArray.push(num);
    counter++
}

//return
numArray[counter - 1]
Enter fullscreen mode Exit fullscreen mode

In this instance, the while loop is checking to see if the current value of num equals the previous value, stored in the numArray array. If it does, it generates a new random number between 1 and 5. The loop will execute as long as num and the previous number stored in the array are the same. However, once the numbers do not match, the while loop ends, and the expression is free to continue to the next line of code.

Because I may have to generate a new random number more than once, an if statement doesn't work here.

And because I don't know how many times I will need to refresh the random number, the number of loops is also a mystery: so a for loop is no good here either.

Therefore a while loop is my best option. It will execute as many times as I need it to, until the argument inside it is false, which will certainly happen as long as the random() function generates a number which does not match the previous one.

Conclusion

In conclusion, while loops are likely to be an uncommon tool in your After Effects expression belt. However they are useful to learn, for instances where the usual suspects may not be suitable.

Did you find this helpful? Do you have an example of using a while loop in your project? Did I get something wrong? Please leave me a comment and let me know.

Top comments (0)