| A Creative COW After EffectsExpressions Tutorial |

|
Trent Armstrong
Allen, TX USA
©Trent Armstrong and CreativeCow.net
|
Article Focus:
Creative Cow Contributing Editor Trent Armstong shows you how to save time and money using Expressions to create an otherwise difficult effect: animating the visibility of a lens flare as it passes behind objects -- including ones that aren't completely opaque.
|
Typically in After Effects, the only ways to get a lens flare to seem like it's disappearing behind an object were to hand-animate the brightness values or drop some serious cash (or credit) on the Knoll Lens Flare filter.
With the release of AE 8 (CS3) there is a new expression method that, when used creatively, can achieve the desired result with very little effort. This method is sampleImage(). There is a great explanation of this method on Dan Ebbert's expression and scripting site www.motionscript.com.
(A special thanks to Dan for his help in fine tuning the expression I'm using in this tutorial)
The Goal
The end goal for this tutorial is to have a lens flare that seems to fade as clouds pass in front of the scene's imaginary distant light.
Click on the image below to play the finished movie.

And we can do all this with just a few Expressions.
The instructions are easy to follow, but you can also right-click/control-click here to download the finished project.
Seting up the Compositions
Create Comp: Clouds with Lens Flare
640x480
Square Pixels
15:00 sec
Create Comp: Clouds Precomp
640x480
Square Pixels
15:00 sec
Make the Clouds
In "Clouds Precomp" create a Solid:
White Solid
Color: White
Make Comp Size
Apply the Fractal Noise filter to the White Solid:
¨

Cut the Clouds out for the Alpha Channel
In "Clouds with Lens Flare" Comp:
• Drop in "Clouds Precomp" and "White Solid."
¨
• Rename "White Solid" to "Clouds with Alpha"
• Turn off the eyeball for "Clouds Precomp".
• Apply the Set Matte filter (Effect>Channel Menu) to "Clouds with Alpha" and Take Matte From "Layer: 2. Clouds Precomp." Also, in the Use For Matte drop down menu, select Luminance.

What's going on here?
This takes the black, white, and grey values from "Clouds Precomp" and uses them to cut the shape of the clouds into the "Clouds with Alpha" solid layer. And it gives us an alpha channel to work with. |
Add the Lens Flare
In the "Clouds with Lens Flare" Comp:
• Drag another instance of "White Solid" to be the top layer in this Comp.
• Rename this layer to "Lens Flare"
• Turn it into an adjustment layer by checking the adjustment layer box (the half-white-half-black circle).

• Add the Lens Flare effect to the "Lens Flare" solid.

What's going on here?
Turning the layer "Lens Flare" into an adjustment layer takes all effects applied to this layer and applies them to every layer underneath. |
NOTE: Without anything in the background the Lens Flare effect is only applied to the "Clouds with Alpha" layer. It's necessary to have something else like another solid behind the "Clouds with Alpha" layer to see any results.

• Drag another instance of the "White Solid" into the "Clouds with Lens Flare" Composition and place it underneath the "Clouds with Alpha" layer.
• Rename this new layer to "Sky".

• Apply a Fill effect to the layer and make it a nice sky blue.
¨
This is what you should see.

The Payoff!
Creating The Control and The Expressions
• Create a NULL. This will be our control for the lens flare in two ways. It will be an easy way to animate the position of the Flare, and it will be the location of where we examine the color data of the image.
Expression 1 - Flare Position
• Twirl down the effects properties of the "Lens Flare" layer and then twirl down the Lens Flare effect to reveal the properties "Flare Center" and "Flare Brightness".

• Option+Click the stopwatch for "Flare Center" (CTL+Click on Windows) to add an expression.
• Pickwhip to the position of "Null 1"

It should look like this ...

... now when you change the position of "Null 1", the flare will follow.
Expression 2 - Flare Position
• Now, Option+Click the stopwatch for "Flare Brightness" (CTL+Click on Windows) to add an expression here, as well.
• Copy and Paste the following into the Expression Field -
target = thisComp.layer("Clouds with Alpha");
x = target.sampleImage(thisComp.layer("Null 1").transform.position, [3.5,3.5], true, time);
alpha = x[3] * 100;
linear(alpha, 0, 36, 80, 20)
Experiment!
Now select "Null 1" and drag it around the screen. You'll see that the Flare appears to go behind the clouds as it dims and brightens dynamically.
Here's an example of how this effect can work with a little variation. Click the image below to play.

What's happening here???
The expression target.sampleImage takes a look at the the Layer "Clouds with Alpha" and stores the Red, Green, Blue, and Alpha pixel data. The numbers [3.5,3.5] sample an area that is 7 pixels by 7 pixels and averages the information.
alpha = x[3] takes only the Alpha value from the sample and gives it to the next line -
linear(alpha, 0, 36, 80, 20)
which then takes the first two numbers (0 and 36) and creates a relationship between those numbers and the last two numbers (80 and 20) where 0 (totally opaque) means that the Lens Flare will be 80% bright and 36 (or 36% transparent) equals 20% bright. |
RECAP: When there is no alpha information, the flare is at its brightest (80%) and will go no further towards transparent than 20% when the alpha information reaches and goes farther than 36%.

What if I don't want clouds?
Of course, if you were interested in the flare going completely away like it is being totally obscured by something, that last line of the expression would look something like this -
linear(alpha, 0, 100, 100, 0)
Now that you have your "Clouds Precomp" composition you can put anything in there you want (Text, Planets, Silhouettes) and watch the Lens Flare dynamically obscure where something appears to go in front of the "light." Click the image below to play.

BONUS!
I'm including an extra project with this tutorial for your viewing pleasure. Right-click/control-click to download.
If you found this page from a direct link, please visit our forums or read other articles at CreativeCOW.net
|