Expression Basics

What are expressions?

In After Effects you can change a layers properties (such as position or opacity) by typing in or dragging the values, adding keyframes to change them over time. This process of changing and animating properties forms the basis of working in After Effects.

  • What if rather than setting a property manually, you need it to always be the same as another one?
  • Or you need it to decide what value to have based on something else?

This is where expressions come in, as an alternate way of setting the value of properties.

They enable you to combine values together and have a property choose what it's value is.

Example Expression

This alternate way is by writing lines of code, in a language called JavaScript, that After Effects will run. The result of this code is then set as the value of the property.

Expressions are lines of computer code that After Effects runs every frame to generate the value of a property.

How to add an expression

You can add an expression to a property in After Effects by:

  • Holding alt while clicking on the properties stop watch (the icon next to it's name)

  • Using the 'Add Expression' shortcut alt + shift + = with the property selected

  • Clicking Add Expression in the Animation menu while the property is selected

These will add a new expression with some placeholder code to the property.

This placeholder code references the before expression value, which is the value of the property as if there was no expression applied. This means you can continue to modify and animate the property as if there was no expression.

Setting the value

You can override this placeholder by typing in some JavaScript code that, when run, gives After Effects a value in the same structure (or type) as the original.

For example, let's replace our position property expression with a new value.

js
[960, 540];

You'll learn more about the [] syntax in future topics.

Now the postion of the layer will always be at 960, 540 even if we change or animate the property, as it is overidden by the expression.

Since we've written this value in JavaScript code, we can change and modify this value with math, or connect it to other properties in our project.

For example:

js
[960 + 50, 540 + 100];
js
1010, 640;

The rest of this course will cover the fundamentals of the JavaScript language and it's use within After Effects expressions.

For enquiries contact us at hey@motiondeveloper.com