After Effects Loop Expression
17.08.2020
Denys Bondartsov
Motion Designer & Tutor
Common knowledge: loops are a "thing" in Adobe After Effects.

It's probably safe to say you're here because you want to learn how to loop in After Effects. No doubt, there are a number of ways you can loop animations (and yes, comps) to your heart's content. There's a ton of tutorials on Time Remapping and "manual" (or more like, old-fashioned) looping; then there's the Loop Expression.
The sad thing is all of the above methods are either tedious, don't work like you'd want them to or just flat out do nothing important, really—minus the AE Loop Expression, that is.

Now, you might wonder why in the world you'd want to learn expressions. Someone could have told you how much of a drag they can be, what with all the math functions and all.

You. Heard. Wrong.

Or, more importantly, do you want to spend the bulk of your time copying keyframe upon keyframe and generally making a giant mess of your AE workspace? Didn't think so too.
Wait... What's a Loop Expression?
Expressions in AE are literally an upgrade from Adobe. For After Effects newbies, expressions are teeny-weeny pieces of code (usually running for a few lines, much like a script) which add more complexity to your animations with less work. You just plug them into your layers and boom! you get a cool result which you could find hard to do manually.

Unlike scripts though, expressions allow you to modify objects or layer properties instead of applying effects across multiple keyframes. By automating these actions, you'll work more efficiently and save lots of time.

The After Effects Loop Expression is a great way to effortlessly add repetition to your animated creations. True to its name, it loops a specific keyframe or series of keyframes in your animation.

The Loop Expression has three parts namely: property, type and argument modifier; in the format property("type", modifier). The loop property is usually loopIn or loopOut, and less commonly loopInDuration or loopOutDuration.

There are four loop types in After Effects, which we'll cover in a minute. The argument modifier is an integer (whole number) value which specifies the number of keyframes to loop. The default value is 0, which is your way of telling AE to loop all keyframes.

Before we dive in, here's some background info on using loop expressions in AE. To add a loop expression (or any other type), you'll need to open the expression field. Simply go to the Timeline panel and select the layer transform property which will take the expression. Then, Option+Click (Mac) or Alt+Click (PC) on the stopwatch icon next to the transform property. This will toggle the expression field and you can then type or paste in the expression code.
"loopIn" vs "loopOut"
From a distance, loopOut seems pretty much like every other expression you know—complex, confusing and scary as hell. Once you start to use it, however, you'll see it has so much to offer in terms of simplicity, control and ease of use.

Like the loop property, there are equally four loop types: cycle, pingpong, offset, and continue. Say you wanted to add a "continue" loop type to your loopOut property, you simply type loopOut("continue");

You can swap out "continue" with another one to change the loop type.

Okay! Now, we need to know what each loop type does, so let's take a closer look at them.
Cycle
loopOut("cycle"); or loopOut(); 
loopIn("cycle"); or loopIn(); 
What the cycle loop type does is repeat your keyframes on and on and on… The loop will go from the first to the last keyframe, then go right back to where it started—at the first keyframe—all the way to the last keyframe over and over again.

Lastly, "cycle" is the default loop type in AE, with an argument modifier (we'll explain this in the next section) of 0. This implies that instead of typing loopOut("cycle"); or loopOut("cycle", 0); you can skip ahead and just type loopOut(); They all mean the same thing, and since we're all for working smarter than harder, maybe pick the latter huh?
Pingpong
loopIn("pingpong"); 
loopOut("pingpong"); 
Picture the pingpong loop as some kind of infinity loop, like a snake chasing its own tail in a circle.

Unsurprisingly, the "pingpong" loop type rocks back and forth between a series of keyframes. It literally ping-pongs between the first and last keyframe you selected, alternating between forward and backwards movement.
Offset
loopIn("offset"); 
loopOut("offset"); 
It's a bit sad that a lot of motion designers don't know how offset works, even though it's one of the most powerful loop types After Effects has to offer.

According to the After Effects expression language reference, offset "repeats the specified segment, but offsets each cycle by the difference in the value of the property at the start and end of the segment, multiplied by the number of times the segment has looped."

In plain English, loopOut('offset'); repeats an animation, starting the next loop at the last frame of the previous loop. That is, the object keeps moving in space, offsetting the next cycle while maintaining the difference in the property value.

Say you animate a layer to rotate 0 to 90 degrees. In the next cycle, the layer rotates 90 to 180, then 180 to 270 and from 270 back to 360—or, you could say 0—degrees. Forever and ever…
Continue
loopIn("continue"); 
loopOut("continue"); 
The "continue" loop type is less common, probably because it's a bit different from other loop types (i.e., it doesn't repeat keyframes). Instead, it continues to animate a layer property using the velocity of the last keyframe. Continue is the only loop type that doesn't accept an argument modifier.

You can apply the four loop types to the loopIn, loopOut, loopInDuration or loopOutDuration property.
Argument Modifier for Loop After Effects Expression

The argument modifier is named numKeyframes — fittingly. Like we mentioned earlier, this modifier determines what segment to loop. The default value is 0, which is your way of telling AE to loop all keyframes. Still, how exactly does that work?

In the case of loopIn, the loop plays from the In point (i.e., first keyframe), moving towards the Out point of the layer. The segment of time looped is the portion of the layer from the first keyframe to the numKeyframes+1 keyframe.
Final Thoughts

So, we've seen the two main loop properties loopIn and loopOut, and how they work in tandem with the different loop types to achieve awesome effects. The loop expression can be a good place to start learning the After Effects expression language. You can do some really fun/cool things with it in your projects.

How about we wrap it up with a note or two about the loop expression?
1
Remember to add a semicolon at the end of each expression statement, where necessary. You'll usually have to do this when you're calling an expression (e.g., loopout();) or assigning a value to a variable (e.g., t = 2;).

2
"Cycle" and "pingpong" seem similar but they aren't. Cycle loops from first to last keyframe, then jumps back to the first keyframe again, over and over again. In contrast, pingpong loops from the first to last keyframe, then reverses its direction (i.e., from the last keyframe and back to the first).
Subscribe to our newsletter and get access to free tutorials, updates and discounts on future courses:
Get even more with our newsletter
We have created an awesome place on Facebook for you to share cool stuff and get answers to the burning questions:
Join our community on Facebook
Find some comments