So I want to do something simple: define two sets of coordinates (a "from" and a "to" position), along with a duration in frames. I want the animation to be executed, then reversed, and looped.

I know I can set two keyframes, and use loopOut("pingPong"); but I want to try and achieve this with a simple expression.

I'm sure it's possible!

  • I don't have specifics, so I won't post an answer, but I presume part of this could be done with a While loop: while(time < 50) { [value[0]+1,value[1]+2] ) }
    – nchpmn
    Jan 31 '14 at 7:18
  • The while loop would be executed every single frame. The above expression would run forever on frame 0, because for that frame time will always = 0
    – stib
    Jan 31 '14 at 12:49
  • or indeed any frame < 50 the loop would run forever.
    – stib
    Nov 11 '15 at 0:51

Darby Edelen posted an answer on Creative Cow.

You won't be able to define any spatial paths any other way, but this should work:

d = 5;
fd = d * 2;
p1 = [200, 500];
p2 = [500, 200];
t = (time - inPoint) % fd;
if(t <= d) linear(t, 0, d, p1, p2);
else linear(t, d, fd, p2, p1); 

This starts from p1 at the beginning of the layer and runs to p2 over 5 seconds, then goes back to p1 and begins again. If you want the animation to ease in to each point you could use the ease() function instead of linear() providing the same arguments.


  • 1
    Hi and thank you for your contribution. Could you please fix the link to the Creative Cow website? Jan 31 '14 at 19:31

It's trickier than it seems if you want to do the whole thing from scratch.

//first make some shortcuts
//choose the property to drive it - could be any property on any layer
//choose the keys that define the movement - in AE key indexes start at 1, not 0
//more shortcuts
//period is the length of the cycle
//phase is the point in the cycle we're at
phase= Math.abs(Math.abs(time - key1.time) % (period*2) -period);
//avoid divide-by-zero errors
if ( period == 0 ) { //this shouldn't happen
} else {
    (pos1*(Math.abs(period-phase))+pos2* phase )/period;

Now what we're doing with phase is creating a value that goes from 0 to period and back again, and then making a weighted average of the two keyframes' values. It's easier to understand if you say there are 4 frames in the cycle and the first keyframe is at zero.

  • At frame 1, phase is abs(abs(4-1) % 8 - 4) = 1 (the % is the modulo operator, aka the remainder). When we substitute it into the last expression, the result is (pos1 * (4-1) +pos2 * 1)/4 or to expand it 3/4*pos1 + 1/4*pos2 - so basically pos1 is influencing the position 3 times as much as pos2.
  • At frame2: phase=2 and it works out to 2/4*pos1 + 2/4*pos2, so half and half, so it will be the average of the two keyframes.
  • At frame 3 phase=3, so it's 1/4*pos1 + 3/4*pos2

The second expression is basically doing the job of the built-in linear() interpolation method. Once time > period, then the abs() function kicks in and phase heads back to the original value.

You could also use the phase variable with the valueAtTime property of the position property: transform.position.valueAtTime(phase), if you wanted a complex motion path. that way it would shuttle backward and forward along the motion path between the two keyframes.

If you don't want no steenkin' keyframes the first few lines could be replaced thus:

pos1=[123,456]; //or for a 3d layer [123,456,789]
phase = Math.abs(Math.abs(time - startTime) % (period*2) -period);

The benefit of using keyframes is that you couple the power of expressions with the ease of the GUI.

I can see the value in this as a learning exercise (and a couple of glasses of Islay single malt this evening made it extra challenging), but one has to ask: why not use the built-in loopOut() expression? It probably will run much faster.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.