0
$\begingroup$

I'm trying to add a new camera to the scene and render it using multiview (with python script). at first, I remove all the objects besides the light from the scene:

    bpy.ops.object.select_by_type(type='MESH')
    bpy.ops.object.delete()
    bpy.ops.object.select_by_type(type='CAMERA')
    bpy.ops.object.delete()

But I saw that it didn't erease the camera from bpy.data.cameras which will give me a problem later on. After that, two cameras were added with init location and rotation:

    bpy.ops.object.camera_add(location = [9.69,-10.85,12.388], rotation = [0.6799,0,0.8254],enter_editmode=True)
    bpy.data.cameras[0].lens = 18

    bpy.ops.object.camera_add(location = [9.69,10.85,12.388], rotation = [radians(40.6),radians(-5.5),radians(140)],enter_editmode=True)
    bpy.data.cameras[1].lens = 18

The cameras are perfectly set in terms of location and rotation but not for the focal lens for some reason.

What is the right way to insert a new camera?

and another quick question - in order to be able to render the scene in multiview are the following lines are all is needed?

    bpy.data.scenes[0].render.use_multiview = True
    bpy.data.scenes[0].render.views_format = 'MULTIVIEW'

BR, Aviv

$\endgroup$
3
$\begingroup$

It looks to me like you've got camera objects confused with the camera data. In the code above, you delete all camera objects in the scene, but the data for those cameras is left over. When you later reference the camera by index, you're referencing unlinked, left over camera data from those deleted objects.

You don't necessarily need to clear the left over camera data - you just need to make sure you're pointing to the correct camera data later on (using pointers, for example, rather than indexing).

In the following code, I create the camera object and data directly from the low-level API, which allows me to store pointers to those objects, and their respective data, and change the properties directly:

scn = bpy.context.scene

# create the first camera
cam1 = bpy.data.cameras.new("Camera 1")
cam1.lens = 18

# create the first camera object
cam_obj1 = bpy.data.objects.new("Camera 1", cam1)
cam_obj1.location = (9.69, -10.85, 12.388)
cam_obj1.rotation_euler = (0.6799, 0, 0.8254)
scn.collection.objects.link(cam_obj1)

# create the second camera
cam2 = bpy.data.cameras.new("Camera 2")
cam2.lens = 18

# create the second camera object
cam_obj2 = bpy.data.objects.new("Camera 2", cam2)
cam_obj2.location = (9.69, 10.85, 12.388)
cam_obj2.rotation_euler = (radians(40.6), radians(-5.5), radians(140))
scn.collection.objects.link(cam_obj2)

Also note that this code was written for Blender 2.80. If you're using 2.79 or previous, replace scn.collection.objects.link function calls with scn.objects.link.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you very much! I will try that. $\endgroup$ – AvivSham Sep 12 '19 at 15:13

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.