List of selected in Outliner collections

There is no single selection list for collections. We can open the Outliner in two different Blender areas, and select a different set of collections in each of them. Since the list of selected collections is different for each Outliner area, it can only be obtained through the context of this area.

The context can be accessed with operators. Having accessed the context through the operator, we can check the list of selected ID-objects in this context through the “selected_ids” list.

Let’s make a simple operator that prints a list of ID-objects from the context and register it in the Blender Python API:

In the “execute” function, we first get a list of all selected ID-objects (ID-objects include scene objects, meshes, armatures, and collections as well) from the “selected_ids” property.

After that, we filter only “Collection” type objects and print the resulting list.

By executing our operator in the Outliner area, for example, by adding an operator call button to the Outliner interface or by binding the operator call to a hotkey, we successfully get a list of selected collections.

If we need to execute the operator from another area (for example, Text Editor or 3D Viewport), we need to override the context and call our operator with the overrided context.

Finally we will get a list of selected collections:

0 0 votes
Article Rating
Subscribe
Notify of
guest

2 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Rombout Versluijs
Rombout
6 months ago

Using these script from the script workspace and adding the override directly under the operator. It returns an error for line 9 about “selected_ds”

Nikita
Editor
6 months ago
Reply to  Rombout

HI!
Try to use temp_override in last Blender versions.
https://b3d.interplanety.org/en/context-overriding-in-blender-3-2-and-later/