LIBRARY: Tutorials Reviews Interviews Editorials Features Business Authors RSS Feed

Making Light Disappear & Reappear Behind Objects Using Expressions

COW Library : Adobe After Effects Tutorials : 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





Comments

Nice Technique but Offset Turbulance missed in Illustration.
by Ashwani Sharma
I think the Offset Turbulance is left out in Steps narrated for Precomp under FRACTAL NOISE > Transform. I got it in your sample project and the clouds are moving in my composition. Thanks from Ashwani Sharma, Delhi, India.
Making Light Disappear & Reappear Behind Objects Using Expressions
by duncan preston
Hi,
I have a query regarding this tutorial.
It works very well, I can get it to work really nicely with the built in Lens Flare effect, but i'm looking for a bit more of a substantial light and a nice spill effect, so I've been trying it on Knoll Light Factories, Light Factory effect.
Everytime I use the coding above it works fine except for one problem.
I keep getting this error message:

After Effects warning: Timeout while waiting for
the engine
Expression disabled.

Error occurred at line 4.


I then click back into the expression, click out of it, and its ok, works fine. The problem comes when i go to render a movie, it always disables the expression.

I'm putting the first line of code:
thisComp.layer("Null 1").transform.position
on Light Source Location to refer to the position of the "Null 1" layer, which works perfectly.

and the rest of the code:
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)

on Brightness

Can anyone help..or is it a case of..I shou;dn't be trying this on tthat effect or my Mac just isn't beefy enough to handle it?

