9
$\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$
13
$\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
8
$\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
    Sep 6 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
    Sep 6 at 14:19
  • 1
    $\begingroup$ @batFINGER I tested the code and indeed it says all cameras are "activada". The code doesn't work. $\endgroup$ Sep 6 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
    Sep 6 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$ Sep 6 at 18:32

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.