Hi! I got my first job in a studio as a junior pipeline TD (but I don’t have much experience in this field, I applied as an asset artist ;p)
Buuut, to the point. I need 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.
We got D.cameras class 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 got any ideas how can i achieve what I want? Thank you!
camera_names = [cam.name for cam in bpy.data.cameras]
camera_objects = [bpy.context.scene.objects[name] for name in camera_names]
selected_cameras = [camera for camera in camera_objects if camera.select_get()]
Thank you for reply. Solution you mentioned does not work ( in first line you are getting data-block name, and in 2nd line you want to refer to objects using data-block names ( and you can’t do it))
I will move discussion to the sites you mentioned, sorry ;p
>>> camera_names = [cam.name for cam in bpy.data.cameras]
>>> camera_objects = [bpy.context.scene.objects[name] for name in camera_names]
>>> selected_cameras = [camera for camera in camera_objects if camera.select_get()]
>>> selected_cameras
[bpy.data.objects['Camera']]
Not sure if you found your answer, but you should be able to use the user_map call to get the objects that have the data-block in question. For instance:
for c in bpy.data.cameras:
user_map = bpy.data.user_map(subset={c}, value_types={'OBJECT'})
for cam_obj in user_map[c]:
# do something with cam_obj