Ben Crowder Blog

General disclaimer for pre-2010 posts: I've changed a lot over the years, and posts this old may not represent my views anymore.

Tutorial: Depth of field in Blender

Update: I’ve rewritten this tutorial for Blender 2.6 at Blender Quicktips.


Here’s how to do depth of field in Blender. What’s depth of field, you ask? Go read the Wikipedia article for the details, but the layman’s summary is that it’s how you blur the out-of-focus parts of an image. (The blur is called bokeh, by the way.) The shallower the depth of field (or the lower the f-stop), the blurrier the out-of-focus parts get. I like it because it looks better and feels more professional, and it also focuses the viewer’s attention right where you want it. (But it’s not something you should use in every shot by default. Don’t leave artistic judgment behind. :))

The good news is that Blender can do depth of field. The bad news is that it’s only possible via postprocessing (using compositing nodes), which isn’t perfect. But it mostly works.

I’m visual—show me what it looks like

Here’s the sample scene before adding depth of field:

DOF Before

And here it is afterward:

DOF After

As you can see, both the foreground and the background are now blurry, leaving only the center thingie in focus.

Overview (Microwave Edition)

All you have to do is give the camera a focus point (Dof Dist or Dof Ob), add a Defocus node, and render with Do Composite.

The Gritty Details, Please (With Pictures!)

1. Set up the scene. Here we’ve got a pretty basic setup with cubes (using some array modifiers and a path to spiral them out), lights, a thingie in the middle (made by extruding a cube), and a camera.

Step 1

2. Select the camera and go to the edit buttons. Click on the Limits button (under “Show:” in the upper right). You’ll now see a line pointing out from the camera, with a yellow line through the camera itself to show you where the camera’s focus will be.

Step 2

3. Now to set the focus. You can either give the camera a set distance (Dof Dist) or an object to focus on (Dof Ob, which we set here to MainCube, but you could just as easily use an empty to have finer control over where the camera focuses).

Step 3

4. Open up the node editor, click on the face (the middle of those three buttons), then click on Use Nodes.

Step 4

5. Add a Defocus node (Add->Filter->Defocus). And ignore the fact that there’s nothing in the RenderLayer on the left — I’m pretty sure it’s a bug in Blender 2.49.

Step 5

6. Drag the RenderLayer node’s Z output to the Defocus node’s Z input, the RenderLayer node’s image output to the Defocus node’s image input, and the Defocus node’s image output to the Composite node’s input. (Just follow the diagram. :))

Step 6

7. Uncheck No zbuffer. This way it’ll look at the depth data from the scene, which is exactly what we want. Also set the fStop down to something low (lower is blurrier) — try 8 or 12 for starters. Note: when you do the final render, deselect the Preview button here.

Step 7

8. Select the Do Composite button. (If you don’t, it won’t use the nodes we just set up and you’ll get an ordinary render.)

Step 8

9. Render. You’ll notice the sharp artifacts on the far left and center bottom. To be honest, I don’t know how to get rid of these. My guess is that they appear because Blender’s depth-of-field isn’t actual DOF but is just a postprocessing illusion. If any of you know how to fix this, let me know.

DOF After

Further Reading

Notes

This tutorial was made using Blender 2.49.

Respond

Comments

Jeff

Thanks for the tutorial! =D

Matt

having tried several overly complex tutorials on the same subject, I thank you for explaining simply what should be simply explainable.

Brandon

Thank you. This is short, sweet, and to the point. I love how this effect adds realism to the scene.

r

You can get rid of the blank node by clicking the button in the lower left. It will render the scene and then populate the node with that render. Alternately I find (using 2.49b) that if I just render the whole scene it then populates those node previews with the render.

Arn Sweatman

Great tute. Thorough and concise.

Thank You.

Baal Garnaal

Simply perfect, lost several hours browsing the net trying to find a tutorial that works without all the pointless info.

Dan Mundy

the “artifact” you think you’re seeing in the left of the screen is actually correct; judging by the way you arranged the cube swirls, it looks like that particular cube falls in the depth of field.

Ben

You know, I think you’re right — good catch. :)

Kristian

this is awesome! I didn’t know about the “Do Composite” … thanks a lot!

Lee

this is a great tutorial, nice and easy. one extra tidbit is that because it is a post-processing filter, it applies it’s effect after the image is fully rendered. so don’t pull a me and keep canceling the render before it finishes because its not giving you the effect!!

Ben

Good advice, Lee. :) It’d be nice if they somehow implemented DOF directly in the internal renderer, but my guess is they probably won’t. (But you can always use an external raytracer instead.)

Craig

Thanks, this helped a lot!!!