1 year ago

#75569

test-img

Joshua de Haan

Blender python scripting UI - How to refresh outliner header when a different collection becomes active?

My script changes the outliner header content depending on the active collection, however it will only refresh when I move my mouse around the screen. Obviously I want the header to refresh as soon as a different collection becomes active.

I tried subscribing to bpy.context.collection with Message Bus to figure out when a different collection becomes active but that does not seem to generate an event. Furthermore I don't know how to actually force a redraw without resorting to using ugly hacks, is there a good way to do it?

As a visual reference I've included some screenshots down below.

We start off like this..

1

Then I press the button that creates a custom collection..

2

Making that collection active changes nothing in the header..

3

Until I move my mouse over the screen..

4

Here is the current code;

# bl_info stuff here
# ...

import bpy

class EXAMPLE_OT_custom_collection_new(bpy.types.Operator):
    """Create a custom collection that has an adjustable 'foobar' variable"""
    bl_idname = "example.custom_collection_new"
    bl_label = "Custom collection"
    def execute(self, context):
        # Get active collection
        activeCollection = context.collection

        # Create custom collection
        cc = bpy.data.collections.new(f"Custom collection.000")
        cc.color_tag = "COLOR_01"
        
        # Give custom collection a variable 'foobar' with a default value
        cc['foobar'] = 100

        # Place this new collection within the active collection
        activeCollection.children.link(cc)
        
        return {'FINISHED'}

def context_draw_outliner_foobar_buttons(self, context):
    activeCollection = context.collection
    layout = self.layout
     
    if "foobar" in activeCollection:
        layout.prop(bpy.data.collections[activeCollection.name], '["foobar"]')
    else:
        layout.operator("EXAMPLE_OT_custom_collection_new", text="Custom", icon='COLLECTION_COLOR_01')

def register():
    bpy.utils.register_class(EXAMPLE_OT_custom_collection_new)
    bpy.types.OUTLINER_HT_header.append(context_draw_outliner_foobar_buttons)

def unregister():
    bpy.utils.unregister_class(EXAMPLE_OT_custom_collection_new)
    bpy.types.OUTLINER_HT_header.remove(context_draw_outliner_foobar_buttons)

if __name__ == "__main__":
    register()

python

user-interface

blender

0 Answers

Your Answer

Accepted video resources