#### Chunk's Favourite After Effects Expressions

Expressions really open up After Effects to another level, and there’s just some things that you couldn’t do otherwise without a little bit of code to help out.

Below is a list of our favourite expressions in not particular order that we have found online at some point or another.

#### Large Number Counter with Prefix and Suffix Support

The Slider Control in After Effects will only go so high (1,000,000), so in comes the Angle Control to save the day.

All you need on your text layer is an “Angle Control”, and you’ll be keyframing the full rotation number. In the expression itself, you can add to the textBefore and the textAfter variables to include any text you like.

```
number = Math.round(effect("Angle Control")("Angle")/360);
textBefore = "";
textAfter = "";
n="" + number;
s="";
for(i=0, l=n.length; i<l; i++){
if(s && s!="-" && (l-i)%3 ==0)
s+=",";
s += n[i];
}
textBefore+s+textAfter;
```

#### Looping a Path Property

The classic loopOut expressions sadly don’t work on Path properties, thankfully we have the expression below to help us out.

```
try{
timeStart = thisProperty.key(1).time;
duration = thisProperty.key(thisProperty.numKeys).time-timeStart;
pingPong = false; //change to true value if you want to loop animationn back & forth
quant=Math.floor((time-timeStart)/duration);
if(quant<0) quant = 0
if(quant%2 == 1 && pingPong == true){ t = 2*timeStart+ (quant+1)*duration - time;
}
else{
t = time-quant*duration;
}
}
catch(err){
t = time;
}
thisProperty.valueAtTime(t)
```

#### Looping Wiggle Expression

If you want your wiggle to be a touch more predictable and loop after a set amount of seconds, then below is for you!

```
freq = 1;
amp = 110;
loopTime = 3;
t = time % loopTime;
wiggle1 = wiggle(freq, amp, 1, 0.5, t);
wiggle2 = wiggle(freq, amp, 1, 0.5, t - loopTime);
linear(t, 0, loopTime, wiggle1, wiggle2)
```

#### Maintain Stroke Width

Putting this on your stroke width property means that regardless of the layer’s scale, the stroke will maintain the same thickness. Thanks to Ben Marriott and Adam Plouff for this code!

`value / length(toComp([0,0]), toComp([0.7071,0.7071])) || 0.001;`

#### Maintain Scale when Parented

Thanks to Ben Marriott for this one, this expression when applied to the Scale property, maintains the layer’s original scale even while it’s parent’s scale changes. Check out Ben’s video here for a great example on how to use it.

```
s = [];
ps = parent.transform.scale.value;
for (i = 0; i < ps.length; i++){
s[i] = value[i]*100/ps[i];
}
s
```

#### Animating Frequency in a Wiggle

Oh boy, this one is a life saver. If you’ve tried to animate the frequency of a wiggle before, you’ll find out that After Effects likes to flip the table. MotionScript has an amazing writeup that I found when I was hunting a solution that really breaks down everything there is to the Wiggle function.

To use this, you’ll need a Slider Control (which represents your frequency amount) and apply this expression below to the property you wish to wiggle. Keyframe the slider to your heart’s desire and everything will function as expected.

```
freq = effect("Slider Control")("Slider");
amp = 10;
n = freq.numKeys;
if (n > 0 && freq.key(1).time < time){
accum = freq.key(1).value*(freq.key(1).time - inPoint);
for (i = 2; i <= n; i++){
if (freq.key(i).time > time) break;
k1 = freq.key(i-1);
k2 = freq.key(i);
accum += (k1.value + k2.value)*(k2.time - k1.time)/2;
}
accum += (freq.value + freq.key(i-1).value)*(time - freq.key(i-1).time)/2;
}else{
accum = freq.value*(time - inPoint);
}
wiggle(1,amp,1,.5,accum)
```

#### Loop In and Out Continuously

Thanks to QuickLoop for this expression, it allows your property to loop both before and after your keyframes.

`loopIn("Cycle") + loopOut("Cycle") - value;`

#### Wiggle on One Axis

Dan Ebbert has a sweet little write up regarding this, and it essentially boils down that when you use the wiggle expression, it returns an array of values dependent on the property. For rotation, it’ll return one value, but when it comes to scale or position, it’ll return two, affecting both axises.

To affect just one axis, we have to split it open and apply it to the specific axis we want. Below is an example for just affecting the X-axis.

```
w = wiggle(2,50);
[w[0],value[1]]
```

To make it affect just the Y-axis, we just switch the position of the ‘value’ and ‘w’.

```
w = wiggle(2,50);
[value[0],w[1]]
```

When it comes to 3D position and scale, it introduces a third array entry. Here’s an example of affecting just the Z-axis.

```
w = wiggle(2,50);
[value[0],value[1],w[2]]
```

#### Overshoot

A classic expression. Apply this to a property for automatic, math driven overshoot. Adjust the Amplitude (amp), Frequency (freq), and Decay to achieve the desired results.

```
amp = .06; // amplitude
freq = 2; // frequency
decay = 7; // decay (the more the value, the faster oscillations fade)
//----------------------------------
n = 0;
if (numKeys > 0){
n = nearestKey(time).index;
if (key(n).time > time){
n--;
}
}
if (n == 0){
t = 0;
}else{
t = time - key(n).time;
}
if (n > 0){
v = velocityAtTime(key(n).time - thisComp.frameDuration/10);
value + v*amp*Math.sin(freq*t*2*Math.PI)/Math.exp(decay*t);
}else{
value;
}
```