1
$\begingroup$

Is it possible to add a button via python to here?

enter image description here

$\endgroup$
3
$\begingroup$

The header of the outliner panel can be accessed through bpy.types.OUTLINER_HT_header. It is possible to add additional buttons to the header by appending or prepending a draw function which assigns the relevant operators to the layout. The draw function could look like the following, if your operator class is called EXAMPLE_OT_something.

def draw(self, context):
        self.layout.operator(EXAMPLE_OT_something.bl_idname)

If the operator has been registered the draw function can be appended to the header. This places the button at the end of the header. Using .prepend would place it at the beginning.

bpy.types.OUTLINER_HT_header.append(draw)

Below is an example add-on that demonstrates the concept.

bl_info = {
    "name": "Outliner Header Button",
    "author": "Robert Guetzkow",
    "version": (1, 0, 0),
    "blender": (2, 81, 0),
    "location": "Outliner",
    "description": "Adds an example button to the outliner header.",
    "wiki_url": "",
    "category": "Outliner"}

import bpy


class EXAMPLE_OT_something(bpy.types.Operator):
    bl_idname = "example.something"
    bl_label = "Do something"
    bl_description = "This operator does something"
    bl_options = {"REGISTER"}

    def execute(self, context):
        print("Hello")
        return {"FINISHED"}


def draw(self, context):
    self.layout.operator(EXAMPLE_OT_something.bl_idname)


classes = (EXAMPLE_OT_something,)


def register():
    for cls in classes:
        bpy.utils.register_class(cls)
        
    bpy.types.OUTLINER_HT_header.append(draw)


def unregister():
    bpy.types.OUTLINER_HT_header.remove(draw)
    
    for cls in classes:
        bpy.utils.unregister_class(cls)


if __name__ == "__main__":
    register()
$\endgroup$
1
  • $\begingroup$ Changing the order of entries requires more work though. It's not impossible but an add-on should probably avoid that (see this old answer) $\endgroup$ – Robert Gützkow Sep 28 '20 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.