After Effects Expressions

Resources:

Dan Ebberts

Ae expressions 

Motion Graphics Exchange

[ Open All | Close All ]

1] Numbers:
Add a slider and name to 'Value'
places = 3; //number of decimal places desired
val = effect("Value")("Slider"); //sub in the name of your slider here
factor = Math.pow(0.1, places) ;
sign = "";
if (val < 0) sign = "-";
val = Math.abs(val);
whole = Math.floor(val);
fraction = "" + Math.round((val - whole)/factor)*factor;
for (fraction; fraction.length < places+2; fraction+="0");
sign + whole + "." + fraction.substring(2, places+2).

2] Position from Layer above:
thisComp.layer(index - 1).transform.position

3] Number of decimal places:
//--Begin Expression
nums = thisComp.layer("Deep Lime Green Solid 1").effect("Slider Control")("Slider");
numOfPlaces = 3;
//--Do not modify below this line
function roundTo(number, places)
{ num = Math.round ( Math.pow(number, places) );


4] Number Rounding:
myNumber = 23.3453255243697978;
round(myNumber, 2);
function round(aNum, dP) {       return(Math.round(aNum*Math.pow(10, dP))/Math.pow(10, dP));
} num /= Math.pow(10, places);
return num; } roundTo(nums, numOfPlaces)
//--End expression

5] Round Decimal:
Math.round(your_decimal_number_here)
//--End expression

6] Random Movement:
//Returns two values by default; [x-pos. y-pos]
segDur = .5;// duration of each "segment" of random motion
minVal = [0.1*thisComp.width, 0.1*thisComp.height];
maxVal = [0.9*thisComp.width, 0.9*thisComp.height];
seed = Math.floor(time/segDur);
segStart = seed*segDur;
seedRandom(seed,true);
startVal =  random(minVal,maxVal);
seedRandom(seed+1,true);
endVal = random(minVal,maxVal);
ease(time,segStart,segStart + segDur, startVal, endVal);




7] Time Output:
//Contains rounding + always display 2 digits

min = Math.floor(time);
strMin = String(min);
if(min < 10)
{strMin = '0' + strMin;}

sec = Math.round(((time) - min)*100);
strSec = String(sec);
if(sec < 10) { strSec = '0' + strSec;}
"18:22:" + strMin + ":" + strSec





8] Looping:
//Looping Script
freq = 0.6;
amp = 5;
loopTime = 10;
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)






9] Time Output:
//Contains rounding + always display 2 digits

min = Math.floor(time);
strMin = String(min);
if(min < 10)
{strMin = '0' + strMin;}

sec = Math.round(((time) - min)*100);
strSec = String(sec);
if(sec < 10) { strSec = '0' + strSec;}
"18:22:" + strMin + ":" + strSec





10] Random Position HOLD:
holdTime = .5; //time to hold each position (seconds)
minVal = [0.1*thisComp.width, 0.1*thisComp.height];
maxVal = [0.9*thisComp.width, 0.9*thisComp.height];
seed = Math.floor(time/holdTime);
seedRandom(seed,true);
random(minVal,maxVal)







11] 2D track to 3Dlight or Null :
thisComp.layer("Light 1").toComp([0,0,0]);
change “Light 1” to any layer you want a track to.








12] Smooth Keyframes:
smooth(width = .2, samples = 5, t = time)








13]Inertial Bounce :
// Inertial Bounce (moves settle into place after bouncing around a little)
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);
amp = .05;
freq = 4.0;
decay = 2.0;
value + v*amp*Math.sin(freq*t*2*Math.PI)/Math.exp(decay*t);
}else{
value;
}









14] Fade in / Fade Out :
// - Scale for length of layer Try this for opacity:
rampTime = 1;
if (time < in_point + rampTime){
linear(time,in_point,in_point + rampTime,0,100)
}else if (time < out_point - rampTime){
100
}else{
linear(time,out_point - rampTime,out_point,100,0)
}
// And this for scale:
startScale = [20,20];
endScale = [100,100];
linear(time,in_point,out_point,startScale,endScale)







