2
$\begingroup$

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?

$\endgroup$

2 Answers 2

3
$\begingroup$

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].
                                      angle
                                      angle_x
                                      angle_y
                                      dof_distance
                                      dof_object
                                      ...
>>> bpy.data.cameras[active_cam.name].dof_distance = 7
$\endgroup$
1
  • 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$ Commented Aug 21, 2017 at 11:29
0
$\begingroup$

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.

$\endgroup$
1
  • 2
    $\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
    Commented Aug 21, 2017 at 13:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .