This is a collection of some of my After Effects Expression scripts. Along with shooting and production, I have a background in software and web development. I believe in making the computer do as much work as possible because when it comes to tedius minutiae I am inherently lazy. My only gripe with After Effects Expressions is you don't have access to the complete object tree (like you do in ActionScript) but are generally limited to what is within a comp. For example, I would love to be able to write a class at the project level and control what happens to a text field buried several comps deep. Alas, the lamentations of a former Flash dev. But there are ways to write generic scripts that can save you a TON of time and prevent the laborious nudging of keyframe diamonds. Hopefully some of the examples on this page can help you out. Feel free to modify and use these in your projects.
This is a very useful script if you have a ton of lower 3rds (or some other graphical overlay) you need to fade in and out throughout your piece. You do not need to set any keyframes. The layer will fade in and out automatically based on the in/out points of the layer. Changing the length of the layer does not require any changes to the script, just set it and forget it.
//tween opacity in and out based on in/out points
var tweenFrom = 0;
var tweenTo = 100;
var tweenMode = 0;
if (time > inPoint && (time < outPoint - 1) )
{
tweenMode = 1;
}
else if (time > outPoint - 1)
{
tweenMode = 2;
}
if (tweenMode == 1)
{
[ease(time-inPoint, tweenFrom, tweenTo)];
}
else if (tweenMode == 2)
{
[ease(outPoint-time, tweenFrom, tweenTo)];
}
else
{
0;
}
Scales a layer to match a given pixel height. For example, you have a bunch of odd size images and you want them all to be 100 pixels tall. Set desiredHeight to 100 and the layer will be proportionally scaled to that height. Easily modifiable to accommodate width rather than height.
//scaling - scales to a specific height
desiredHeight = 540;
actualHeight = height;
percentageChanged = ( desiredHeight / actualHeight ) * 100;
newScale = 100 - (100 -percentageChanged);
[newScale, newScale];
Hide or show a layer based on a value within another layer's effect. In this example, we are checking the "Amount to Tint" value of a given layer within the Tint effect. You could easily substitute any other effect's property value here.
//automatically hide an object based on other layer's tint value
if ( thisComp.layer( "tile_" + name.substring( name.indexOf("_")+1 ) ).effect("Tint")("Amount to Tint") == 0 )
{
0;
}
else
{
100;
}
Like the first example for auto alpha, this basically mimics an additional keyframe somewhere in the middle of the clip.
//variation of alpha tween for background -- starts at 100, dips to 20 at 3 seconds, stays at 20, ends at 0
var keyframe2 = 3; //seconds after inPoint where you'd otherwise set a keyframe
var tweenMode = 0;
if (time > inPoint && (time < inPoint + keyframe2) )
{
tweenMode = 1;
}
else if ( time > inPoint + keyframe2 && time < outPoint - 1)
{
tweenMode = 2
}
else if (time > outPoint - 1)
{
tweenMode = 3;
}
if (tweenMode == 1)
{
[ease(time-inPoint, 0, 100)];
}
else if (tweenMode == 2)
{
[ease(time-inPoint-keyframe2, 100, 20)];
}
else if (tweenMode == 3)
{
[ease(outPoint - time, 0, 20)];
}
else
{
0;
}