User Guide Cancel

# Expression examples

Many of the examples in this section are based on expressions provided by Dan Ebberts.

# Layer revolves in a circle

You can create an expression without using properties from other layers. For example, you can make a layer revolve around the centre of the comppsition.

Select a layer. Press P to reveal its Position property in the Timeline panel. Alt-click (Windows) or Option-click (macOS) the stopwatch to the left of the property name.

Enter the following code snippet in the expression field:

```var centerOfComp = [ ( thisComp.width / 2 ), ( thisComp.height / 2) ];
var circleOverTime = [ Math.sin( time ) * 50, -Math.cos( time ) * 50 ];
centerOfComp + circleOverTime;```

# Wiggle randomly

The wiggle expression is one of the most common After Effects expression. It wiggles an object across random values. This expression can be used to make your scene seem more natural. You add this expression to the position property of the layer.

For example, wiggle (frequency, amount) where the first number is the number of wiggles per second and the second number is the value of the wiggle. So, using wiggle(2,30)will make the layer wiggle 2 times per second up to 30 pixels in any direction.

# Control a wiggle with Slider controls

Expressions can be keyframed by replacing values with links to expression controls, like a Slider Control. By replacing the second argument to the wiggle() expression with a link to a Slider Control, you can keyframe the behavior to start and stop at specific times.

1. Add a Slider Control effect to any Layer and name it "Wiggle Amount"
2. Apply the following expression to the Position of the same Layer:
```// Uses the pick-whip to create the "effect(...)" link to the Slider
var wiggleAmount = effect("Wiggle Amount")("Slider");

// Wiggles 4 times per second by the amount set by the Slider
wiggle( 4, wiggleAmount );```

# Overshoot

Commonly known as inertial bounce, this expression uses the animation of a layer's own keyframes to create a natural overshoot. It creates a bouncing motion of any parameter from one keyframe to the next, based on its velocity. The bounce happens in whatever direction the object is traveling. To achieve this animation: · Create or import your graphics in After Effects.

Add keyframes to the Position property of the layer you want to animate. · Add the following expression to the Position property of the layer:

```// Sets up values to control overshoot.
// Link these to Slider expression controls to quickly preview different settings.
var amp = 40;
var freq = 30;
var decay = 50;

// Finds the most recent keyframe
var nK = nearestKey(time);
var n = (nK.time <= time) ? nK.index : --nK.index;
var t = (n === 0) ? 0 : time - key(n).time;

// If the current time is later than a keyframe, calculate overshoot.
// If not, use original value.
if ( n > 0 && t < 1 ) {
var v = velocityAtTime( key( n ).time - thisComp.frameDuration /10 );
value + v * amp * .001 * Math.sin(freq * .1 * t * 2 * Math.PI) / Math.exp(decay * .1 * t);
} else {
value;
}```

# Rotate with time

You can use the pick whip to link rotation values between layers to animate objects. The way a clock works, consider these three circles as three hands of the clock - the hour hand moves from hour to hour, the minute hand rotates the full circumference of the clock face.

1. Import or create three circles solid-color layers. Let's assume one of them works like an hour hand, the other one as the minute hand, and the third as the seconds hand. (See Solid-color layers and solid-color footage items.)
2. Set the anchor points at the ends of the layers. (See Layer anchor points.)
3. Move the layers so that the anchor points are at the center of the composition. (See Move layers in space.)
4. Set Rotation keyframes for the hour hand. (See Set or add keyframes). Select the Rotation property for the minute hand and choose Animation > Add Expression.
5. Drag the pick whip to the Rotation property for the biggest circle. The following expression appears:   thisComp.layer("circle").rotation.
6. To make the second circle rotate 12 times as fast as the first one, add *12 at the end of the expression as follows: thisComp.layer("circle").rotation*12.
7. Repeat the same with the third circle and add *24 at the end of the expression: thisComp.layer("circle").rotation*24.

