Creative COW SIGN IN :: SPONSORS :: ABOUT US :: CONTACT US
LIBRARY: TutorialsVideo TutorialsReviewsInterviewsEditorialsFeaturesBusinessAuthorsRSS FeedTraining DVDs

Making Light Disappear & Reappear Behind Objects Using Expressions

COW Library : Trent Armstrong : Making Light Disappear & Reappear Behind Objects Using Expressions
Obscuring a Lens Flare with Expressions
A Creative COW After EffectsExpressions Tutorial


Obscuring a Lens Flare with After Effects Expressions

Trent Armstrong

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.

Clouds 1

 

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:

¨Fractal Noise


noise

 


Cut the Clouds out for the Alpha Channel
In "Clouds with Lens Flare" Comp:

• Drop in "Clouds Precomp" and "White Solid."

¨clouds precomp

• 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.

Click to play Clouds Movie

 

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.

Click to play Planet movie


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





  Digg it Digg itAdd Comment
Comments

Making Light Disappear & Reappear Behind Objects Using Expressions
Thank you very much Trent.
The way you descibed should work fine. It's simple enough to impliment, no need to rack your brains for a beter solution.

And to Robert,
I didn't literally want the objects to move infront of the lense flare, but rather to shadow the lense from a cetral light source. You'll understand how that was a problem when u read Trents solution.
by Laurence Rietdijk on Apr 15, 2008
Making Light Disappear & Reappear Behind Objects Using Expressions
I think the first way I would try is to build your 3D move in a precomp (which you have probably already done) and then duplicate it (3D Move Front and 3D Move Back).

In 3D Move Front set an expression for opacity that checks the Z position of the object and turns it off when it passes to the negative side of Zero.

Do the same for 3D Move Back except reverse the expression to turn the object ON when it passes Zero.

Example for Front:
Z = transform.position[2];
if (Z <= 0) 100 else 0

THEN, take both of those layers into the final comp with the lens flare and use the information from 3D Move Front only for to pull the alpha information for the lens flare brightness.

I'll post again if I think of another way.

Trent
by Trent Armstrong on Apr 14, 2008
In reply to Laurence Rietdijk...
I have been wracking my brain and for the life of me I cannot think of an instance where physics would allow a lens flare to occur while not obfuscating an object in the view of the lens. A lens flare is an activity of light behaviour happening *inside* the lens -- therefore everything in the focal field in front of the lens is going to have the flare laid over it.

Maybe I am missing something here but I cannot imagine an image with a flare behind something and the foreground object is not affected.
by Robert Reed on Apr 13, 2008
Making Light Disappear & Reappear Behind Objects Using Expressions
I know the answere will probably be completely different to the method you described; but how do you make a lens flare dissaper behind a 3d layer?

actually, my 3d-layers orbit the lens flare, which means they pass both infront and behind the flare.

too complicated?
by Laurence Rietdijk on Apr 12, 2008
cow starcow starcow star
Making Light Disappear & Reappear Behind Objects Using Expressions
Great! I got it all set up. Now is there a tutorial on how to actually animate the clouds moving by like in the example movie clip at the top of the page?
by Patrick Miller on Mar 9, 2008
Making Light Disappear & Reappear Behind Objects Using Expressions
Katie,

Thanks for the question. You'll need to precomp your layer that is moving. The expression looks at the width and height of the layer and compares that to where the Null is in relationship to the width and height of the composition.

That means if you move the layer 100 pixels left, the expression is still watching that layer as if it was in the same place.

Now, since you are using expressions, you can precomp your layer that you want to move. Then put a new Null in the final comp to be a position controller. In your precomp, use an expression in the position of the layer you want to move and link it to the position of the new Null in your final comp. That will keep the precomp in the correct place, but also give you the ability to remotely control the position of your precomped layer. If you need any more help with this, please let me know.

Incidentally, the clouds are precomped, and I think there is an expression that changes the "offset turbulence" of the fractal noise so the clouds appear to be moving.

