How to select all objects of a known collection with python

Sometimes, when you expect something to be simple, it’s a bit different in reality.
I have a collection, let’s say “MonkeyCollection”. And with Python / Blender API, I want to select all objects in that collection.

I expected something like:
bpy.ops.object.select_all(action=‘DESELECT’)
bpy.data.collections[‘MonkeyCollect’].all_objects.select_set(True)

but such seems not to be available.

1 Like
for obj in bpy.data.collections['MonkeyCollect'].all_objects:
    obj.select_set(True)
4 Likes

Ohhh, so it is quite simple. (once you know it).
Many thanks!

@Stan_Pancakes
what about if I list selected collections and want to select all in them?

collection_names = [col.name for col in bpy.data.collections]

collection_names = ["Collection", "Collection 2"]

# If collection_names is an empty list, use all collections,
# otherwise use only collections whose names are in collection_names
collections = bpy.data.collections if not collection_names else [col for col in bpy.data.collections if col.name in collection_names]

for col in collections:
    for obj in col.all_objects:
        obj.select_set(True)
1 Like

just realized that I listed all collections in scene, not selected ones…
Is there a way to list only selected collections?

Add: as far as I understood, this is not possible yet.

With 2.92+ it is now possible to list multiple collections.
collection_names = [col.name for col in bpy.context.selected_ids]

1 Like

Hi APEC, please can you give me some tip where to find info about how to use this new feature?
I mean: bpy.context.selected_ids
I just need to list selected object in outliner (for example because they are hidden in viewport)
but python is giving me the error: AttributeError: ‘Context’ object has no attribute ‘selected_ids’
I have 2.92.

Hi, it’s because you need to switch Text Editor to Outliner, select there something, then switch back to Text Editor and run the script which executes from Outliner (like below)

import bpy

# Set the area to the outliner
area = bpy.context.area
old_type = area.type 
area.type = 'OUTLINER'

# some operations
ids = bpy.context.selected_ids
print(ids)

# Reset the area 
area.type = old_type   

Also look at this addon for outliner (collections section, there I used new IDs feature)

Thank you APEC, it basically works. When I register it like an operator and execute it hovering mouse over outliner, it works as it should. But everywhere else it doesn’t, even in text editor… after my investigation I realized, when the area is switched back to old one, it somehow breaks the result. If this last reset command is not used, it somehow wokrs… but still it’s very confusing behavior for me, just take a look on the video. What should I do to make it work? I was searching over the net but haven’t found the solution yet :frowning:

It’s because you need your Text Editor area switch to Outliner by hands, select there something, and switch to Text Editor again and execute script.
The new IDs feature I think in development stage and have some limitations, that’s why it so complicate to achieve results.

All other actions with IDs I recommend to relocate to this topic

Here I made a simple addon and trying to achieve your request.

You right - it works as you described - I didn’t know I should have done this, thx :slight_smile:
I hope this limitation will be overcome soon.
I will continue disscusion on you proposed place - great!