any help would be greatly appreciated.
Making Light Disappear & Reappear Behind Objects Using Expressions
by duncan preston
I have done everything your example shows, it all works fine (i've tweeked it a little bit to serve my purpose, only the numerical values that you set),
you can see it working nicely if i move around the "null" layer....everytime i go to render out a movie i get this error message and it disables the expression:

After Effects warning:Timeout while waiting for the engine
Expression disabled.

Error occured at line 4.

Can anyone help?? Does this mean my Mac is not beefy enough to render this out?

Any help on this matter would be greatly received

cheers
Making Light Disappear & Reappear Behind Objects Using Expressions
by Laurence Rietdijk
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.
Making Light Disappear & Reappear Behind Objects Using Expressions
by Trent Armstrong
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
In reply to Laurence Rietdijk...
by Robert Reed
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.
Making Light Disappear & Reappear Behind Objects Using Expressions
by Laurence Rietdijk
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?
Making Light Disappear & Reappear Behind Objects Using Expressions
by Patrick Miller
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?
Making Light Disappear & Reappear Behind Objects Using Expressions
by Trent Armstrong
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
Making Light Disappear & Reappear Behind Objects Using Expressions
by Katie Marcia
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!
Making Light Disappear & Reappear Behind Objects Using Expressions
by Trent Armstrong
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
Making Light Disappear & Reappear Behind Objects Using Expressions
by Travis Kaney
I seem to have the same problem as Justin.
I keep getting errors with the code
Making Light Disappear & Reappear Behind Objects Using Expressions
by pxlguy
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
Making Light Disappear & Reappear Behind Objects Using Expressions
by Maltaannon
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
Making Light Disappear & Reappear Behind Objects Using Expressions
by shroomly
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.
Making Light Disappear & Reappear Behind Objects Using Expressions
by pxlguy
Sorry to do this here, but ,Justin, would you contact me at trent@ignitermedia.com?

I have some questions for you.

Trent
Making Light Disappear & Reappear Behind Objects Using Expressions
by JustinG1983
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.
Thank you for illustrating a useful technique, Trent
by Ron Lindeboom
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.


Related Articles / Tutorials:
Adobe After Effects
Imagineer mocha Pro 5 Plug-In for Adobe: An In Depth Review

Imagineer mocha Pro 5 Plug-In for Adobe: An In Depth Review

Imagineer mocha Pro 5 Plug-in for Adobe brings all the amazing features of the professional version of the mocha Planar Tracker directly into After Effects and Premiere Pro in the form of a plugin. In this in-depth review, After Effects tutorial guru Tobias Gleissenberger of Surfaced Studio will show you what you can do with this new plug-in, and discuss what he likes and doesn't like about the new update.

Tutorial
Tobias Gleissenberger
Adobe After Effects
After Effects 2015.3 - My Favorite Features

After Effects 2015.3 - My Favorite Features

Learn why you should upgrade to After Effects CC 2015.3 - 13.8.1 - a close and detailed look at the latest release of After Effects (August 2016). Roei Tzoref will be focusing on his favorite features that set this release apart from previous versions: Performance, Queue in AME, Lumetri Color new features, and more.

Tutorial
Roei Tzoref
Adobe After Effects
Advanced Masking in Adobe After Effects

Advanced Masking in Adobe After Effects

Some of the coolest stuff you can do inside of Adobe After Effects is only possible once you unlock the power of masks. Join After Effects whiz Tobias Gleissenberger of Surfaced Studio to learn about mask animation and interpolation, using the variable width feathering tool, managing mask modes and ordering, and more.

Tutorial
Tobias Gleissenberger
Adobe After Effects
Fixing Common After Effects Problems and Mistakes

Fixing Common After Effects Problems and Mistakes

Got problems using Adobe After Effects? Exported files too large, Expressions not working, mixing shapes and makes, modes/switches, selecting previews for layers vs. comps - Surfaced Studio's Tobias Gleissenberger shows you fast fixes for these and more!

Tutorial
Tobias Gleissenberger
Adobe After Effects
mocha AE Planar Tracker for Absolute Beginners

mocha AE Planar Tracker for Absolute Beginners

Want to learn how to create advanced visual effects? Learn how to use mocha AE to track your shot and add advanced visual effects to live action footage inside Adobe After Effects. mocha can help you track shots that would be hard to track using traditional 2D point or feature trackers because it is a PLANAR TRACKER. A planar tracker uses planes and textures to track as opposed to points or groups of pixels. This allows the tracker to stay on track even if your shot contains motion blur or a very shallow depth of field. mocha AE comes included with Adobe After Effects and is available since CS3 and there is no reason for you not to use this awesome tool to make it easier for you to track your shot, replace screens or rotoscope!

Tutorial
Tobias Gleissenberger
Adobe After Effects
How to Make After Effects Faster with Proxies

How to Make After Effects Faster with Proxies

Learn how to make Adobe After Effects faster by using PROXIES! A proxy is a placeholder that stands in place for a very large video file or image sequence in your project. You can easily create proxies for the large source files that you are using and After Effects will automatically link them to the item in your project panel.

Tutorial
Tobias Gleissenberger
Adobe After Effects
Stabilize & Smooth: mocha 5 Plug-in for Adobe & Avid

Stabilize & Smooth: mocha 5 Plug-in for Adobe & Avid

Imagineer Systems and Boris FX product specialist Mary Poplin shows you how to stabilize with the new mocha Pro 5 plug-in inside of Avid Media Composer, Adobe Premiere Pro and After Effects. This tutorial covers artistic stabilization, such as smoothing out camera movements or stabilizing around moving objects, completely locking down shots, and automatically replacing edge fill on planar backgrounds.

Tutorial
Mary Poplin
Adobe After Effects
Adobe After Effects Puppet Tool

Adobe After Effects Puppet Tool

Become a puppet master by learning how to use the Puppet Tool in Adobe After Effects! This intermediate-level tutorial from After Effects guru Tobias will show you how the Puppet Tool allows you to add joints and animations to bring life to any static image!

Tutorial
Tobias Gleissenberger
Adobe After Effects
How to Spawn A Clone in Adobe After Effects

How to Spawn A Clone in Adobe After Effects

Want to learn how to create a cool clone spawn effect in Adobe After Effects? Follow along with After Effects whiz Tobias from Surfaced Studio in this exciting new visual effects tutorial that combines green screen using Keylight, CC Vector Blur, the Liquefy Effect, CC Particle World, and much more, delivered in Tobias' inimitable style!

Tutorial
Tobias Gleissenberger
Adobe After Effects
Creating A Flame on Your Finger with After Effects

Creating A Flame on Your Finger with After Effects

It is easy to do some motion tracking and attach a basic stock footage element of fire onto your hands, but there is a little bit of work involved if you actually want to make it look good. In this intermediate tutorial by After Effects expert Tobias, you'll see how to use a fire stock footage element to set your thumb on fire! There are lots of useful tricks for null objects, expressions, and more in this tutorial that will help you create all sorts of other cool visual effects -- or set even more things on fire!

Tutorial
Tobias Gleissenberger
MORE
© 2016 CreativeCOW.net All Rights Reserved
[TOP]