14
$\begingroup$

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?

$\endgroup$

3 Answers 3

16
$\begingroup$

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
$\endgroup$
0
9
$\begingroup$

"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")
$\endgroup$
0
-1
$\begingroup$

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))
$\endgroup$
5
  • 2
    $\begingroup$ Suggest iterate through all objects of the scene bpy.context.scene.objects instead. $\endgroup$
    – brockmann
    Commented Sep 6, 2021 at 14:06
  • 2
    $\begingroup$ 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) $\endgroup$
    – batFINGER
    Commented Sep 6, 2021 at 14:19
  • 1
    $\begingroup$ @batFINGER I tested the code and indeed it says all cameras are "activada". The code doesn't work. $\endgroup$ Commented Sep 6, 2021 at 15:24
  • $\begingroup$ @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 $\endgroup$
    – MrMelqui
    Commented Sep 6, 2021 at 18:15
  • 1
    $\begingroup$ @MrMelqui "disable" an active camera this way and press F12 - the image will still be rendered from that camera. At least in 2.93... $\endgroup$ Commented Sep 6, 2021 at 18:32

You must log in to answer this question.

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