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
After Effects Advanced Content-Aware Fill With Photoshop

After Effects Advanced Content-Aware Fill With Photoshop

In part three of his series on Adobe After Effects Content Aware Fill, filmmaker and After Effects artist Cody Pyper takes his deepest dive yet! Following requests from viewers in the series so far, Cody takes a closer look at how Adobe Photoshop can help you remove unwanted objects from your video footage in After Effects.


Cody Pyper
Adobe After Effects
Best Results with After Effects Content-Aware Fill: Reference Frames

Best Results with After Effects Content-Aware Fill: Reference Frames

Join filmmaker and After Effects artist Cody Pyper for a deep dive into how to get the absolute best results using the Content-Aware Fill tool in After Effects. Locked-down shots with simple backgrounds are one thing, but Cody shows the details of how to get fantastic results with complicated backgrounds and a moving camera using reference frames.


Cody Pyper
Adobe After Effects
Adobe After Effects Reverse Stabilization

Adobe After Effects Reverse Stabilization

You're going to be blown away by how you can power up your After Effects workflow with reverse stabilizing your footage! By separating your tracking from your compositing, you can focus on each step, and in addition, overcome the render order complexities when match moving elements and effects on a moving shot.


Roei Tzoref
Adobe After Effects
After Effects Content Aware Fill: When It Doesn't  Work

After Effects Content Aware Fill: When It Doesn't Work

There’s a new artificial intelligence-powered feature in Adobe After Effects called Content-Aware Fill that allows you to remove anything from your shots fairly easily! It's powerful, but if you’ve tried it you know that it doesn’t always work perfectly. So what do you do when it doesn’t work as well as you'd hoped? Filmmaker Cody Pyper is here to show what to try next!


Cody Pyper
Adobe After Effects
VFX in Adobe After Effects: An Artists Panel (NAB)

VFX in Adobe After Effects: An Artists Panel (NAB)

Join panelists Andrew Kramer of Video Copilot, Jayse Hansen, and Mary Poplin of Boris FX, along with moderator, Victoria Nece of Adobe, as they discuss the world of visual effects from an artist’s perspective.


Adobe Creative Cloud
Adobe After Effects
Scars, Tattoos & Digital Makeup: After Effects & Mocha Pro

Scars, Tattoos & Digital Makeup: After Effects & Mocha Pro

How do you add SCARS, TATTOOS or DIGITAL MAKEUP to a person's face? Learn how to use Mocha Pro's planar tracker and the Mesh Warp tool to insert a flat image/video on a (non flat) human face! Join VFX guru Tobias G from Surfaced Studio for a closer look!


Tobias G
Adobe After Effects
Compositing Secrets Everyone Can Use, Pt 5: Brightness

Compositing Secrets Everyone Can Use, Pt 5: Brightness

The first challenge to understanding the nature of brightness in compositing starts with remembering that we're not actually seeing color at all, but rather something of an illusion that appears to us as color! Join longtime VFX artist, editor, software developer, and business owner Simon Ubsdell for Part 5 of the best look behind the technology of compositing that you've ever seen, as he takes a look at the math behind brightness, and how to apply that to the compositing toolsets in your favorite editing, compositing, and color grading applications.

Tutorial
Simon Ubsdell
Adobe After Effects
60 Second Tutorial: Alien Invasion in Adobe After Effects

60 Second Tutorial: Alien Invasion in Adobe After Effects

Here's a fast, fun one! Sixty seconds to create and animate an alien using the 3D and Track Camera tools to animate your character in Adobe After Effects. Use the included files, or bring your own!

Tutorial
Adobe Creative Cloud
Adobe After Effects
What Are Adobe Motion Graphics Templates?

What Are Adobe Motion Graphics Templates?

A Motion Graphics Template, referred to as a MOGRT, is an animated sequence that is self-contained and can be used in Adobe Premiere Pro, Adobe Premiere Rush and Adobe After Effects, combining graphics, text, audio and video files, as well as vector or still images (including logos), to create a still or animation that can then be customized by the MOGRT user. The result is a dynamic creative tool that provides design freedom and is consistent to its users across apps and devices. Reuse, share, and even sell them!


Rod Harlan
Adobe After Effects
Compositing Secrets Everyone Can Use Pt. 4 - Advanced Alphas

Compositing Secrets Everyone Can Use Pt. 4 - Advanced Alphas

When most people hear the words "alpha channels", they think "transparency", but that's not exactly accurate. The truth is more complex, and a quite bit more interesting! Join longtime VFX artist, editor, software developer, and business owner Simon Ubsdell for Part 4 of the best look behind the technology of compositing that you've ever seen, packed with practical advice for applying the secrets of alpha channels that's simply not possible before understanding these underlying principles. No matter which applications you're using for editing, compositing, or visual effects, this one is a must-see!

Tutorial
Simon Ubsdell
MORE
© 2020 CreativeCOW.net All Rights Reserved
[TOP]