Creative Coding in After Effects
Controlling After Effects Animation with Expressions and Logic
Introduction
For coders, opening After Effects can feel like wandering into a nightclub with no instructions. Panels are everywhere, the timeline is a spaghetti mess, and tiny stopwatch icons apparently change the laws of physics. It’s a visual jungle that doesn’t behave like any IDE they’ve used before. Meanwhile, creatives are completely at home keyframing layers and finessing bezier handles. However, they often freeze at the mention of “scripting.” Code feels cold and abstract, making it seem out of place in a workflow built on intuition and visual feedback.
But there’s a sweet spot in the middle: ExtendScript, which is Adobe’s customized version of JavaScript. It is based on the older ES3 spec, but don’t let that scare you. It is designed to talk directly to After Effects by moving layers, animating properties, and generating complex motion with just a few lines of code. Here’s the kicker: in many cases, scripting isn’t the hard way; it’s the smart way. When you need to animate along a spiral path, rotate with precision, or scale something over time, writing a few lines of ExtendScript can be faster and more accurate than wrestling with twenty keyframes and a motion path.
Adobe’s Introduction and use of ExtendScript
ExtendScript first appeared in the early 2000s as part of Adobe’s effort to unify scripting across its Creative Suite applications. Built on ECMAScript 3, an early version of JavaScript, ExtendScript was Adobe’s answer to automating repetitive tasks, customizing interfaces, and generating complex behaviors that would be painful (or impossible) to keyframe by hand. It was tightly integrated into apps like After Effects, Photoshop, and Illustrator, allowing scripts to poke directly into project structures and manipulate layers, comps, and properties in real time.
The language itself is mostly just JavaScript, with a few quirks and custom APIs sprinkled in. If you’re familiar with JavaScript fundamentals — variables, loops, functions — then ExtendScript won’t feel like foreign territory. You won’t be dealing with modern JS niceties like let, const, or arrow functions, but the core syntax is the same. Think of it like stepping into a slightly older version of the language: no React, no promises, but enough to get the job done. And once you understand the basic object model of After Effects, scripting animations or layer behaviors often becomes more predictable and reusable than relying on the UI alone.
Our Test: A Small Ship Spiraling into a Black Hole
Let’s put this into practice by animating a spaceship spiraling into a black hole. This test scene gives us a great excuse to demonstrate scripting on the Position property, where expressions can really shine. We'll also apply expressions to Rotation and Scale, even though those could be handled with traditional keyframes, it's useful to see how scripting can streamline the process or make it more dynamic.
More importantly, this example illustrates that expressions are not limited to circular paths or basic math. We're using an elliptical spiral with a moving center point, giving the animation a more natural, 3D-feeling path as the ship falls into the vortex. It's not just about spinning around a fixed point; we’re mimicking a kind of gravitational pull that changes over time. This is where scripting starts to offer creative possibilities that would be difficult to replicate manually.
This demonstration is not an exhaustive tour of everything ExtendScript can do. You could apply similar expressions to animate color shifts, effect intensities, or even audio-reactive behaviors. For the purpose of clarity, we’ll keep things focused on the essential Transform properties: Position, Rotation, and Scale. If you'd like to follow along, I’ve included some assets below for you to test with.
After Effects – Accessing the Expression Editor
To access the expression editor for a property, you'll use a modifier-click action on the stopwatch icon. On Windows, hold Alt while clicking the stopwatch. On Mac, hold Option while clicking. This tells After Effects that you want to add an expression instead of setting a keyframe.
We’ll be doing this for three core Transform properties:
Position, to define the spiral movement
Rotation, so the ship appears to turn naturally as it spins
Scale, to simulate the ship shrinking into the distance
These are the most visually useful properties for this demonstration, though expressions can be applied to nearly any animatable effect value inside After Effects.
Note, my composition was set to ten seconds. You’ll notice some areas where the total time is specified in the expression. Make sure your composition settings match this value. I’m using 720, and I added some other visual effects (such as a glow on the spaceship, and a rotation of the vortex that isn’t covered here).
Position Expression
// Position
// Spiral inward with a moving center (3D look)
var startPos = [200, 394];
var startCenter = [640, 360]; // center of 1280x720
var endCenter = [948, 426];
var totalTime = 10;
var laps = 3;
var t = clamp(time / totalTime, 0, 1);
// Animate center point
var center = [
linear(t, 0, 1, startCenter[0], endCenter[0]),
linear(t, 0, 1, startCenter[1], endCenter[1])
];
// Spiral radius shrinks
var maxRadius = length(startPos - startCenter);
var radius = maxRadius * (1 - t);
// Angle increases to make laps
var angle = laps * 2 * Math.PI * t;
// Spiral around the shifting center
var x = Math.cos(angle) * radius;
var y = Math.sin(angle) * radius;
var pos = [center[0] + x, center[1] + y];
// Lock to startPos at t=0
t < 0.01 ? startPos : pos;
Scale Expression
// Scalle
// Shrink in 3D from 12 to 0 over 10 seconds
var startScale = 12;
var endScale = 0;
var totalTime = 10;
var t = clamp(time / totalTime, 0, 1);
var s = linear(t, 0, 1, startScale, endScale);
[s, s, s];
Rotation Expression
// Rotation
// Spin one full turn per lap
var startRotation = 180; // degrees at time = 0
var totalLaps = 3;
var totalTime = 10;
var t = clamp(time / totalTime, 0, 1);
// 360 degrees per lap
var rotation = startRotation + (totalLaps * 360 * t);
rotation;
First Results?
Once everything is wired up, you should see your spaceship follow a smooth, elliptical spiral across the screen. It begins on the left side of the frame, circling inward while the center of its path drifts toward the black hole on the right. This shifting spiral center creates a sense of depth, as if the ship is not just moving across the screen, but being pulled into a distant, off-axis vortex. As it spirals in, the ship gradually shrinks in scale, reinforcing the illusion that it’s falling deeper into space. The motion should feel fluid and natural, giving a strong sense of both direction and depth without needing a single keyframe.
Extra Credit: Cross-Property Expressions
Expressions in After Effects aren’t limited to just generating values in isolation. One of their most powerful features is the ability to pull data from other properties, even if those properties are keyframed, driven by other expressions, or animated separately. This opens the door to dynamic relationships between aspects of a layer. For example, you can tie the intensity of a glow to a layer’s opacity, drive blur based on a layer’s speed, or as we’ll demonstrate, shift color based on rotation.
For this effect, we’ll use Color Balance (HLS), found under Effect > Color Correction > Color Balance (HLS). Unlike the older Hue/Saturation effect, this one supports expressions. We’ll focus on its Hue property, which rotates the color spectrum of the layer. By applying an expression to this property, we can cause the ship’s colors to shift as it rotates simulating lighting changes or a color phase effect as it spins toward the black hole.
Here’s the code you’ll use for the Hue property:
// HLS Hue Shift
var r = transform.rotation % 360;
if (r < 0) r += 360;
var hue = 0;
if (r >= 90 && r <= 270) {
hue = linear(r, 90, 270, 0, 90);
} else if (r < 90) {
hue = linear(r, 0, 90, -90, 0);
} else {
hue = linear(r, 270, 360, 90, -90);
}
hue;
This tells After Effects to shift the hue gradually to +90 degrees when the ship is rotated between 90 and 270, and to -90 degrees when it's rotated through the remaining arc. The transition between ranges is smooth, creating a dynamic but subtle color modulation based purely on the ship's rotation.
If you're unsure how to reference properties like transform.rotation
, the easiest approach is to Alt-click (or Option-click on Mac) the stopwatch of the target property (the one you want to animate) and use the pick whip inside the expression editor to drag to the source property. After Effects will automatically generate the correct expression path. For example, if you're linking Hue to Rotation, it may generate something like thisLayer.transform.rotation
. From there, you can build on it with functions like linear()
to map ranges and produce the behavior you want. This technique is also a quick way to learn the naming structure of properties without digging through documentation.
This kind of cross-property linkage can be used almost anywhere in After Effects. It's a clear example of how expressions can help you build smarter, more reactive animations that evolve naturally with the motion, rather than relying on dozens of hand-tuned keyframes.
Did you find these tips about After Effect’s ExtendScript helpful?
Please consider supporting sonnik’s work with a one-time five-dollar contribution!
Conclusion
As you've seen, scripting in After Effects doesn't have to be intimidating or over-engineered. With just a few lines of code, you can create complex, dynamic motion that would be a pain to replicate using traditional keyframes alone. Expressions let you build relationships between properties, automate repetitive tasks, and generate motion that evolves over time in a way that's both precise and easy to update.
While this demonstration focused on a spaceship spiraling into a black hole, the techniques apply to almost anything. These techniques can be used with logos, titles, abstract shapes, or even UI elements. Whether you're easing into scripting from the creative side or venturing into After Effects from a coding background, learning to wield expressions gives you serious leverage. You are not replacing creativity with code; instead, you are amplifying it.