To select all objects in a collection with the Blender Python API, we need to walk through the list of these objects and call the “select_set” method for each of them, specifying the “True” value in the parameter.
If the required collection is active, we can access it through active collection pointer:
1 |
bpy.context.collection |
Otherwise, the collection can be referred to by its name:
1 |
bpy.data.collections['_COLLECTION_NAME_'] |
We can get a list of all objects that are contained in the desired collection through “objects” property:
1 2 3 |
bpy.context.collection.objects[:] # [bpy.data.objects['Cube.006'], bpy.data.objects['Cube.007'], ...] |
So we can select all the objects in the collection:
1 2 |
for obj in bpy.context.collection.objects: obj.select_set(True) |
However, this way we can only select objects that are directly in the active collection. If there are nested collections and there are objects in them, these objects will not be selected.
We can select all objects in the collection and in all nested collections at all levels through the “all_objects” collection property:
1 2 |
for obj in bpy.context.collection.all_objects: obj.select_set(True) |