The Expressions Cheat Sheet
Loading...

Looping wiggle does not exist in After-Effects, this is a great chance to use wiggle and other functions After-Effects offers to create our own.
The result would look something like this:

// properties you can change
var loopDuration = 1;
var frequency = 1;
var amplitude = 110;


// looping wiggle calculation
var t = time % loopDuration;
var wiggle1 = wiggle(frequency, amplitude, 1, 0.5, t);
var wiggle2 = wiggle(frequency, amplitude, 1, 0.5, t - loopDuration);
linear(t, 0,  loopDuration, wiggle1, wiggle2)

At the top, we have our three main parameters that we can manually set in order to determine how long it takes for our loop to restart, as well as how frequent or how much our layer will wiggle.

What is this long expression? What is going on there?

To create a looping wiggle we are blending between two symmetrical versions of the wiggle() function, using the linear() function.
This lets us end up exactly where we started, then repeat.

Creating our own function

Another cool thing we can do is to convert this code into a function and give it a name that's easy to understand.

function loopingWiggle(frequency, amplitude, loopDuration){
	var t = time % loopDuration;
	var wiggle1 = wiggle(frequency, amplitude, 1, 0.5, t);
	var wiggle2 = wiggle(frequency, amplitude, 1, 0.5, t - loopDuration);
	return linear(t, 0,  loopDuration, wiggle1, wiggle2)
};

Notice that pasting this into After-Effects will result in an error. Why?
That's because functions are like templates, they are sitting there and waiting for us to use them.

Think about the original wiggle expression, somewhere behind the scenes there is a function called wiggle, waiting for you to use it.
We created a very similar function called "loopinWiggle" which takes the same two parameters "wiggle" takes (frequency and amplitude) but then also takes a third parameter, the duration of the loop.
We call it in a similar way we call wiggle, at the end of our code.
Our final expression should look like this:

function loopingWiggle(frequency, amplitude, loopDuration){
	var t = time % loopDuration;
	var wiggle1 = wiggle(frequency, amplitude, 1, 0.5, t);
	var wiggle2 = wiggle(frequency, amplitude, 1, 0.5, t - loopDuration);
	return linear(t, 0,  loopDuration, wiggle1, wiggle2)
};

loopingWiggle(2,5,10); // calling the function

Why a function?

Creating your own functions helps you better understand that expressions in After-Effects are not some random magic.
Now we know that the original wiggle() expression is a function itself.
You don't see its source code because it's built into After-Effects, but it's there, somebody wrote it and it's waiting for you to call it.
You can do the same with functions you make on your own, and that's simply really cool!
Learn more about Functions