I’m adding custom entries to the Outliner’s context menu. Now I’d like to operate on the collection that is selected in the outliner underneath the cursor, but I’m struggling to find something like context.selected_items. context.collection gives me the active collection, but if the collection I have clicked on is excluded from the scene, context.collection will give me a completely different collection instead (the last active one or the master collection). Also I can not distinguish via Python if the context menu was opened with an object selected or a collection selected.
Is there something implemented in the Python API like this?
Thanks for pointing towards the documentation. This partially revealed that what I am looking for does not exist in the Python API. I’d like to get the selected item in the outliner window, not in the scene.
Digging further in the source code of Blender, I found that outliner_tools.c from within source/blender/editors/space_outliner is calling the various Python menus defined in release/scripts/startup/bl_ui/space_outliner.py. So the checking of the selected data type in the outliner is done in C code, which then calls different Python context menus, like this:
This leads me to the idea to append my draw function to the corresponding Outliner menu types instead (so if I want to operate on a collection, I append my operator to the draw function of OUTLINER_MT_collection, if I want to work on an object, I append to OUTLINER_MT_object instead). That should do what I want.
The outliner is being worked afaik. I think there were ideas floating around where outliner and viewport selection become the same thing or are linked in some way. But at least 2.81 or later.