0
$\begingroup$

I would like to create several cameras, and store them as follows:

a = bpy.ops.object.camera_add()
b = bpy.ops.object.camera_add()
c = bpy.ops.object.camera_add()

This command allows me to select the camera called "Camera.001"

bpy.data.objects['Camera.001'].select = True

But if my scene already contained several cameras before I created a, b & c, how would I select camera b without knowing its name? This code doesn't work:

bpy.data.objects[b].select = True

Thanks for your help. My python knowledge is minimal.

$\endgroup$
1
$\begingroup$

bpy.ops don't return items like that.

Instead, you want to work on the cameras and objects data blocks directly, like so:

camdata = bpy.data.cameras.new(name="Camera A")
a = bpy.data.objects.new(name="Camera A", object_data=camdata)

Then, link it to the currently active scene:

bpy.context.scene.objects.link(a)

You could then select the camera either, by just doing:

a.select = True

or by doing

bpy.data.objects[a.name].select = True
$\endgroup$

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.