Does Defocus node work when Compositing previously rendered layers?

I’m trying to test using the compositor to add Depth of Field from previously rendered passes. I’m using Blender to render the layer passes now, but I would also like to be able to use this to composite layers rendered in other programs.

First, I created a scene and set the DOF at the green monkey. Using the Defocus node and compositing directly at render time, I get the expected result: green monkey in focus, softer focus on the others.

In order to test the compositor alone, I then render an OpenEXR RGBA with Z-buffer and a separate image with no DOF, no Z-buffer and no compositing (everything in focus).

From older tests and previous threads, I know that I can use Map Value and Color Ramp into Blur nodes and combine those for a DOF effect as described here:

What I’m wondering is if it’s possible to use the Z-buffer from the .exr (with or with Map Value) to go into the Defocus node Z input. Something like my attached node setup. It seems like this may not be possible, since there is no camera in the scene to send input to the Defocus node.

Can I use the Defocus node this way without the need for Color Ramp and Blur nodes when compositing pre-rendered images? What settings do I need in the Map Value to recreate the DOF of the original render time composite?

The .exr Z-buffer alone directly into the Defocus node just blurs the whole image. With the posted node setup (Map Values and Z Combine), I don’t get any DOF effect. Everything is sharp regardless of Offset or Size settings in the Map Values.


  1. render-time composite with Defocus, the desired result
  2. render-time composite nodes
  3. nodes attempting to composite previously rendered layer image and Z-buffer (not working)


I’d really appreciate some comments. I’ve been searching (forums, Wiki, Google) and test rendering for a couple days now, with no success.

If you skipped my first post, my intent is to composite a pre-rendered image and Z-buffer layer through the Defocus node for DOF.

I found this example mentioned in an old thread:

It looks extremely simple, but I cannot get a Z-buffer render layer out of Blender that works for this. It either does nothing, so the image is completely sharp or the image is completely blurred.

Now, I have switched to testing a reference scene from here:

I always get the expected result when doing a render time composite, but I can’t successfully composite separately rendered image and Z-buffer layers.

I have tried combinations of the following:

Multilayer RGB
Multilayer RGBA
OpenEXR Z-buffer, RGBA
OpenEXR Z-buffer, RGBA


Combined and Z render layers checked with/without Compositing
Z only (Combined unchecked) on it’s own layer selected as the only Active Render layer

Do I need ZMask or AllZ or do I need to uncheck everything else (Solid, halo, Ztra, Sky, Edge, Strand) when rendering Z?

I also tried setting Camera DOF distance in the image only comp, then using that Z distance, as well as trying to use that with a Z combine of my original Z buffer export. I also made a similar attempt using a DoF target with an empty. These methods also did not work,

What should the node thumbnail for a proper Multilayer or OpenEXR Z-buffer render layer look like? I variously am seeing a white image or transparent pink/purple.


  1. node for a working render time composite with Defocus. The result is successfully rendered DOF in the scene.
  2. one attempt at settings to Render a Multilayer image with a Z-buffer
  3. the intended node setup of pre-rendered images (rather than render time layers), which doesn’t seem to work with any combinations of settings to export a Z-buffer.


Well, I finally got it to work. I used the same nodes as posted in image 3 above. The thing I was missing was setting DOF distance in the camera to match the original render DOF distance. I had tried that before, and I thought it wasn’t working, but I was trying so many different combinations of settings that I think it got lost in the shuffle.

To summarize what’s working to get a Defocus DOF composite working with a pre-rendered image and Z-buffer is:

To render the elements:

  1. uncheck Use Nodes
  2. uncheck Do Composite
  3. check Combined and Z in render layer
  4. Use Multilayer format RGB or RGBA depending on the scene

To composite:

  1. check Use Nodes
  2. check Do Composite
  3. add an Input > Image node and load the pre-rendered Multilayer .exr
  4. connect Image and Z to Defocus and make sure No Zbuffer is disabled
  5. camera settings, set DoF Dist to match the pre-rendered DoF distance

You can also render a Z only layer and use two separate Image nodes, one for the Image and one for Z.

In 2.5 you may also need extra nodes: Gamma and Alpha Convert (Key to Premul) to match the render time composite, depending on your scene and settings.

I started out mainly testing this in 2.49b, but it also works the same in 2.5. However, 2.49 seems to be more glitchy and inconsistent about changes to the node Input images and changes to the Dof Dist. When F12 rendering, 2.49b does not always update the changes properly, which may have been part of my problem when I was testing. In 2.5, the changes made to image nodes and Dof Dist update properly for each render.

Attached images:

  1. The 2.5 nodes I used to try to match the Doftest.blend default render time composite. It’s not an exact match because the original has a bright blurred edges behind the focal plane that I wasn’t able to duplicate. Otherwise the composite looks fine.

  2. my original test now in 2.5, this is the original render time composite

  3. in 2.5 the composite of pre-rendered layers. It basically works, but if you look closely it’s not a 100% match. This is partly because the gamma node probably doesn’t exactly match the Defocus gamma correction setting, but there are also other qualities that are slightly different in the composite of the pre-rendered image and Z buffer.


great lighting…

Thanks. Sort of an attempt at a standard 3-point setup. I wasn’t working lighting though, just testing Defocus in the compositor with a pre-rendered element. I turned off soft shadows and AO and subsuf, so the render would be quick.

harveen: Thanks a lot for the tip! I’ve had a hard time dealing with EXRs too, since I also wanted to post-process the DoF “after” I have rendered the image, sort of like a safe way to add in the DoF later on instead of (which I often use) applying the DoF already in the image as it is rendered. However, the only drawback in this is that it “must” remember the camera settings you used in your original file from which you saved your exr from, that way you won’t have to readjust the DoF of the camera in the composite file. Anyhow, that’s just a few clicks and it doesn’t hurt. Thanks for sharing. :slight_smile: