12

I could just use:

obj_camera = bpy.data.objects["Camera"]

to get the camera, but it is possible to have a scene with multiple cameras.

So if that is the case, how can I choose the active one?

iKlsR
  • 43,379
  • 12
  • 156
  • 189
P i
  • 3,921
  • 9
  • 34
  • 53

3 Answers3

15

The property you are looking for is 'Scene.camera'.

If you have 'python tooltips' enabled in the user preferences you can hover over the camera field in the 'Scene' tab of the 'Properties' area, to reveal the python code for this property:

enter image description here

The code to access this property for the current scene and assign it to a variable is:

obj_camera = bpy.context.scene.camera
Ray Mairlot
  • 29,192
  • 11
  • 103
  • 125
9

"active" is a weak definition, there is the scene camera (can also be None), but it could also mean the active object and that object being a camera. A special case is operator Set active object as camera, which makes an arbitrary object the scene camera.

cam_ob = bpy.context.scene.camera

if cam_ob is None:
    print("no scene camera")
elif cam_ob.type == 'CAMERA':
    print("regular scene cam")
else:
    print("%s object as camera" % cam_ob.type)

ob = bpy.context.object
if ob is not None and ob.type == 'CAMERA':
    print("Active camera object")
CodeManX
  • 29,298
  • 3
  • 89
  • 128
-1

I use the following code:

cameras_obj = [cam for cam in bpy.data.objects if cam.type == 'CAMERA']
for cam in cameras_obj:
    if cam.hide_render:
        print("la camara {} esta desactivada".format(cam.name))
    else:
        print("la camara {} esta activada".format(cam.name))
MrMelqui
  • 1
  • 1
  • 2
    Suggest iterate through all objects of the scene bpy.context.scene.objects instead. – brockmann Sep 06 '21 at 14:06
  • 2
    I was under the impression hide_render was an object property that ... stopped an object from being rendered, (which doesn't really matter for a non rendered object like a camera, empty etc) – batFINGER Sep 06 '21 at 14:19
  • 1
    @batFINGER I tested the code and indeed it says all cameras are "activada". The code doesn't work. – Markus von Broady Sep 06 '21 at 15:24
  • @MarkusvonBroady You should try pressing the disable in processing icon that is to the right of the name, with that it will tell you camera disabled – MrMelqui Sep 06 '21 at 18:15
  • 1
    @MrMelqui "disable" an active camera this way and press F12 - the image will still be rendered from that camera. At least in 2.93... – Markus von Broady Sep 06 '21 at 18:32