How do I inject python UI elements into existing UI?

I’m trying to customize Blender UI putting here and there little panels and buttons I need to have around quickly, and I was able to create a couple of custom panels. Needless to say, I’m new to python, and I’m “coding” copy-pasting stuff :sweat_smile:
Now I need to put some buttons and menus into the 3dview header. How do I do it in a new script (eventually an addon)?
Edit: and just in case if exists, is there a way to remove things from UI?

I’m still only coding by copy and pasting myself, but I think I know this one.

For placing menus and buttons in the header you can reference the ui_menu template (Text Editor > Templates > Python > UI Menu)

The custom menu shown there is added to the header of the info panel, if you execute the script.
I played around a little and you can add the custom menu from that script the the 3D view header by changing the line

bpy.types.INFO_HT_header.append(draw_item)

in the register function to

bpy.types.VIEW3D_HT_header.append(draw_item)

to add it to the header menu bar, where the other menus in the 3d viewport are located (View, Select, Add…).
If you want to put your menu in the ‚real‘ header (where the selection mode, transform orientation and so on located) you can use.

bpy.types.VIEW3D_HT_tool_header.append(draw_item)

Don’t forget to also change the change part in the unregister function, where you remove your menu accordingly.

Also using .append will put your menu at the end of the header, while using .prepend will add it at the beginning. From my research there does not seem to be an easy way to just insert your own stuff in the header.

I hope this is helpful enough so you can adapt it to your script :slight_smile: good luck with your addon!

1 Like

Thanks a lot, very nice!
(btw calling this an addon sounds hyperbole, it’s just a few operators collected in my own way)