Check if a draw function exists in a Menu before registering

How can I check if a draw function exists in a Menu before registering? When I write prototypes in the Text Editor, I get multiple draw functions added in Blender’s UI.

def menu_func(self, context):
    layout = self.layout
    # draw code here

def register():
    bpy.types.VIEW3D_MT_object.append(menu_func)

Though it has an append method, bpy.types.VIEW3D_MT_object is of type bpy_types.RNAMeta and is not iterable.

EDIT: I found this post, but the remove method isn’t working for me.

Inspired by this post, I am iterating through bpy.types.VIEW3D_MT_object._dyn_ui_initialize() and comparing it to my draw function’s name. If it doesn’t exist, I’m appending it to the Menu:

def object_menu(self, context):
    self.layout.label(text="Test")

def register():
    omenu = next(iter([f for f in bpy.types.VIEW3D_MT_object._dyn_ui_initialize() if f.__name__ == object_menu.__name__]),None)
    if not omenu:
        bpy.types.VIEW3D_MT_object.append(object_menu)
2 Likes