Enabling DoF in Blender viewport

The DoF (depth of field) parameter in the final render is responsible for blurring details in the background or objects close to the camera. During the modeling process, enabling DoF is distracting, so it’s usually only enabled for rendering. However, for proper scene setup, enabling DoF directly in the viewport can be useful.

We can manually enable DoF in the 3D viewport through the viewport shading menu. Expand the menu and on the “Depth of Field” checkbox.

To disable DoF in the viewport, off the checkbox.

DoF in the viewport can also be enabled and disabled using the Blender Python API.

First, ensure DoF is enabled for the camera. Get a pointer to the currently active camera in the scene:

And enable depth of field for it:

We can also set DoF parameters for the camera using its pointer. For example, focal length and aperture:

Now let’s enable DoF in the viewport.

To do this, we need to set the viewport shading property “use_dof” to True.

To ensure that the code executes correctly from another context, for example, if we’re executing a script from the Text Editor, we need to temporarily override the context to the viewport context.

Get a pointer to the viewport’s area:

And with context overridde, set the value of the viewport property we need.

To disable DoF in the viewport, we need to override the context in the same way and set “use_dof” property to False.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments