Get list of selected Collections in Outliner?

Hello,
I can get selected Collection with bpy.context.collection
but how to get list of multiple selected collections?

As of 2.92, the outliner exposes all selected data-blocks via context: bpy.context.selected_ids. https://wiki.blender.org/wiki/Reference/Release_Notes/2.92/Python_API
Collections are data-blocks/IDs (the scene collection being an exception!) so this should work.

This was added for the asset browser and will probably be extended for more specific queries and used much more in future (esp. to rewrite the Outliner context menu).

1 Like

what I tried to do, make modified context menu for collection “Select Objects” to work with multiple collections…thought it was possible untill now.
Thanks for explanation.

import bpy

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

ids = bpy.context.selected_ids
print(ids)

# Reset the area 
area.type = old_type   

result in console just [] (in 2.93 alpha)
Ok. I just leave it for a while.

(Forgot about this, sorry for the late reply.)

If you create an Outliner from scratch there is no selection, so of course the output will be empty. But if you run it from an area that had an Outliner visible before, and where that Outliner had some selection, it should succeed.

E.g. when I create an Outliner, select something, change that Outliner to a Text Editor and run the script there, I get output like this:
[bpy.data.collections['Collection'], bpy.data.objects['Camera'], bpy.data.objects['Cube']]

2 Likes