User Guide Cancel

Expression examples

  1. After Effects User Guide
  2. Beta releases
    1. Beta Program Overview
    2. After Effects Beta Home
    3. Features in Beta
      1. Properties panel (Beta)
  3. Getting started
    1. Get started with After Effects
    2. What's new in After Effects 
    3. Release Notes | After Effects
    4. After Effects system requirements
    5. Keyboard shortcuts in After Effects
    6. Supported File formats | After Effects
    7. Hardware recommendations
    8. After Effects for Apple silicon
    9. Planning and setup
    10. Setup and installation
  4. Workspaces
    1. General user interface items
    2. Get to know After Effects interface
    3. Workflows
    4. Workspaces, panels, and viewers
  5. Projects and compositions
    1. Projects
    2. Composition basics
    3. Precomposing, nesting, and pre-rendering
    4. View detailed performance information with the Composition Profiler
    5. CINEMA 4D Composition Renderer
  6. Importing footage
    1. Preparing and importing still images
    2. Importing from After Effects and Adobe Premiere Pro
    3. Importing and interpreting video and audio
    4. Preparing and importing 3D image files
    5. Importing and interpreting footage items
    6. Working with footage items
    7. Detect edit points using Scene Edit Detection
    8. XMP metadata
  7. Text and Graphics
    1. Text
      1. Formatting characters and the Character panel
      2. Text effects
      3. Creating and editing text layers
      4. Formatting paragraphs and the Paragraph panel
      5. Extruding text and shape layers
      6. Animating text
      7. Examples and resources for text animation
      8. Live Text Templates
    2. Motion Graphics
      1. Work with Motion Graphics templates in After Effects
      2. Use expressions to create drop-down lists in Motion Graphics templates
      3. Work with Essential Properties to create Motion Graphics templates
      4. Replace images and videos in Motion Graphics templates and Essential Properties
  8. Drawing, Painting, and Paths
    1. Overview of shape layers, paths, and vector graphics
    2. Paint tools: Brush, Clone Stamp, and Eraser
    3. How to taper shape strokes
    4. Shape attributes, paint operations, and path operations for shape layers
    5. Use Offset Paths shape effect to alter shapes
    6. Creating shapes
    7. Create masks
    8. Remove objects from your videos with the Content-Aware Fill panel
    9. Roto Brush and Refine Matte
  9. Layers, Markers, and Camera
    1. Selecting and arranging layers
    2. Blending modes and layer styles
    3. 3D layers
    4. Layer properties
    5. Creating layers
    6. Managing layers
    7. Layer markers and composition markers
    8. Cameras, lights, and points of interest
  10. Animation, Keyframes, Motion Tracking, and Keying
    1. Animation
      1. Animation basics
      2. Animating with Puppet tools
      3. Managing and animating shape paths and masks
      4. Animating Sketch and Capture shapes using After Effects
      5. Assorted animation tools
      6. Work with Data-driven animation
    2. Keyframe
      1. Keyframe interpolation
      2. Setting, selecting, and deleting keyframes
      3. Editing, moving, and copying keyframes
    3. Motion tracking
      1. Tracking and stabilizing motion
      2. Face Tracking
      3. Mask Tracking
      4. Mask Reference
      5. Speed
      6. Time-stretching and time-remapping
      7. Timecode and time display units
    4. Keying
      1. Keying
      2. Keying effects
  11. Transparency and Compositing
    1. Compositing and transparency overview and resources
    2. Alpha channels, masks, and mattes
  12. Adjusting color
    1. Color basics
    2. Use the Adobe Color Themes extension
    3. Color management
    4. Color Correction effects
  13. Effects and Animation Presets
    1. Effects and animation presets overview
    2. Effect list
    3. Simulation effects
    4. Stylize effects
    5. Audio effects
    6. Distort effects
    7. Perspective effects
    8. Channel effects
    9. Generate effects
    10. Transition effects
    11. The Rolling Shutter Repair effect
    12. Blur and Sharpen effects
    13. 3D Channel effects
    14. Utility effects
    15. Matte effects
    16. Noise and Grain effects
    17. Detail-preserving Upscale effect
    18. Obsolete effects
  14. Expressions and Automation
    1. Expression
      1. Expression basics
      2. Understanding the expression language
      3. Using expression controls
      4. Syntax differences between the JavaScript and Legacy ExtendScript expression engines
      5. Editing expressions
      6. Expression errors
      7. Using the Expressions editor
      8. Use expressions to edit and access text properties
      9. Expression language reference
      10. Expression examples
    2. Automation
      1. Automation
      2. Scripts
  15. Immersive video, VR, and 3D
    1. Construct VR environments in After Effects
    2. Apply immersive video effects
    3. Compositing tools for VR/360 videos
    4. Tracking 3D camera movement
    5. Work in 3D Design Space
    6. 3D Transform Gizmos
    7. Do more with 3D animation
    8. Preview changes to 3D designs real time with the Real-Time Engine
    9. Add responsive design to your graphics 
  16. Views and Previews
    1. Previewing
    2. Video preview with Mercury Transmit
    3. Modifying and using views
  17. Rendering and Exporting
    1. Basics of rendering and exporting
    2. Export an After Effects project as an Adobe Premiere Pro project
    3. Converting movies
    4. Multi-frame rendering
    5. Automated rendering and network rendering
    6. Rendering and exporting still images and still-image sequences
    7. Using the GoPro CineForm codec in After Effects
  18. Working with other applications
    1. Dynamic Link and After Effects
    2. Working with After Effects and other applications
    3. Sync Settings in After Effects
    4. Creative Cloud Libraries in After Effects
    5. Plug-ins
    6. CINEMA 4D and Cineware
  19. Collaboration: Frame.io, and Team Projects
    1. Collaboration in Premiere Pro and After Effects
    2. Frame.io
      1. Install and activate Frame.io
      2. Use Frame.io with Premiere Pro and After Effects
      3. Frequently asked questions
    3. Team Projects
      1. Get Started with Team Projects
      2. Create a Team Project
      3. Collaborate with Team Projects
  20. Memory, storage, performance
    1. Memory and storage
    2. Improve performance
    3. Preferences
    4. GPU and GPU driver requirements for After Effects

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

Download sample project

Download

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.

Adobe logo

Sign in to your account