Camera Focus Lock on Layer (Depth of Field)

Locks your camera’s focus distance to always stay on a selected layer when using depth of field.

Author:
Denis Stefanides
Category:
Camera

Expression Code

// Apply this to the Focus Distance property of your 3D camera
target = thisComp.layer("Your Target Layer"); // change this to the layer you want to stay in focus
cam = thisLayer;

// Calculate distance between camera and target
length(cam.toWorld([0,0,0]) - target.toWorld(target.anchorPoint))
javascript

Where to Apply

Apply this to the Camera’s Focus Distance property.
Path: Camera Layer > Camera Options > Focus Distance

You can use this on any 3D camera layer with Depth of Field turned on. Works great for text layers, nulls, or 3D objects.


How It Works:

Here’s what’s going on from top to bottom:

  1. target = thisComp.layer("Your Target Layer");
    This sets the layer you want the camera to focus on. You can replace "Your Target Layer" with the actual name, or just use the pick whip to make it easy.

  2. cam = thisLayer;
    This is a shortcut so you don’t have to keep typing thisLayer every time you reference the camera.

  3. cam.toWorld([0,0,0])
    This grabs the camera’s position in 3D space (right where the lens is).

  4. target.toWorld(target.anchorPoint)
    This gives you the 3D position of your target layer’s anchor point.

  5. length(...)
    This calculates the actual 3D distance between the camera and the target. That’s the number that drives the focus distance, so your camera is always focusing right where it should.

Frequently asked questions

How do I use the expression on this page?

Just copy the full expression code from the top of this page. Then Alt-click (or Option-click on Mac) the stopwatch on the property you want to animate. Paste the code into the editor and that's it. If you’re not sure which property to use, check the "Where to Apply" section above.

The expression isn’t working. What should I check?

First, make sure your project is using the JavaScript engine (go to File > Project Settings > Expressions). Also double-check for missing characters, and see if the code requires parenting a layer. If so, there will be a comment in the code explaining what needs to be connected.

I’m not sure what the expression does. Where can I learn more?

Take a look at the "How It Works" section on this page. It explains each part of the expression in plain language so you can understand how everything works together.

Can I customize how the expression behaves?

Yes, absolutely. Most expressions include easy-to-edit variables near the top and comments that guide you on what to change. You can also link sliders or checkboxes using Expression Controls if you want more control in the timeline.

Can I apply this to other properties too?

Yes, you can use it on any property with a stopwatch. That includes Position, Scale, Opacity, and also effect settings like Blur or Tint.

Will it work in comps with different frame rates or sizes?

In most cases, yes. These expressions are designed to adapt to your comp’s resolution and frame rate. If anything specific needs adjusting, it’ll be noted in the code or in the "How It Works" section.