1
$\begingroup$

I am trying to create a custom entry just after the "Object" menu of the 3D View, but I can only append or prepend it, meaning it is at the begining or at the end of that particular space. enter image description here

Here is the code:

import bpy

class BlablaMenu(bpy.types.Menu):
    bl_label = "blabla"
    bl_idname = "VIEW3D_MT_blabla_menu"

    def draw(self, context):
        layout = self.layout            
        layout.operator("object.text_add")

def draw_item(self, context):
    layout = self.layout
    layout.menu(BlablaMenu.bl_idname)

def register():
    bpy.utils.register_class(BlablaMenu)
    bpy.types.VIEW3D_HT_header.prepend(draw_item)

def unregister():
    bpy.types.VIEW3D_HT_header.remove(draw_item)
    bpy.utils.unregister_class(BlablaMenu)

if __name__ == "__main__":
    register()

I am aware of similar questions, but the answers are outdated. I also tried to understand how RetopoFlow and Boxcutter are solving this problem, but I can't find the code that draws those menus in order to reverse engineer it. Another approach would be to remove all the menus, create my own and re-create te deleted menus, but I don't know how to do that either.

Thank you and happy new year!

$\endgroup$
3
  • $\begingroup$ Hello ! You can indeed only prepend or append your menus to existing ones. AFAIK the registration order of addons depends on the order in which you enabled them. Unless you specifically unregister and re-register the other addons you won't be able to fit your menu inbetween existing menu entries. If you have python tooltips enabled in the preferences you can get the menu class name by hovering over the menu button $\endgroup$
    – Gorgious
    Jan 2 at 8:07
  • $\begingroup$ I get that. I also know how to unregister and addon from code, but I have no clue how to unregister an existing Blender menu. Any idea? $\endgroup$
    – radoo
    Jan 2 at 20:48
  • $\begingroup$ It's the same for builtin menus. If you have python dev tools enabled, you can right click on a menu item and show the layout code in the text editor. usually inside space_view3d.py I believe. you can treat it as a regular addon script $\endgroup$
    – Gorgious
    Jan 2 at 21:58

0

You must log in to answer this question.

Browse other questions tagged .