I require a way to select certain cameras in a scene. My plan is to get all camera objects in a scene, and then based on their names get the ones that I need.
Currently I’m selecting all cameras like this:
all_cams = [ob for ob in list(bpy.context.scene.objects) if ob.type == ‘CAMERA’]
But i think there should be a better way to do this, without iterating through all objects in a scene.
There is the D.cameras collection and from there I can get all data-block names. But how I can go from data-block names to the object name?
Or at least how do I select the cameras using their data-block name? ( then I can work further based on selected objects)
Do you have any ideas how I can achieve this? Thank you!
[ob for ob in bpy.context.scene.objects if ob.data.name in camera_names]and even then if 2 cameras use the same data it will fail. Object data don't hold a reference to their object because they can be shared amongst an indefinite number of objects – Gorgious Oct 27 '21 at 07:50Yeah, i was thinking that. Didnt have opportunity to run a test tho. I was just wondering if there is a way to do this. I'll stick to what I have right now, and if it will be a problem I will try to adress it then I guess ;p
– Przemek Oct 27 '21 at 09:03ob.type.startswith('CAM')or via an attribute testhasattr(...), doubt its worth it. Somewhat related https://blender.stackexchange.com/a/233823/15543 – batFINGER Oct 27 '21 at 10:42[camera for camera in bpy.data.cameras if bpy.context.scene.objects.get(camera.name)]. No guarantee that it will be faster in any given situation though. – Marty Fouts Nov 01 '21 at 17:22