Select any one property or set of properties and choose Edit > Copy With Property Links or Edit > Copy With Relative Property Links.
Learn how to add, edit, and work with expressions in After Effects.
Any property that you can keyframe can use an expression.
To add and reveal an expression, select the property in the Timeline panel and do one of the following:
When the expression is activated, four icons appear under the property, and the value turns red.
The pick whip is the drag-and-drop selection tool for linking in After Effects. Click and hold the spiral icon next to the expression to enter selection mode. A whip links to your cursor as you drag to another property. Releasing the click targets the property underneath, generating expression syntax automatically.
To fully understand how expressions work, you need to understand how code affects a property value.
To link a set of properties across layers and compositions, use property links:
Select any one property or set of properties and choose Edit > Copy With Property Links or Edit > Copy With Relative Property Links.
Then paste those properties on any layer in a composition. The pasted properties will now be connected to the layer from which the properties were copied. Any change made to the original property is reflected in the instances of the pasted property links.
(or)
You can also copy an entire layer with property links and paste it to create duplicates that follow the changes made to the original.
The result of copying a layer’s Position property with the Copy With Relative Property Links command is as follows:
thisComp.layer("control_layer").transform.position
The result of copying a layer’s Position property with the Copy With Property Links command is as follows:
comp("source_comp").layer("control_layer").transform.position
To temporarily disable an expression, click the Enable Expression switch. When an expression is disabled, a slash appears through the switch.
If you're unsure about using JavaScript or After Effects expressions, go ahead and use the pick whip tool to get started. This tool is an easy way to link properties of expressions.
You can drag the pick whip to the name or value of a property. If you drag to the name of a property, the resulting expression displays all the values as one. For example, if you drag the pick whip to the name of the Position property, an expression like the following appears:
thisComp.layer("Layer 1").transform.position
If you drag the pick whip to one of the component values of the Position property (such as the y value), an expression like the following appears. Both the x and y coordinates of the property are linked to the y value of the Position property:
temp = thisComp.layer("Layer 1").transform.position[1];
[temp, temp]
If you use the pick whip to create an expression to refer to a layer, and that layer has the same name as another layer in the same composition, then the targeted layer’s name changes. The new layer name is the old layer name with a numeral at the end. This change is necessary to ensure that the expression unambiguously refers to a single layer in the composition.
The pick whip creates expressions, which use the names for properties as they appear in the Timeline panel. As these names are in-built, they can work when After Effects is running in another language. Editable property names are enclosed in double quotation marks and remain the same across languages. Deselect this option if the project is not being shared in other languages.
Here is an example in compact English:
thisComp.layer("Layer 1").transform.position
Here is the same expression, not in compact English:
thisComp.layer("Layer 1")("Transform")("Position")
Because the default is to use compact English, this document uses compact English in most examples and illustrations.
To edit an expression manually, do the following:
Enter text-editing mode by clicking in the expression field. When you enter text-editing mode, the entire expression is selected. To add to the expression, click within the expression to place the insertion point; otherwise, you will replace the entire expression.
Type and edit text in the expression field, optionally using the Expression Language menu. To see more of a multiline expression, drag the bottom or top of the expression field to resize it.
You can choose to only show properties which contain expression errors, which helps you quickly view and correct the errors. Use the following steps to do it:
To show only properties with expressions, select one or more layers, and then press EE.
Use the search field in the Timeline panel to search expressions as well as other components of a property. If the search string appears in an expression, the property and its containing property groups and layer are shown in the set of search results.
To resize the expression field, drag its top edge up or down.
In Graph Editor mode, the single expression field appears as a resizable box at the bottom of the Graph Editor. It shows only the expression for the selected property. To show multiple expression fields simultaneously, the Timeline panel must be in layer bar mode.
To toggle between layer bar mode and Graph Editor mode, press Shift+F3. You can also use the Graph Editor button at the Top of the Timeline panel for this.
To show the expression field in the Graph Editor, choose Show Expression Editor from the Choose Graph Type And Options
menu at the bottom of the Graph Editor. When you add an expression to a property, the expression editor is shown.
To see how an expression changes the value or velocity graph, click the Show Post-Expression Graph button
while showing the value or velocity graph in the Graph Editor.
The dimly colored graph displays the value or velocity before the expression is applied. The brightly colored graph displays the value or velocity after the expression is applied. Turning on the graph overlay for the Position property changes the motion path display in the Composition panel so you can see the expression-affected path.
If you write a complex expression, you should add comments that explain what the expression does and how its pieces work.
Use one of the following syntaxes to comment your code:
Once you have written an expression, you can save it for future use by copying and pasting it into a text-editing application. You can also save it in an animation preset or template project. However, because expressions are written in relation to other layers in a project and may use specific layer names, you must sometimes modify an expression to transfer it between projects.
You can define your own functions within expressions using normal JavaScript function syntax. In this example, a function is defined that calculates the average of two values, and the last line uses this function:
function average(a, b)
{
return (a + b) / 2;
}
average(position, thisComp.layer(1).position);
You must define each function fully within each expression in which it is used. There is no global library of functions that you can add to.
If you want to save an expression for use in another project, consider adding comments to the expression. (See Add comments to an expression.) You should also use variables so that you can change a value in one place rather than having to change it in several places.
For example, this expression has a multiline comment at the beginning that explains what the expression does and a short comment after a variable is declared and initialized that tells what the variable is for:
/* This expression on a Source Text property reports the name of a layer and the value of its Opacity property. */ var myLayerIndex = 1; // layer to inspect, initialized to 1, for top layer thisComp.layer(myLayerIndex).name + ": \rOpacity = " + thisComp.layer(myLayerIndex).opacity.value
You can save an animation preset that includes an expression and reuse it in other projects, as long as the expression does not refer to properties that don’t exist in the other projects. When you save a preset in which a property has an expression but no keyframes, only the expression is saved. If the property has one or more keyframes, the saved preset contains the expression along with all keyframe values.
You can save an expression as a preset and use it in other projects using the following steps:
In the TImeline, select the property on which the expression is applied, for example - the expression is applied to the Rotation property of a layer, so select it. Then, select Animation > Save Animation Preset.
Rename the preset - let's say "Time40", and save. By default, it is saved in the User Presets folder as an Adobe After Effects Preset file.
To apply your preset, twirl open Animation Presets under the Effects & Controls menu. You have two folders there - Presets and User Presets, twirl open User Presets. It has all the presets that you create within After Effects. Drag the "Time40" preset onto the layer you are working on and the expression is applied!
You can copy an expression from a layer property, with or without the keyframes for the property.
TIP: Copying an expression without keyframes is useful when you want to simultaneously copy multiple expressions and paste them onto one or more new layers, or when you want to copy one expression and paste it onto more than one layer.
As with pasting keyframes and other items, you can often paste the item into the target layer and rely on After Effects to determine which property should be the target of the paste operation. For example, this works for copying Position properties from one layer to another. But you must select the target property if you are pasting an expression from a Position property into a Scale property.
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.
Sign in to your account