(Page 2 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 BREAKDOWN:
1) fadeTime = 10;
2) opacityMin = 0;
3) opacityMax = 100;
4) layerDuration = outPoint - inPoint;
5) singleFrame = thisComp.frameDuration;
6) animateIn = linear(time, inPoint, (inPoint + framesToTime(fadeTime)), opacityMin, opacityMax);
7) animateOut = linear(time, (outPoint - framesToTime(fadeTime+1)), (outPoint-singleFrame), opacityMax, opacityMin);
8) if(time < (layerDuration/2+inPoint)){ <––CODE UPDATED 01/28
9) animateIn;
10) }else{
11) animateOut;
12) }
Line 1: We set a variable called fadeTime and assign it the amount of frames we want the fade to last.
fadeTime = 10;
Line 2: We make a variable called opacityMin and assign it the minimum opacity we want, which is 0.
opacityMin = 0;
Line 3: We make a variable called opacityMax and assign it the maximum opacity we want, which is 100.
opacityMax = 100;
Line 4: We create another variable called layerDuration to hold the total length in time of our layer. We do this by subtracting the inPoint value from the outPoint value.
layerDuration = outPoint - inPoint;
Line 5: We make a variable called singleFrame to hold the value of the current comp’s frameDuration.
singleFrame = thisComp.frameDuration;
Line 6: This variable called animateIn uses the linear() method to calculate our overall fade value and when it happens. Hopefully with the linear() explanation on the previous page and the video just below, this should start to make a little more sense.
- Our first argument for linear() is time, this is what we are sourcing as our master input, the timeline’s current time.
- The second argument is our layer’s in point.
- The third argument is our layer’s in point plus our ten frame variable, fadeTime. You’ll notice here, that we place that variable in our framesToTime() method to convert our value into seconds. Without this conversion After Effects would think that we wanted to add 10 seconds, not 10 frames. We also surround the entire equation with parenthesis, (equation), to help both organize the code and tell the computer that this is a self contained equation.
- The fourth argument is our starting opacity value variable, opacityMin. For our fade in, we start at 0.
- The fifth argument is our ending opacity value variable, opacityMax. For our fade in, we finish at 100.
animateIn = linear(time, inPoint, (inPoint + framesToTime(fadeTime)), opacityMin, opacityMax);
Line 7: This variable called animateOut uses the linear() method to calculate our fade value and when it happens. This may look repetitive, but pay close attention to what’s happening for our fade out because values are flipped.
- Our first argument for linear() is time, identical to our animateIn setup.
- The second argument is our layer’s out point. We also do the framesToTime() conversion, but we are also adding one frame to our fade out. The reason for this one frame offset is that After Effects gives us the literal out point of a layer, the very edge that you see in the timeline when in fact the actual visual out of a layer is on the last frame, so we add this one frame to compensate for that.
- The third argument is our layer’s out point and we have to subtract a single frame here to offset for the same reason as above.
- The fourth argument is our starting opacity value variable, opacityMax. For our fade out, we start at 100, to match the value we faded up to with animateIn.
- The fifth argument is our ending opacity value variable, opacityMin. For our fade out, we finish at 0.
animateOut = linear(time, (outPoint - framesToTime(fadeTime+1)), (outPoint-singleFrame), opacityMax, opacityMin);
Line 8,9,10,11,12: CODE UPDATED 01/28 For the if/else statement we are checking to see which half of the layer we are reading in our timeline. If you were to cut the layer in half vertically, you would have a left(or first half) and a right(or second half) to your layer. We setup an equation taking the total length of time our layer exists, variable layerDuration, and divide that by 2 using the backslash. (layerDuration/2+inPoint) This gives us the middle of the layer time wise. We then check to see if time is less than that value. If it is less than, then we know we are on the left side of the layer and we should run animateIn. If the value of time is greater than, we know we are now on the right side of the layer and we should run animateOut. Hopefully that makes sense.
if(time < (layerDuration/2+inPoint)){
animateIn;
}else{
animateOut;
}
I hope this explains this code clearly enough. Not knowing Javascript can make this stuff frustrating, believe me I know, but I hope this code breakdown made that process a little less painful for you. If you have questions or feedback, please leave comments and I will try to reply as best I can.
(Page 2 of 2 pages for this article < 1 2)
You must be registered to comment. This is an effort to reduce spam. Please REGISTER HERE.
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
|