Sincerely,
Trent
by Trent Armstrong on Feb 11, 2008
cow starcow starcow star
Making Light Disappear & Reappear Behind Objects Using Expressions
I got this expression to work when I move the null layer, but not when I move the layer with the image to go over the flare... the clouds movie on the tutorial page shows the clouds moving over the flare, and the flare stationary. But in my comp, and the one I downloaded from the tutorial page, this does not work. How can I make it so moving the clouds/trees etc will cover the stationary flare? Thanks so much!
by Katie Marcia on Feb 8, 2008
Making Light Disappear & Reappear Behind Objects Using Expressions
Travis,

Make sure you are using After Effects 8 (C3). The sampleImage() function doesn't work in previous versions of AE and will give you an error.

Cheers,
Trent
by Trent Armstrong on Jan 11, 2008
Making Light Disappear & Reappear Behind Objects Using Expressions
I seem to have the same problem as Justin.
I keep getting errors with the code
by Travis Kaney on Jan 10, 2008
Making Light Disappear & Reappear Behind Objects Using Expressions
Shroomly,

You might need to precomp the tree. The effect works best when all of the layers used with the expressions are the size of the composition. Just put your tree in a new comp the size of your working comp and bring that new tree comp back in adjusting the expression accordingly. You might get an error when you bring the precomp in, but that is fixable by changing the name of the target comp in the expression.

Thanks,
Trent
by pxlguy on Oct 19, 2007
Making Light Disappear & Reappear Behind Objects Using Expressions
You can make this even simpler by using my CE PixelSampler, that you can find here:
http://library.creativecow.net/articles/drozda_jerzy/CEPixelSampler.php
by Maltaannon on Oct 14, 2007
cow starcow starcow star
Making Light Disappear & Reappear Behind Objects Using Expressions
thanx for the tutorial..very helpful but doesnt really work with everything...

i just tried making a lens flare go behind a tree with a black fill... it only obscures on some of its branches, and when putting it behind the main tree trunk, no editing of the expression actually makes the lens obscure... i dont know what to do to make it work.
by shroomly on Oct 6, 2007
Making Light Disappear & Reappear Behind Objects Using Expressions
Sorry to do this here, but ,Justin, would you contact me at trent@ignitermedia.com?

I have some questions for you.

Trent
by pxlguy on Sep 21, 2007
Making Light Disappear & Reappear Behind Objects Using Expressions
I have a quick question about this tutorial. When I set the code in on the flare brightness it says an error occurs on line 2. The error reads class ' layer' has no property or method named 'sky'.
Error occurred at line 2.
Expression disabled.
If you could help me out I would appreciate it.
by JustinG1983 on Sep 20, 2007
cow starcow starcow starcow starcow star
Thank you for illustrating a useful technique, Trent
This is a great technique, Trent. If you don't mind, I am going to rename the article from the original "Obscuring a Lens Flare with Expressions" to a title of "Making Light Disappear & Reappear Behind Objects Using Expressions". I think that more people will get the point and the technique will get the kind of attention it deserves. After all, many people see the words Lens Flares and their eyes begin to water. Used well, lens flares can bring real drama to an otherwise commonplace scene. Nice work, Trent.
by Ron Lindeboom on Sep 19, 2007


Related Articles / Tutorials:
More Of The Same Thing - Part 1Shape Layer Tips #3 - Trim PathsBehind the Scenes from Behind OasisCreating the Jumbotron Look in After EffectsPromo Breakdown - Part 2


Recent Articles / Tutorials:
Build your own affordable SAN -- that <i>works!</i>Where is my rocket car? NAB spends $60 million to roadblock the futureCreating a HologramPromo Breakdown - Part 1Want to be ready for Reality? Mod your camera!
MORE


FORUMSLIBRARYPODCASTSBLOGSMAGAZINESERVICESNEWSLETTERSNEWSSTOREEVENTS

© CreativeCOW.net All rights are reserved.

[Top]