## Feb 8, 2016

### Expressions Library

Welcome! Here you'll find pre-written expressions that you can copy into your projects. I've done my best to explain how they work! I'll update this post as often as I find new and useful expressions.

And remember to Save before applying.
You have been warned!

New to expressions? Check out the free AE Expressions e-book here. There's also plenty of tutorials you can follow to practice or learn new things.

# Index

• Counting Numbers with Commas
• Counting Numbers with Flexible Options
• Automatic Fade In & Out
• Layer Opacity Follows Parent
• Wiggle at Specific Time
• Stepper
• Stepper Respecting Keyframes
• Looping Wiggle
• Alternating Text Animator
• Tie Animation to a Slider
• External Sites
LAST UPDATE: 13.01.2017

Expression segments highlighted:
like this are variables that you can use to control the behavior of the expression and can be replaced with Expression Controls reference to make controlling them easier.
like this are numbers that are likely causing the problem you're having. Try tweaking it!

# Counting Numbers with Commas

1. Create a Text layer.
3. Add this expression to the "Source Text" property:
var num = Math.floor(effect("Slider Control")("Slider"));
num = Comma(num);
[num]
function Comma(number)
{
number = '' + Math.round(number);
if (number.length > 3)
{
var mod = number.length % 3;
var output = (mod > 0 ? (number.substring(0,mod)) : '');
for (i=0 ; i < Math.floor(number.length / 3); i++)
{
if ((mod == 0) && (i == 0))
output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
else
output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
}
return (output);
}
else return number;
}
4. Animate the Slider Control.

Parenting a Slider to a text is trivial stuff, the first line of this expression takes care of it, adding commas for thousands is a whole other story.
I got it from this post on Video Copilot forums. There's different ways to achieve this depending on where you look, I found this worked nicely for what I needed it for.

# Counting Numbers with Flexible Options

Thanks to Sandeman for suggesting this in the comments.

The great master Dan Ebbert himself wrote it for me some years ago. With this expression, you can choose the number of decimals, the suffix and prefix you need (+,-, €, \$, C°, %, -> or whatever), and you can switch between dot, space or comma to write the numbers. [highlighted below] In Europe, we write like this : 1 256,55 € and in english language it's something like : €1,256.55

1. Create a new text layer.
3. Apply this expression to "Source Text":

`numDecimals = 2; // number of decimals`
beginCount = effect("Slider Control")("Slider");
endCount = effect("Slider Control")("Slider");
dur = 3;
t = time - inPoint;
s = ease (t, beginCount, endCount).toFixed(numDecimals);

prefix = "+"; // or replace by whatever you need
if (s == "-"){
prefix = "-";
s = s.substr(1);
}
suffix = " °C"; // change for what you need

decimals = "";
if (numDecimals > 0){
decimals = s.substr(-(numDecimals + 1));
s = s.substr(0,s.length - (numDecimals + 1));
}
outStr = s.substr(-s.length, (s.length-1)%3 +1);
for (i = Math.floor((s.length-1)/3); i > 0; i--){
outStr += "," + s.substr(-i*3,3); // choice between dot or space inbetween thousands and hundreds
}
prefix + outStr + decimals.replace(/./,",") + suffix;

4. Animate the slider control.

# Automatic Fade In & Out

