3
$\begingroup$

How do I iterate and select only camera's across multiple scenes via python? This is what I tried but only works for the first active scene.

import bpy
    
    objects = bpy.context.scene.objects
    scenes = bpy.data.scenes.keys()
    
    for i in scenes:
        bpy.context.window.scene = bpy.data.scenes[i]
        
        for obj in objects:
            obj.select_set(False)
            if obj.type == "CAMERA":
                obj.select_set(True)
$\endgroup$
5
$\begingroup$

Always looping over context scene's objects.

Logic error, you were only ever looping over the context scene's ("the first ones") objects.

Test code below, selects all and only camera objects. Sets the scenes camera as active for each viewlayer.

import bpy
for scene in bpy.data.scenes:
    bpy.context.window.scene = scene
    for ob in scene.objects:
        ob.select_set(ob.type == 'CAMERA')
        # set scene camera to active for each viewlayer
        if scene.camera:
            for vl in scene.view_layers:
                vl.objects.active = scene.camera
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Great thanks! Tried and it works, just curious - how does this de-select the other objects? Does .select_set deselect automatically? $\endgroup$ – Dan Sep 2 at 7:44
  • 1
    $\begingroup$ Expression equates to true or false. $\endgroup$ – batFINGER Sep 2 at 8:13
  • $\begingroup$ Ok, so true for camera's and false for other objects. $\endgroup$ – Dan Sep 2 at 8:15

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.