I want to change the DoF distance of the active camera in a scene using a script. So far I have written:

bpy.data.cameras[0].dof_distance = distance #"distance" is the variable I want to use

But that sets the DoF distance for the camera that comes first in the outliner (alphabetically), which is not necessarily the active one.

So I tried getting the active camera property from the scene settings using

bpy.context.scene.camera.dof_distance = distance

But that does not work at all, because this method only returns the object, but not the "camera-properties" (as far as I get it).

How can I make this work?


bpy.context.scene.camera returns the active camera in the scene. In order to change your Depth of Field settings use the Console, Python Tooltips or the API to find each corresponding property.

>>> active_cam = bpy.context.scene.camera
>>> bpy.data.cameras[active_cam.name].
>>> bpy.data.cameras[active_cam.name].dof_distance = 7
  • 1
    $\begingroup$ Thanks for your answer! Unfortunately this leads to another problem: imgur.com/a/KySFk The settings of a camera and the "camera-object" can hold a different name.... In that case the script does not work... $\endgroup$ – Lennart Demes Aug 21 '17 at 11:29

I think this is it:

obj_camera.data.dof_distance = distance

This seem to always choose the correct camera, even if the datablock is named differently.

  • 1
    $\begingroup$ @brockmann using eg above obj_camera is an object (in bpy.data.objects) of type CAMERA, so obj_camera.data is a member of bpy.data.cameras. They do not need to have the same name, (which is the issue with your answer). $\endgroup$ – batFINGER Aug 21 '17 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.