# 6 Common After Effects Expressions You Should Be Using

Pardon the expression, but you should “work smarter, not harder.” See what I did there? Today, we’re going to talk about expressions. Not that kind of expression though. We’re going to be talking about After Effects expressions, and how they will help you to work smarter, not harder.

For those new to After Effects or anyone who hasn’t dug too deep, expressions are bits of code that you can plug into various After Effects layers that, in turn, tell the program to do certain things without you having to hand animate them. We have a more detailed explanation in our post __How To Use Templates, Presets, Scripts, & Expressions__.

Basically, expressions can save you a lot of time and give you some cool results that may otherwise be hard to pull off. Today, we’ll talk about 6 common expressions that you can use over and over again.

Before we get started, let’s just cover the basics of using an expression. To add an expression or make changes, you’ll want to open the expression controls. Do this by picking the layer property you want to work with and then hold Option on Mac or Alt on PC while clicking on the keyframe (stopwatch) icon. This will open the expression controls and allow you to paste in or type the code.

You can download the After Effects project that contains all of the examples below, including the bird animation HERE.

### Wiggle

One of the most simple and useful expressions is the Wiggle. In motion graphics, we like to keep things moving. Having an object or layer wiggle around and look somewhat random is hard to pull off. But the Wiggle expression will do it for you with ease.

Simply select the layer you want to work with. Open the Position property, and then access the expression controls. Paste or type the simple expression below and replace “frequency” and “amount” with numbers to represent those values.

The frequency value will affect how fast the layer wiggles and the amount value will affect the magnitude of the wiggle. Note that the magnitude equals the largest distance the layer may travel at once. Since it’s random, it will generally travel less distance. Play around with different values until you find what you like.

This expression will randomly move your layer on the x and y axis if it’s a 2D layer. And if it’s a 3D layer, it will wiggle the x, y, and z axes.

**Wiggle Expression**wiggle( frequency , amount );

### Wiggle One Dimension

The Wiggle expression is super handy, but it does affect all of the axes, and sometimes you may only want to wiggle on one axis. For instance, let’s say you want your layer to move back and forth in z space randomly, but not across the x or y-axis.

Here’s where the Wiggle One Dimension expression comes in. (Thanks Dan!) The base of this expression is the same as the Wiggle expression, but you need some extra data to tell the program to limit the movement to one axis.

Here again, you’ll change out “frequency” and “amount” with numbers to represent the speed and amplitude of movement.

In order to limit the wiggle to one dimension, you'll need to add an extra set of instructions. The example below uses [w[0],value[1]] and limits the movement to the x-axis. For movement on the y axis only, you'll change this section to [value[0],w[1]], and on a 3D layer where you want to wiggle on the z-axis, you'll use [value[0],value[1],w[2]] instead.

**Wiggle One Dimension Expression**

w = wiggle(frequency , amount);

[w[0],value[1]]

### Inertial Bounce

The Inertial Bounce expression is a bit more complex than Wiggle, but when used properly, it can be really helpful in simulating physics for a bounce animation. For example, if you wanted to have a ball fall from the top of the screen, hit the bottom, and bounce with decaying force until it stopped, you could do this is individual keyframes and the graph editor. But it would take a long time to get right.

Instead, you can paste in the Inertial Bounce expression on your position property, and adjust the parameters to your liking.

In the expression below, there are three values you’ll want to adjust for your needs. We’ve filled them in with numbers to start, but you can change them as needed.

These are the values related to “amp”, “freq”, and “decay”. Here the “amp” value represents how high the initial bounce will be when it starts. The “freq” value defines how fast the layer will bounce. And the “decay” tells the expression how much decay to add between bounces. In other words, the higher the decay, the less the layer will move on each successive bounce before settling.

**Inertial Bounce Expression**n = 0;

if (numKeys > 0){

n = nearestKey(time).index;

if (key(n).time > time){

n--;

}

}

if (n == 0){

t = 0;

}else{

t = time - key(n).time;

}

if (n > 0){

v = velocityAtTime(key(n).time - thisComp.frameDuration/10);

amp = .05;

freq = 4.0;

decay = 2.0;

value + v*amp*Math.sin(freq*t*2*Math.PI)/Math.exp(decay*t);

}else{

value;

}

### loopOut

The loopOut expression is rather simple looking, but it does have a lot of control to it. The basic premise of the expression is to allow you to loop an animation without having to continuously add keyframes.

Think of something like a record spinning on screen until the end of the comp. Here you can set an initial keyframe for the start rotation and another for the end rotation. Then when you add the loopOut expression to the rotation parameter, the spinning will continue.

The variables on the expression below are for the type of loop, and how many keyframes to include in the loop.

Let’s discuss the type of loop first. In the expression below, we’ve set the type to “cycle”. This type of loop will end at the last keyframe, then start again that the first keyframe in your selected range, and will most commonly be used.

Additionally, you can change this value to “pingpong”, “offset”, or “continue.” For more information on how those values work, check out the __After Effects Expression Language Reference__.

The second variable is the amount of keyframes to include. This may be a bit confusing at first. Since this expression is a loopOut, it’s based on the last keyframe moving backward. By default, 0 will account for all keyframes in the set from beginning to end. If you don’t want to use all of the keyframes for the loop, set a number counting backward from the end. Setting the variable to 1 will include 1 keyframe before the last one, 2 will go 2 keyframes back, and so on.

Also note that if you do want to use the default values of (“cycle”, 0), you don’t even have to type them in. You can simply type loopOut().

**loopOut Expression**loopOut("cycle", 0);

### Time

Time is a variable used in expressions to tell something to move over time with the default being per second. One of the simplest ways to make use of this is with rotation.

Adding the Time expression to a layer's rotation will cause it to rotate from its axis point at one degree per second. In order to speed that up, you can add simple math functions, using * for multiply and / for divide.

Take our GIF example with the hands of the clock. We set the axis point at the center of the clock for the hands to rotate around. For the minute hand, we altered the expression to time*200, making it rotate at 200 degrees per second. For the hour hand, we set the expression to time*200/12. By adding the same expression and then dividing by 12, we ensure that the minute hand will go around 12 times by the time the hour hand goes around once.

**Time Expression**time*200

### To Comp

The basis for the expression below is to allow you to give 3D properties to 2D effects. This is particularly useful in something like the example below, where we have a 2D layer with the 2D beam effect, but we want to have it move with the circles that are in z space.

To do this, we'll add expressions to the Starting Point and Ending Point controls in the beam effect. The expression below is set with "Null 1" as the layer to connect to. In your own version, you'll replace that with whatever layer you want to attach the end of the line to.

This is a perfect and easy way to create cool dynamic motion in 3D space with 2D layers.

**To Comp Expression**layer = thisComp.layer("Null 1")

layer.toComp([0,0,0])

Expressions can really change the way you work within After Effects. If you want to get serious about it, you can start writing your own expressions to do all kinds of things for you. But you can also start building a library of simple expressions like the ones above to be more efficient and give you more creative control over your work.

Here is an example of a more complex animation that uses many of the expressions above.

Editor's Note: We used the GifGun plugin to create these Gifs directly out of After Effects, and it was a real time saver. For more information on GifGun, check out our recent GifGun review.