1
$\begingroup$

I'm trying to change the camera FOV by script. I've already seen different questions like this, but the answers doesn't work for me... This is my code:

import bpy
import math

a = 'hello'
bpy.ops.object.camera_add(enter_editmode=False, align='VIEW', location=(50,0,0), rotation=(1.57057,0.00174533,1.57057))
bpy.data.objects["Camera"].name = a
bpy.data.cameras[a].lens_unit = 'FOV'
bpy.data.cameras[a].angle= math.radians(10)
$\endgroup$

1 Answer 1

3
$\begingroup$

bpy.data.cameras gives access to the camera data. And camera data is not renamed (stays 'camera.xxx') when you set the object name.

So, access the camera via objects and use its data:

import bpy
import math

a = 'hello'
bpy.ops.object.camera_add(enter_editmode=False, align='VIEW', location=(50,0,0), rotation=(1.57057,0.00174533,1.57057))
bpy.data.objects["Camera"].name = a
bpy.data.objects[a].data.lens_unit = 'FOV' #Access it by its object name
bpy.data.objects[a].data.angle= math.radians(10)

Additionally, you should not rely on the fact the created object is named "Camera" (because it won't if such a camera already exists).

You can:

import bpy
import math
from mathutils import Euler

a = 'hello'
camera_data = bpy.data.cameras.new(a)
camera = bpy.data.objects.new(a, camera_data)
bpy.context.collection.objects.link(camera)
camera.location = (50,0,0)
camera.rotation_euler = Euler((1.57057,0.00174533,1.57057))
camera.data.lens_unit = 'FOV'
camera.data.angle = math.radians(10)
$\endgroup$

You must log in to answer this question.

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