# How can I get a Depth of field render pass?

How can one make a render pass with black and white data to display depth of field based on the Settings > Camera dof settings?
I want to blur or defocus in the compositor, similar to the way it is done with the Z pass but with a more accurate dof information that is based on a target, as opposed to distance away from the camera.

I tried to fiddle with the settings in a new Render layer, overwriting the material, but I have been unable to achieve a render pass effect that outputs something useful.

# Defocus node

There's build-in support for focusing on objects using this node.

• In the Properties editor under Object data (with the camera selected):

Camera Object Data, Depth of Field settings

• When setting up your Defocus node, enable Use Z-Buffer.

# Cycles DoF

Cycles also has built-in support for DoF. Again in Depth of Field panel, set your Focus object and now increase the Size.

Aperture Size

Also called lens radius. If this is zero, all objects will appear in focus, while larger values will make objects farther than the focal distance appear out of focus.

# Creating a black and white depth image

1. In the Node Editor:
1. In the Properties shelf (N) of the Driver section of the Graph Editor:
• Change the variable type to Distance and set the two object to the camera and your target object.
• Set the script expression to -var.
1. Back to the Node Editor:
• Adjust the Size setting in the Map Value node.
• Add | Converter | Math(Shift+A,N,M), set to Multiply and the values to -1 and the Map Value node.
• Add | Color | Mix (Shift+A,C,M), set to Screen and connect the Map Value and Math nodes.

• Wow, sometimes, some things are so simple that I wouldn't believe it :) Thanks. I wasn't aware that z depth works in relationship to a target setup in the depth of field section, hence the reason I was looking for a way to achieve another zdepth map. But now, that I just did a simple test, one with defocus node, and one with dof settings adjusted in the camera, i see that the results are comparable. I was previously trying to use the normalize node in between the z depth output and the defocus node z input. Jul 1, 2013 at 12:12
• @Aldrik any idea why I still get a white image after using the "Creating a black and white depth image" technique? Nov 17, 2015 at 22:39

This is because the Z-Depth pass outputs values greater than 1 for distance. You will need to use a Normalize node to convert those values to a 0-1 scale:

– p2or
Dec 14, 2015 at 13:34
• Absolutely, I didn't see that one. Good find. Dec 14, 2015 at 14:01
• This is the only thing thats missing in Aldriks answer. I'm not sure, should we ask the mod-team for merging this answer?
– p2or
Dec 14, 2015 at 14:17
• Maybe, we can ask their opinion Dec 14, 2015 at 14:18
• Use "Map Range" vector node if you are interested in preserving more depth information over an animation and also convert it to 0~1. For example if you do From 0~100 To 0~1 then you will lose everything further from camera than 100, but anything lower will be black to white in your zpass render. If you use Normalize in animation the relationship between 2 zpass renders will not be comparable. (One image's white might be really far, but other might be close) Sep 5, 2017 at 15:01

You can also hijack the Mist Pass. This works for both BI and Cycles.

Enable it in Properties > Render Layers > Passes:

And tweak the start/depth settings in Properties > World. If you are using BI, adjust the settings without enabling it, so that the combined pass is not affected:

Enable Mist in Camera Settings > Display to draw guides in the 3D view which make setting the values in Properties > World easier: