2
$\begingroup$

I tried to set the FOV of my camera but could not do it via bpy.context.scene.camera. When I tried to access the angle property I got AttributeError, but I found bpy.data.cameras["Camera"] has an angle attribue that I could change. My script now looks like this:

def setup_camera():
    camera = bpy.context.scene.camera
    camera.scale = (-1, 1, -1)
    camera.rotation_mode = "QUATERNION"
    cam = bpy.data.cameras["Camera"]
    cam.angle_x = 69.4
    cam.angle_y = 42.5

I just wanted to know if this is the right way to change the FOV? I guess one is a camera profile the other one is an actual camera?

$\endgroup$

1 Answer 1

3
$\begingroup$

I'm not that familiar with the structure of Blender's type hierarchy, but according to the documentation for Blender's Object type there is a distinction between the object and the objects's data block.

# From Blender's Documentation:

# Create new lamp datablock
lamp_data = bpy.data.lamps.new(name="New Lamp", type='POINT')

# Create new object with our lamp datablock
lamp_object = bpy.data.objects.new(name="New Lamp", object_data=lamp_data)

Likewise, bpy.context.scene.camera is a Blender Object type which has its camera specific attributes stored in it's data block of Camera type, which can be accessed with the attribute data.

camera = bpy.context.scene.camera
camera.data.angle_x = math.radians(69.4)  # angle_x and angle_y take radians

The properties panel in the user interface is arranged the same way. Values like location, rotation etc. are set in the Object tab, type specific values like the Field of View of a camera or shape keys for a mesh are set in the Data tab.

You could also have more than one camera in the scene, in which case the cameras could either be accessed as objects in bpy.data.objects['<name>'] (with the camera data accessible from the objects's data attribute again), or you access the data block directly bpy.data.cameras['<name>'].
bpy.context.scene.camera would only refer to the active camera of the scene, just like the corresponding entry in the properties panel under the Scene tab.

N.B.: I assume angle_x and angle_y can't be set independently but instead are coupled by the aspect ratio. If you set angle_x, angle_y will follow suit.

$\endgroup$
1
  • $\begingroup$ I saw that question, but I assumed since the api exposes both I can set both. Oh and they take rads that is very good to know $\endgroup$
    – Hakaishin
    Commented Sep 10, 2018 at 21:15

You must log in to answer this question.

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