(Page 1 of 2 pages for this article 1 2 >)
Sunday, January 22, 2012
Expression Shorts - Auto Fade Layer
David Torno | 01/22
Automatically fade a layer up and down without keyframes.
(CODE UPDATED 3/25 See article for updates) Learn how to fade a layer up and down without using keyframes. The cool part is that you can change the in/out points of your layer and it’ll adjust accordingly.
The expression code on this one is a bit more involved, but I give a full breakdown below the video tutorial. Enjoy.
SOURCE CODE: (gets applied to the Opacity property of a layer)
fadeTime = 10;
opacityMin = 0;
opacityMax = 100;
layerDuration = outPoint - inPoint;
singleFrame = thisComp.frameDuration;
animateIn = linear(time, inPoint, (inPoint + framesToTime(fadeTime)), opacityMin, opacityMax);
animateOut = linear(time, (outPoint - framesToTime(fadeTime+1)), (outPoint-singleFrame), opacityMax, opacityMin);
if(time < (layerDuration/2+inPoint)){ <––CODE UPDATED 03/25
animateIn;
}else{
animateOut;
}
The Auto Fade expression we have here is definitely confusing to look at, but I hope to help you understand it better. We are dealing with some new terms in this code, we have inPoint, outPoint, frameDuration, framesToTime(), linear() and an if/else statement. We are gonna be learning some very hefty expression code today my friends, but it is all worth it.
inPoint - The inPoint is pretty self explanatory. It retrieves the in point of a layer.
outPoint - The outPoint is pretty self explanatory. It retrieves the out point of a layer.
frameDuration - frameDuration is used to retrieve the duration of time for a single frame. Basically a single frame written as seconds, this is the native method of how After Effects reads time behind the scenes. So one frame in a 23.976 fps comp is equal to 1 frame or 0.04170837504171 seconds. Crazy I know, but that’s how it works. We will need the value of one frame later on.
framesToTime() - A very handy conversion method that does exactly what it says, it converts a frame value to a time value. So if we were to write framesToTime(1), we would get that same seconds value mentioned above, 0.04170837504171. For our purpose we will be converting our numbers from different variables so all the math works out properly in the end.
linear() - linear() is a handy bit of code when you are wanting to map a value to another value over time. There are two versions of this code, the one we are using requires five input values, like so…
linear(time, tMin, tMax, value1, value2);
time: Is the master source of information. You can use time, a layer’s position, rotation, opacity, etc…
tMin: The minimum value you want to use from the master source, time.
tMax: The maximum value you want to use from the master source, time.
value1: The start value that maps to the tMin value.
value2: The finish value that maps to the tMax value.
if(){}else{} - if/else statement runs a comparison of values and then runs different code depending on weather or not that comparison is true or false. Simply put “if” a value is true, run this code, “else” run this other code. Another annalogy would be, if I have my keys, I can drive my car, otherwise if I don’t have my keys, I can’t drive my car. The formating of an if/else statement can vary, but they all do the same thing. Here are three examples.
Version 1:
if(I have my keys){
I can drive my car;
}else{
I can’t drive my car;
}
Version 2:
if(I have my keys)
{
I can drive my car;
}
else
{
I can’t drive my car;
}
Version 3:
if(I have my keys)
I can drive my car;
else
I can’t drive my car;
Version 4:
(I have my keys) ? I can drive my car : I can’t drive my car;
Versions 1 and 4 are my favorite to use. 1 is easy to read, plus properly contained with the curly braces and 4 is just plain faster to write. On the next page I have a full breakdown of the expression code.
(Page 1 of 2 pages for this article 1 2 >)
You must be registered to comment. This is an effort to reduce spam. Please REGISTER HERE.
Thanks for an interesting post. I’ve just gotten AE and have a lot to learn. I’m a programmer by trade so even though this is my first exposure to AE expression code, I felt pretty comfortable.
I look forward to trying some of this out.
Posted by Rob on 01/23 at 12:57 PM
hi david,
thank you for a great post and a really useful expression. there seems to be a little problem though: on layers with short durations late on in the timeline (e.g. in-point at 4 sec, duration 1 sec), the layer’s transparency value at the in-point will already be at 100% instead of fading in. to fix this, i suggest a change of the line
if(time < (layerDuration/2)){
to this:
if(time < (layerDuration/2+inPoint)){
this worked perfectly well for me.
btw i also tried using the script on different properties (like rotation or single position) - working great as well.
thanks again!
Posted by .(JavaScript must be enabled to view this email address) on 01/25 at 10:43 AM
Nice catch e.s., I hadn’t dealt with such short layer lengths when using this previously, so I didn’t catch that. I will update the text to reflect this.
Posted by David Torno on 01/28 at 06:55 PM
The updated code has a slight typo that makes it error. There is a curly bracket missing.
Needs changed to:
if(time < (layerDuration/2+inPoint)){
Posted by Enhanced Dimensions on 03/19 at 08:09 AM
Ah, my bad. It got removed during the last update. Thanks Enhanced Dimensions.
Posted by David Torno on 03/25 at 08:25 PM
|