To make changes to menus in Blender UI, for example, add new items or overriding the menu completely, we first need to know the class of the menu which we need to change.
Menu class definitions can in some cases be quickly accessed by right-clicking on them, and switching to source code by selecting the “Edit source” in the pop-up menu. However, this method does not work with all menus in the Blender interface.
A complete list of menu classes can be obtained from the bpy.types list.
Let’s define a generator function that, from the entire list of types, will give us classes inherited from the base “menu” type bpy.types.Menu:
1 2 3 4 5 6 |
def _all_menu_classes(): for type in dir(bpy.types): type_cls = getattr(bpy.types, type) if hasattr(type_cls, '__mro__'): if bpy.types.Menu in type_cls.__mro__: yield type_cls, type_cls.bl_rna.name |
The __mro__ tuple provides access to all classes that the current class inherits from. By checking to see if it has the bpy.types.Menu class, we end up with only the classes of the type we need.
Let’s loop through the generated values and display the list of menu classes in the console:
1 2 |
for cls in _all_menu_classes(): print(cls) |
As a result, we got a list of classes with their identifiers:
1 2 3 4 5 6 7 8 |
(<class 'bl_ui.space_topbar.TOPBAR_MT_file'>, 'TOPBAR_MT_file') (<class 'bl_ui.space_topbar.TOPBAR_MT_file_cleanup'>, 'TOPBAR_MT_file_cleanup') (<class 'bl_ui.space_topbar.TOPBAR_MT_file_context_menu'>, 'TOPBAR_MT_file_context_menu') (<class 'bl_ui.space_topbar.TOPBAR_MT_file_defaults'>, 'TOPBAR_MT_file_defaults') (<class 'bl_ui.space_topbar.TOPBAR_MT_file_export'>, 'TOPBAR_MT_file_export') (<class 'bl_ui.space_topbar.TOPBAR_MT_file_external_data'>, 'TOPBAR_MT_file_external_data') (<class 'bl_ui.space_topbar.TOPBAR_MT_file_import'>, 'TOPBAR_MT_file_import') ... |
Once we found the desired identifier in the list, we can use it to modify the Blender menus.