1. Select the layer you want and reveal the Opacity settings.
2. Apply this expression to it:

} else {

I use this expression a lot to quickly fade text in & out, especially useful when doing Kinetic type on a short deadline. This can be ported to any other property, just remember to add the correct arrays for to replace the "0" and some extra calculation.
For example, to set this on Position and make the layer move upward, replace the "0"s with "value+[0,100]".

This expression uses "linear" to fade the opacity of the layer in and out. There's 2 at work here, for the beginning of the layer, which checks inPoint, and for the outro, which checks outPoint. The if that wraps around them is comparing the time to inpoint+fadetime, which is *after* the fadein is supposed to happen. This keeps the second expression from triggering, the 1.2 acts like a buffer in the case of short layers, but can be tweaked in case things look weird with really short layers.
Fun Fact: This expression is the base for i2c_TRANSIT.

# Layer Opacity Follows the Parent

1. Select the layer's Opacity and apply this:

parent.opacity
Simple enough. Sometimes you have a layer following another, but parenting doesn't usually carry over Opacity animations. This should fix that. Remember that Null's usually have 0 opacity when they're created, so don't fret if your layers disappear.
Referencing parent means as soon as that layer loses the connection, the expression will break and switch off until you manually enable it after parenting the layer back.

# Wiggle at Specific Time

1. In the property you want to wiggle, apply this:

startTime=1;
endTime=10;
wig=wiggle(1,200);
if(time > startTime && time < endTime) {wig} else {value};
2. Replace "startTime", "endTime" and "wiggle" with your own values or expression controls.

Wiggle affects a layer at all times, and sometimes you just want a little shake for a specific time period. Alternatively, you can just link a Slider Control to the wiggle's second parameter. Either way works.

# Stepper

1. Select a property with 1 dimension you want to apply this to:

`steps=10;`
exe=value/steps;
ex=Math.floor(exe)*steps;
[ex];

Use this for property with 2 dimensions:

`steps=10;ops=value`
exe=ops/steps; eye=ops/steps;
ex=Math.floor(exe)*steps;ey=Math.floor(eye)*steps;
[ex,ey];

2. Add keyframes to the property and watch it get stepped!

This is sort of like turning a smooth movement into a stepping sort of movement. It's good for cursors and tickers and quite useful to make a fractal noise animation choppier, or with some strict steps, can be used as a randomized on/off switch.

Math.floor will set everything to the right of the decimal point to 0. The exe step divides value, pushing the decimal point to the left by the number of steps you've set. exe gets rounded off by Math.floor and 'ex' then pushes the decimal back to the right by the number of steps.

So an original value starts at 2335.23,, goes through steps set to 100 to become 23.35, math.floor makes it 23.0, then steps comes back to make it 2300.00, effectively rounding off the original 2335.23.

This allows you to move around where the zeros are by setting steps to 10s or 100s, but numbers in between, like 50,25, also work to produce smaller steps. Play around with steps as it varies with the range the keyframes are covering.

steps ssets the range of movement.
ops=value can be replaced with wiggle(), time equations, or can remain as 'value' to be used with keyframes.

# Stepper Respecting Keyframes

1. Used in a position property:

`steps=50;`
exe=value/steps;ex=Math.floor(exe)*steps;
eye=value/steps;ey=Math.floor(eye)*steps;
if(transform.position.speed > 0) {[ex,ey]} else {value};

The original stepper had one problem: although it followed keyframes, it would never end at the right place due to the rounding off. My keyframes end at [243,250], but the formula is rounding them to [200,200], which is waaaay out of place. In this case, I only wanted it to round off while animating and stop at the right place.
eye and exe are just as before, what happens after changes.

if checks the speed of position, which is what this is set to. If the speed is greater than 0, meaning it's moving, it uses the "stepper" expression. Otherwise, when the speed is less than 0, use the value of this property which is the keyframe. There's a bit of a loop going on here. The expression is receiving it's own result while calculating the speed at each frame. I expected an issue from this reference, but it seemed to work.

transform.position needs to be changed to match the layer this expression is set to. For some reason, AE cannot check value.speed

## Looping Wiggle

`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)

A brilliant solution by Dan Ebberts, this expression blends 2 time-offset wiggles to create a looping wiggle.
Original article here by Dan Ebberts

# Alternating Text Animator

1. Add a new Text Animator (Works best with Y Position)
3. Apply this expression to the amount:

if(textIndex % 2 > 0){selectorValue}else{-selectorValue}

This one is pretty interesting and covered near the end of the Text Animators tutorial. Keep in mind that "selectorValue" is the amount of the animator that is applied to the text. So, this makes it only work with position since this is the only one who's negative value is not completely destructive.

# Tie Animation to A Slider

1. Create a new Null object
3. Apply this expression to any property in the same comp:

`animator=thisComp.layer("Null 2").effect("Slider Control")("Slider");`
linear(animator,0,100,key(1).value,key(2).value);

4. Apply two keyframes to the property. Their time doesn't matter, only the value.
5. Use the Slider in the null to animate the property.

This one is pretty interesting method that I used for some mouth animations. This basically animates the values of the keyframes from the values of 0-100 of the slider. Conveniently, if you expand the slider you'll get the interactive 0-100 control and you can play around with it.