Post Production

Automatic animation with expressions

How time is crucial to animating automatically in After Effects

Have you ever wished you could just press a button and have After Effects do all of the work for you?  While we’re not quite at that stage yet, expressions in After Effects can go a long way to automating specific types of animation.  This video tutorial introduces the topic of how to automatically animate using expressions, demonstrating why it’s not quite as simple as you might think.

When I was working on an earlier article, demonstrating how I used maths to automatically animate a cake, I felt a strong sense of deja vu.  I was creating a graphic to illustrate what the modulo function did and I was sure that I had done this before.  But I searched through all of my older articles and I couldn’t find anything.  Eventually, I uncovered a tutorial that I made in 2009 but never posted!  It had been sitting in a dusty folder on an old drive for almost 10 years… I’m not completely sure why I didn’t post it, but I updated the graphics and edited the audio and here it is now.  Better late than never!

In the original 2009 version of this tutorial, I spent a few minutes at the start apologising for making a tutorial about expressions and justifying why they are useful.  Now that it’s 2018,  I think expressions are much less divisive and I’ve chopped that bit out.  However I’d like to emphasise that the tutorial is demonstrating types of animation, and not just lines of JavaScript.  We often use the term “motion graphics designer”, but I often think that the “motion” is left out of the design part (this is the topic of an upcoming article…)  While this tutorial is clearly about expressions, I’ve structured it around types of motion – linear motion, smooth motion, and so on.

The point of the video tutorial is that all automatic animations need to be based on time.  The tutorial demonstrates a few useful pieces of code, so here they are in a form you can copy & paste.

This first expression is a basic frame counter for a layer.  By itself, it doesn’t do anything.  But it sets up basic variables and creates a counter (t) which starts at 0 at the beginning of the layer, and is clamped to the layer’s duration.  One example I mention in the tutorial is an expression that automatically fades a layer up at the start.  This frame counter is an example of the code you would use as a base for that sort of automation.

t = time - thisLayer.inPoint;
dur = thisLayer.outPoint - thisLayer.inPoint;
clamp (t,0,dur);
fps = 1 / thisComp.frameDuration;
t = t * fps; dur=dur*fps;
[t]

The tutorial also mentions the “linear” function, which scales one range of values to another.  If you did want an expression that automatically fades up a layer, then you could use the frame counter code above, and then use the linear function to scale the frames being counted to opacity values.  If we wanted a 12-frame dissolve, we would be scaling the numbers 0 – 12 to 0 – 100, like this:

opacity = linear ( frame counter,0,12,0,100)

The frame counter sets up a variable for the layer duration, but it’s not currently being used.  We can use it with a second linear function to also make the layer fade down, at the end.  Instead of counting frames 0 – 12 at the beginning of the layer, we need to start counting 12 frames before the layer ends, like this:

opacity = linear ( frame counter, dur-12,dur,100,0)

However we only want to start fading down AFTER the layer has faded up, so we can add an if statement to check first.

If we use a variable for the fade duration, it makes it easier to change the duration of the fade.  Put together in a form you can copy & paste to a layer’s opacity, we get this:

fade = 12; // this is the duration of the fade in frames //
t = time - thisLayer.inPoint;
dur = thisLayer.outPoint - thisLayer.inPoint;
clamp (t,0,dur);
fps = 1 / thisComp.frameDuration;
t = t * fps; dur=dur*fps;
o=linear(t,0,fade,0,100);
if (t>fade) o=linear (t, dur-fade,dur,100,0);
[o]

The second expression here looks pretty complex, but it’s easy enough to copy & paste.  It creates a linear ping-pong animation, just set the variables in the first line to suit:

min=35; max=75; spd=100;
n = min+Math.abs((((time*spd)+(max-min))%((max-min)*2))-(max-min));

Finally, here’s the expression I used to demonstrate layers that automatically move in a circle:

spd=3;
xradius=400;yradius=400;
x=Math.sin(time*spd)*xradius;
y=Math.cos(time*spd)*yradius;
x=x+(thisComp.width/2);
y=y+(thisComp.height/2);
[x,y]

I’ve posted expressions in articles before, and sometimes I get comments / emails from readers who say they don’t work.  The problem is not the expressions themselves, but rather the way they get copied and pasted.  The biggest culprit seems to be quotation marks, which sometimes get automatically converted to smart quotes when the text is copied.  This might look nicer in Word but it stops the expression from working in After Effects.  While the expressions above don’t have quotation marks in them, if you are having problems with copying & pasting them into After Effects, it’s safest to paste them into TextEdit / Notepad first to convert them to plain text, then copy the plain text from there.

Expressions are a fundamental part of my daily workflow, and so they’ve popped up in a number of other articles I’ve written.  While this tutorial is intended as a basic introduction to animating automatically with expressions, if you find it interesting then here are links to other articles that feature the use of expressions:

This article shows how I used expressions to automatically produce minutes of animation in only a few hours.

This article demonstrates how to use text layers to manage complex expressions.

This article demonstrates how expressions can automate multiple versions of the same animation, such as name straps.

This article includes a number of expressions for working with the After Effects camera.

This article shows how a simple expression can be used to generate Z-depth maps from After Effects compositions.

And of course, there are plenty of other articles that don’t mention expressions but are all to do with After Effects – check them out too!


Share Our Article

Chris was born a geek, and was lucky enough to own a Commodore 64 at a time when the number of students at his primary school who owned a computer could be counted on one…

Leave a Reply

avatar
  Subscribe  
Notify of