Add Operator to Right Click Menu for Operators

Hi there! I’m wanting to add a menu item to the menu when you right-click on an operator. Does anyone know what the menu’s id name (or whatever it is called) is so I can append to it?

_RightClickMenu

See bpy.types.Menu.

The short story is the context menu is drawn internally. However, developers have allowed a way of inserting draw functions after the menu is populated.

import bpy

class WM_MT_button_context(bpy.types.Menu):
    bl_label = ""

    # Leave empty for compatibility.
    def draw(self, context):
        pass

# Your draw function.
def draw(self, context):
    layout = self.layout
    layout.label(text="Insert something here")

if __name__ == "__main__":
    # Register menu only if it doesn't already exist.
    rcmenu = getattr(bpy.types, "WM_MT_button_context", None)
    if rcmenu is None:
        bpy.utils.register_class(WM_MT_button_context)
        rcmenu = WM_MT_button_context

    # Retrieve a python list for inserting draw functions.
    draw_funcs = rcmenu._dyn_ui_initialize()
    draw_funcs.append(draw)

From the code above you’ll see we’re registering a menu called exactly WM_MT_button_context, which Blender specifically looks for to execute any custom hooks. Then add the draw function to the list.

If you want to provide compatibility with other addons that also use the menu, do as the example above where you check if the menu exists in bpy.types before attempting to register it. To unregister your draw function, use draw_funcs.remove(draw)

image

1 Like

That’s it!!! Thank you so much! I do apologize that it was right there on the api docs. I didn’t even think to check it. I didn’t really think it would something they had written about. Silly me! Again, thank you so much!!!