2
$\begingroup$

I want a callback for Collection selections in the Outliner. So far I already found a common solution for object selections:

handle = object()
subscribe_to = bpy.types.LayerObjects, 'active'

def notify_test():
    print('Notify')

bpy.msgbus.subscribe_rna(
    key=subscribe_to,
    owner=handle,
    args=(),
    notify=notify_test,
)

bpy.msgbus.publish_rna(key=subscribe_to)

But what bpy.types do I subscribe to when I want a callback for collection selections? When I select a different collection in the outliner. There is:

bpy.types.LayerCollections

But there are no 'active' or 'selected' properties for this certain type.

Any help would be appreciated.

$\endgroup$

1 Answer 1

3
$\begingroup$

The msgbus is quite limited, I don't think there is a solution using that, but you can use the depsgraph_update_post application handler instead:

import bpy

def on_depsgraph_update_post(scene, depsgraph):
    selected_items = None
    area  = next(area for area in bpy.context.window.screen.areas if area.type == 'OUTLINER')
    
    with bpy.context.temp_override(
        window=bpy.context.window,
        area=area,
        region=next(region for region in area.regions if region.type == 'WINDOW'),
        screen=bpy.context.window.screen
    ):
        selected_items = bpy.context.selected_ids

    for item in reversed(selected_items):
        if isinstance(item, bpy.types.Collection):
            print("You have selected a collection:", item.name)
            return

bpy.app.handlers.depsgraph_update_post.clear()
bpy.app.handlers.depsgraph_update_post.append(on_depsgraph_update_post)
 
$\endgroup$
2
  • 2
    $\begingroup$ Thats what Im using currently, I should have mentioned that. The downside is that it also fires events Im not interested in. I guess theres no cleaner way. I.e.: When you are using a UI Panel you can just push your function into 'poll' call. Thank you Harry! $\endgroup$
    – rbx 775
    Sep 4 at 12:07
  • 2
    $\begingroup$ @rbx775 yes I agree. there's lots of different events that fire it, which is kind of uncool, i hope they will change it to make events and callbacks more modular $\endgroup$ Sep 4 at 14:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .