From the course: After Effects CC 2023 Essential Training

Using expressions to loop animation

- [Instructor] After Effects offers users the ability to simplify and automate tasks using lines of code in a system called expressions. They can be used in place of key frames or work in conjunction with them. Expressions can be daunting, but it's helpful to see how the basics work and how they can speed up your workflow. Let's take a look. In this composition, I've got an animation of a wifi signal and it just animates on and animates off over the course of what, 45 frames, 44 frames? Let's see how we can loop this. I'll nest this into a new composition here, this 03 comp. And here you can see that it only plays once. Let's use time remapping to get looping involved. Let's right-click on this layer and come up to Time. Enable Time Remapping. After Effects gives us two key frames for the start and the end. And we're going to manipulate these key frames by using expressions. To add an expression to a property, either right-click on the property and select Edit Expression, or hold alt or option and click on the property stopwatch. When we do, you'll notice a couple things. The original blue value now turns red, and that's After Effects indicating to us that a particular property is driven by expressions. Underneath we have the expressions window where we'll do the actual typing of code. Over here we have a button that enables or re-enables the expression. If we tap on this, you can see that the red numbers turn back to blue, indicating to us that the expression is off. I'll turn this back on. Over here we have this little fly out menu that allows us to insert basic lines of code into our expression. So for this, I'm going to come over to the Property family and I'm going to select Loop Out. Basically what this line of code is telling After Effects to do is start repeating or cycling through the key framed animation. Once it hits the last key frame, start over again. So if we scrub through this, you'll notice that my frame count for the time remapping is starting at zero. And when we get to the last key frame, we're here at frame 45 of our animation. And when we get to the very next frame, you'll note that the time remap is now reading one. And that's because this animation is looped thanks to this expression. And as we scrub through, we keep going, and it does it over and over again until we hit the end of our composition. Let's see what this looks like in terms of the graph editor. I'll click on the graph editor, and one thing we want to make sure that we have is under these options, make sure Show Expression Editor is enabled. And right now I'm just going to view the value graph, and just make sure that our property is selected. And it's basically the key frames that we saw earlier. Zero to frame 45. Now note that we have this dashed line here, and that is representing our original key framed values. If we disable this, that's what this is telling us. It's going to hold at frame 45 until the very end of the composition. But because we're altering this value with expressions, what does that look like and how does that affect our graph? Let's come over here, turn on our expression. And the button right next to it is the Show Post Expression Graph. This will visualize what the expression is doing to our original values, and in this case, we're starting over from frame 45 down to frame zero right here, or frame one, and looping this animation over and over again. So, if we were to make changes to this original key framed animation, watch what happens to the loops. I'll select this one key frame and come over here and select Easy Ease Out. That adds an ease to the outgoing key frame, and all the repeating loops follow suit. One other type of loop out that we can do is pingpong. So let's select cycle and type in the word pingpong, one word. And now the animation plays forwards and then backwards again. So let's play this back. There it is forward. And there it is rewinding, and forward and rewinding over and over again. So this is really handy and you can see how this would be kind of tedious if we were to key frame this by hand. Let's see how we can apply some of this to a character. Here in this composition I've got some key frames for this dancing character, and let me just select everything here and hit U on the keyboard to expose all the key frame properties. And now what I'll do is just add in an expression for this first property, the face. We'll hold down alt or option and click on the stopwatch. And here, let's go to our pop-up menu. And I'm going back to my property and select Loop Out. Just by inserting that bit of code, you can see that now my face animation is looping. And now let's apply that to all the other animated layers. Let's come over here and right-click on our Position property and say Copy Expression Only. Now all I have to do is select all the other animated properties and then come up to Edit, Paste. And you'll note that all these values now turn red, indicating that they're driven by expressions. And when we play this back, that loop out applies to all these animated properties. And now we have a little dancing character. So to finish this off, we have a little dance party, and all of this is just using the same looped animation, and we have some repetition going on here with the repeaters in the background. And the world of expressions can be really fun. It's a little daunting at first, of course, but there are many other movies in the library that can help you out with your journey. This was just a small taste of the world of expressions and how it can help you inside of After Effects.

Contents