Fully override context menu in Blender

While we can append additional items to context menus in Blender, context menus can also be completely redefined. Instead of the basic one, we can get a completely custom context menu with any set and order of items we need.

First, let’t define a function for rendering a custom menu with a set of operators that will be displayed as items in this new menu.

For example, we can take a set of operators that add basic mesh primitives to the scene.

The structure of the function is similar to the structure of the “draw” function in custom UI panel classes. Menu items are created by calling the “operator” function of the “self.layout” object.

Now we can completely redefine any context menu by replacing the pointer to its “draw” function with a pointer to our custom draw function.

For example, let’s redefine the main context menu of the 3D viewport.

Now, when we right-click in the 3D viewport area, instead of the basic context menu, our custom menu will be displayed for quickly adding mesh primitives to the scene.

5 1 vote
Article Rating
Subscribe
Notify of
guest

4 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Jan Javůrek
Jan Javůrek
2 years ago

Hi, thanks for this awesome tutorial. I have a specific issue though.

I would like to completely override UIList’s context menu with a custom menu, but only for one specific UIList, that I am adding in my addon. For the rest already present UIlists (UVmaps, Vertex color, etc.) in Blender I wanna keep the original ones. Do you have any idea on how to do this?

Thank you

NikitaD
Admin
2 years ago
Reply to  Jan Javůrek

Hi!
Try the UI_MT_list_item_context_menu
To find what UIList user clicked, try the way from the official docs
https://docs.blender.org/api/current/bpy.types.Menu.html#extending-the-button-context-menu
using context.button_pointer and context.button_prop properties

Jan Javůrek
Jan Javůrek
2 years ago
Reply to  Nikita

THx for advice…I have looked into that. I also got the example code from the Docs somehow working to dump some of the context information when I click on new button in the UIlist context menu…but I have no idea how to use it further to achieve what I want….I am rather “noob” in this area…I started with the python scripting relatively recently 🙁

Last edited 2 years ago by Jan Javůrek