15]Creating Trails :
Tip from Dan Ebberts

delay = 5; //number of frames to delay
d = delay*thisComp.frameDuration*(index - 1);
thisComp.layer(1).position.valueAtTime(time - d)
OR
thisComp.layer(index+1).transform.position.valueAtTime(time-.1)








16]Smooth Wiggle :
Tip from Adobe Message Board

seedRandom(time*5);
wiggle(0,100);







17]Copy size and rescale from layer above :
thisComp.layer(index - 1).scale-[7,7]






18]Copy rotation from layer below and rotate 22.5 :
thisComp.layer(index+1).transform.rotation+22.5






19]Copy position from layer above :
thisComp.layer(index-1).position






20]Time delay from layer above:
thisComp.layer(index+1).transform.position.valueAtTime(time-.1)






21]Autofade :

//Autofade: Add to opacity


transition = 20;       // transition time in frames
if (marker.numKeys<2){
tSecs = transition / ( 1 / thisComp.frameDuration); // convert to seconds
linear(time, inPoint, inPoint + tSecs, 0, 100) - linear(time, outPoint - tSecs, outPoint, 0, 100)
}else{
linear(time, inPoint, marker.key(1).time, 0, 100) - linear(time, marker.key(2).time, outPoint, 0, 100)
}






22]Wiggle in one direction only:
Wiggle Y Only:
[value[0],wiggle(1,100)[1]]

Wiggle X Only:
[wiggle(5,30)[0],value[1]]

Wiggle X and Y seperately:
[wiggle(5,30)[0],wiggle(1,100)[1]]






23]Depth of field on Layer :
1) Make sure you've got a camera in your composition.
2) Create either a solid or a null and name it "focusControl" (note: you can name your solid when you create it, but to change a null you have to select it in your comp window and hit the 'enter' key. You can then type a new name for it.)
3) Make the solid/null a 3d object.
4) Apply this expression to the "Focus Distance" parameter of your camera:

length(thisComp.layer("focusControl").transform.position, toWorld([0,0,0]))






24]Delay Light intensity :

//put on layer intensity under layer you want to follow



delay = 5; //number of frames to delay
d = delay*thisComp.frameDuration*(index - 1);
thisComp.layer(index -1).lightOption.intensity.valueAtTime(time - d)






25]Delay Light Colour :
put on layer color under layer you want to follow

delay = thisComp.layer("Null 1").effect("FRAME")("Slider"); //number of frames to delay
d = delay*thisComp.frameDuration*(index - 1);
thisComp.layer(index -1).lightOption.color.valueAtTime(time - d)






26]Pointing a positional value at a null and getting Comp space:

//This case specifically looks at a 2D null within a PreCOMP.


sourceNull = comp("MrCool_PreCOMP").layer("MrCool_Null");
adjPos = sourceNull.toComp([0,0]);
[adjPos[0],adjPos[1]];

//To get a 3D position value use:


adjPos = sourceNull.toComp([0,0,0]);
[adjPos[0],adjPos[1],adjPos[2]];







27]Making a layer (this layer) follow another layer (leader):
Tip from Dan Ebberts

//Making a layer (this layer) follow another layer (leader).
Note: restLength keeps it from ever matching the position exactly.


restLength = 10;
damp = .95;
leader = thisComp.layer("leader");
fDur = thisComp.frameDuration;
currFrame = Math.round(time / fDur);
p2 = position.valueAtTime(0);
v2 = 0;
for (f = 0; f <= currFrame; f++){
  t  = f*fDur;
  p1 = leader.transform.position.valueAtTime(t);
  delta = p2 - p1;
  nDelta = normalize(delta);
  a = 2 * nDelta * (length(delta) - restLength) * fDur;
  v2 = (v2 - a) * damp;
  p2 += v2;
}
p2


28]Skip Frames (every 2nd frame) expression to timeremap:

nthFrame = 2;
fps = (1.0/thisComp.frameDuration) / nthFrame;
posterizeTime (fps); value






+ QUESTION 1

REPLY 1

+ QUESTION 2

REPLY 2

+ QUESTION 3

REPLY 3