# Loop

The expression allows you to loop an animation without having to continuously add keyframes. For example, multiple shapes spinning until the end of 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”.

```//loopOut set to cycle all keyframes
loopOut("cycle", 0);```

types can be used with loopOut expressions. Cycle, Continue, Offset and Ping PongThis type of loop ends at the last keyframe, then starts again that the first keyframe in the selected range. The second variable is the number of keyframes to include. The loopOut is based on the last keyframe moving backward. By default, 0 accounts 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 includes 1 keyframe before the last one, 2 goes 2 keyframes back, and so on.

# Get the true Position of a parented layer

When a layer is parented, the value shown in its Position property is not its "true" location in the Composition but are instead relative to its parent layer's location. To find the parented layer's true location, an expression must be used to convert the parent layer's coordinate space to the Composition's coordinate space. Apply the following expression to the Position of an unparented layer to link it to the location of parented layer:

```// Defines the parented layer
var targetLayer = thisComp.layer("Parented Layer");

// Finds the parented layer's Anchor Point in the Composition
targetLayer.toComp( targetLayer.anchorPoint );```

# Delay a layer's Position from its parent

Delay and offset are a great way to make animation more dynamic and natural. You can create and control these types of offsets by parenting many layers to one parent layer and then applying an expression to the Position of each child to delay the motion inherited from the parent.

Note: this will only work with parented layers

```// Sets a delay amount in frames
var delay = 5;

// Offsets the layer's Position in time based on delay
parent.fromComp( toComp( anchorPoint, time - framesToTime( delay ) ) );```

You can also drive the delay amount based on the index of the child layer relative to the parent layer. This allow you to easily reorder the animation of the child layers by reordering them in the Timeline below the parent ( or above if you want their animation to happen before the parent ) :

```// Sets a delay amount in frames
var delay = 5;

// Multiplies delay based on this layer's index relative to it's parent
var multiplyDelay = delay * ( index - parent.index )

// Offsets layer's Position in time based on delay
parent.fromComp( toComp( anchorPoint, time - framesToTime( multiplyDelay ) ) );```

# Create a trail of images

This example expression instructs a layer to be at the same position as the next higher layer in the Timeline panel, but delayed by a specified amount of time (in this case, 0.5 seconds). You can set similar expressions for the other geometric properties.

1. Start with a shape layer scaled to approximately 30% of the composition size. (See Solid-color layers and solid-color footage items.)
2. Open the Position property and add keyframes. Select the layer. Press P to reveal the Position property. Alt-click (Windows) or Option-click (macOS) the stopwatch  button to the left of the property name. (See Setting, selecting, and deleting keyframes.)
3. Enter the following in the expression field:
thisComp.layer(thisLayer, -1).position.valueAtTime(time - .5)
4. Duplicate the last layer five times by selecting it and pressing Ctrl+D (Windows) or Command+D (macOS) five times. All layers follow the same path, and each is delayed 0.5 seconds from the previous.
```if ( toCompVec([0, 0, 1])[2] > 0 ) {
value;
} else {
0;
}```

# More Expression resources

Now that you have understood some of the concepts behind expressions, come to the community for some real-life examples, and to share your work.

You can also check out Dan Ebberts' excellent collection of example expressions and tutorials on his MotionScript website.

Dan Ebberts provides example expressions and tutorials for learning how to work with expressions on his MotionScript website. For example, Dan provides an excellent page about collision detection.

# More expression examples

Rick Gerard provides an example on his website that demonstrates rolling a square object along a floor so that the sides stay in contact with the floor plane.

Colin Braley provides a tutorial and example project on his website that show how to use expressions to make one layer repel others in a natural-seeming manner.

The AE Enhancers forum provides many examples and much useful information about expressions, as well as scripts and animation presets. In this post on the AE Enhancers forum, Paul Tuersley provides a tutorial and example project that show how to use expressions to animate several layers in a swarm.