Sub topic 13
Depth of field with focus object and f-stop
This chapter assumes you can add a camera, make it the active scene camera, and read its focal length and sensor size in the Properties editor. It also assumes you know how field of view follows from those two values. Depth of field builds on them, because focal length and camera placement both change how blurry the background looks. The Python examples are optional, but they show every setting by name.
What depth of field is
A real lens focuses sharply on one distance. Objects nearer or farther than that distance fall out of focus, and the blur grows with the gap. The band of distances that still looks acceptably sharp is the depth of field (DoF). A shallow DoF isolates a subject from its background. A deep DoF keeps everything crisp.
Blender does not blur by default. Its camera is a pinhole, so everything is sharp. You turn DoF on per camera. Because the setting lives on the camera data, two cameras in the same scene can have different DoF settings.
The three controls
Depth of field in Blender comes down to three decisions: whether it is on, where the focus plane sits, and how wide the aperture is.
| Setting | bpy property | What it does |
|---|---|---|
| Depth of Field checkbox | `cam_data.dof.use_dof` | Turns blur on for this camera. |
| Focus Object | `cam_data.dof.focus_object` | Sets the focus distance to the distance of this object from the camera. |
| Distance | `cam_data.dof.focus_distance` | Manual focus distance in scene units. Ignored while a focus object is set. |
| F-Stop | `cam_data.dof.aperture_fstop` | Aperture size. Lower values give more blur. |
In the interface, these are in the Properties editor under the Object Data tab (the green camera icon), in the Depth of Field panel.
Focus object versus manual distance
A focus object is the easier method for most shots. Blender measures from the camera to the object's origin each frame. If the camera or the subject moves, focus follows, with no keyframes.
Two details matter:
- The distance is taken to the object's origin, not to its surface. For a character, the origin often sits at the feet. The focus plane then passes through the feet, and the face can be slightly soft. Use an Empty parented to the head or eyes as the focus object.
- The focus plane is a plane perpendicular to the camera's view direction, not a sphere around the camera. An object off to the side at the same straight-line distance can fall slightly out of focus.
If you leave the focus object empty, Blender uses focus_distance. This is useful for fixed shots. When you enable Limits in the camera's Viewport Display panel (show_limits), a yellow mark appears along the camera's view line at the focus distance. Use it to see where focus lands before you render.
If the focus object is a bone-carrying armature, focus_subtarget selects the bone by name.
The f-stop and the aperture
The f-stop number is the focal length divided by the aperture diameter:
aperture diameter = focal length / f-stopA smaller f-stop means a wider opening, so more blur. A 50 mm lens at f/2.8 has an opening about 17.9 mm wide. At f/16 it is about 3.1 mm, and the image looks almost entirely sharp.
focal_length = 50 # mm
for fstop in (1.4, 2.8, 5.6, 16):
diameter = focal_length / fstop
print(f"f/{fstop}: aperture {diameter:.1f} mm")The f-stop scale is not linear. Each full stop (1.4, 2, 2.8, 4, 5.6, 8, 11, 16) multiplies the f-number by about 1.41, which halves the light area. Blender does not simulate exposure from f-stop, so changing it will not make the image darker. It only changes blur. Brightness is controlled separately, for example by Exposure in Color Management.
Blender's default f-stop is 2.8. At f/128 the blur is effectively gone, which is useful for turning DoF down without unchecking it.
Setting it up with bpy
This script creates the camera and the subject if they are missing, so it runs in an empty scene. Then it turns on DoF and aims the focus at the subject.
import bpy
scene = bpy.context.scene
# Subject: an empty to focus on (create it if it does not exist)
subject = bpy.data.objects.get("Subject")
if subject is None:
subject = bpy.data.objects.new("Subject", None)
scene.collection.objects.link(subject)
# Camera (create it if it does not exist)
cam_obj = bpy.data.objects.get("ShotCamera")
if cam_obj is None:
cam_data = bpy.data.cameras.new("ShotCamera")
cam_obj = bpy.data.objects.new("ShotCamera", cam_data)
scene.collection.objects.link(cam_obj)
cam_obj.location = (7.0, -7.0, 5.0)
scene.camera = cam_obj
cam_data = cam_obj.data
cam_data.lens = 85 # longer lens, shallower DoF
cam_data.dof.use_dof = True
cam_data.dof.focus_object = subject
cam_data.dof.aperture_fstop = 2.8
cam_data.show_limits = True # draw the focus point in the viewportTo focus manually instead, clear the object and set a distance:
import bpy
cam_data = bpy.data.objects["ShotCamera"].data
cam_data.dof.focus_object = None
cam_data.dof.focus_distance = 9.5 # scene units (meters by default)What else changes the blur
The f-stop is only one factor. The amount of blur depends on four things:
- Aperture diameter. Lower f-stop means a larger opening and more blur.
- Focal length. At the same f-stop, a longer lens has a larger physical aperture (focal length divided by f-stop), so the blur is stronger. An 85 mm lens at f/2.8 blurs a background much more than a 24 mm lens at f/2.8.
- Distance to the subject. Moving the camera closer to the subject makes the background blur stronger.
- Distance from the subject to the background. The farther the background is behind the focus plane, the softer it gets.
A common portrait setup combines these: a long lens, a low f-stop, a close subject, and a distant background. If you want the subject to fill the frame but keep a deep DoF, use a wide lens at a high f-stop and move in close.
Sensor size matters too, because it changes which focal length frames the shot. A larger sensor needs a longer lens for the same field of view, and the longer lens gives more blur at the same f-stop. Keep the sensor width fixed while you compare lenses, or the comparison will mislead you.
Bokeh shape
Out-of-focus highlights, called bokeh, take the shape of the aperture. By default they are circles. The Aperture settings under the f-stop control the shape:
aperture_blades: the number of diaphragm blades. A value of 0 gives a circle. Values of 3 or more give a polygon, such as a hexagon at 6.aperture_rotation: rotates the polygon, in radians in Python.aperture_ratio: stretches the bokeh to mimic anamorphic lenses. Values above 1 stretch it vertically, and values below 1 stretch it horizontally.
import math
import bpy
cam_data = bpy.data.objects["ShotCamera"].data
cam_data.dof.aperture_blades = 6
cam_data.dof.aperture_rotation = math.radians(15)
cam_data.dof.aperture_ratio = 1.0Render engines and common problems
Cycles computes DoF by sampling many rays through the aperture, so blur is accurate but adds noise. More samples or denoising clean it up. EEVEE approximates DoF as a post-process, which is faster but can show artifacts. Common artifacts are halos around sharp edges and flat-looking blur on thin objects. If EEVEE looks wrong, check its Depth of Field options in the Render properties and compare against a Cycles render of the same frame.
Check these first when the result looks wrong:
- Nothing is blurry. DoF is off, the f-stop is too high, or the shot has little distance between subject and background. Confirm
use_dofand try f/1.4 as a test. - Everything is blurry. The focus object may be far from the subject, or its origin is somewhere unexpected. Select it and check where its origin is.
- Focus does not follow the subject. A manual distance is set and no focus object is assigned. Assign the object.
- Wrong camera. Settings apply to the camera data. If you have several cameras, make sure you edited the one assigned to
scene.camera, or the one a timeline marker switches to. - Shared camera data. Two camera objects that use the same camera data share DoF settings. Make the data single-user before you give one a different f-stop.
Exercises
Exercise
A scene has a 50 mm lens at f/2.8. You change the lens to 100 mm and want the same physical aperture diameter. What f-stop do you need, and why?
Show answer
The aperture diameter is focal length divided by f-stop. At 50 mm and f/2.8 the diameter is about 17.9 mm. At 100 mm you need a diameter of 17.9 mm, so the f-stop is 100 / 17.9 = 5.6. At f/2.8 the 100 mm lens would have a 35.7 mm opening, which gives much stronger blur.
Exercise
You set a focus object on a character, but the face is soft while the feet look sharp. What is the most likely cause, and how do you fix it?
Show answer
The focus distance is measured to the object's origin, and the character's origin sits at the feet. Add an Empty at the face or eyes, parent it to the character, and set that Empty as the camera's focus object.
Exercise
Write the three bpy lines that turn on depth of field for the camera data in a variable named cam_data, focus it on an object named target, and set the f-stop to 4.
Show answer
camdata.dof.usedof = True camdata.dof.focusobject = bpy.data.objects["target"] camdata.dof.aperturefstop = 4