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 $\endgroup$ob.type.startswith('CAM')
or via an attribute testhasattr(...)
, doubt its worth it. Somewhat related blender.stackexchange.com/a/233823/15543 $\endgroup$[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. $\endgroup$