Merge branch 'blender-v3.3-release'

This commit is contained in:
Luca Rood 2022-08-18 15:26:30 +02:00
commit 9a65dca37c
4 changed files with 35 additions and 25 deletions

View File

@ -3,8 +3,8 @@ Extending the Button Context Menu
+++++++++++++++++++++++++++++++++
This example enables you to insert your own menu entry into the common
right click menu that you get while hovering over a value field,
color, string, etc.
right click menu that you get while hovering over a UI button (e.g. operator,
value field, color, string, etc.)
To make the example work, you have to first select an object
then right click on an user interface element (maybe a color in the
@ -14,7 +14,6 @@ Executing the operator will then print all values.
"""
import bpy
from bpy.types import Menu
def dump(obj, text):
@ -47,36 +46,20 @@ class WM_OT_button_context_test(bpy.types.Operator):
return {'FINISHED'}
# This class has to be exactly named like that to insert an entry in the right click menu
class WM_MT_button_context(Menu):
bl_label = "Unused"
def draw(self, context):
pass
def menu_func(self, context):
def draw_menu(self, context):
layout = self.layout
layout.separator()
layout.operator(WM_OT_button_context_test.bl_idname)
classes = (
WM_OT_button_context_test,
WM_MT_button_context,
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.WM_MT_button_context.append(menu_func)
bpy.utils.register_class(WM_OT_button_context_test)
bpy.types.UI_MT_button_context_menu.append(draw_menu)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
bpy.types.WM_MT_button_context.remove(menu_func)
bpy.types.UI_MT_button_context_menu.remove(draw_menu)
bpy.utils.unregister_class(WM_OT_button_context_test)
if __name__ == "__main__":

View File

@ -239,3 +239,23 @@ class UI_MT_list_item_context_menu(bpy.types.Menu):
bpy.utils.register_class(UI_MT_list_item_context_menu)
class UI_MT_button_context_menu(bpy.types.Menu):
"""
UI button context menu definition. Scripts can append/prepend this to
add own operators to the context menu. They must check context though, so
their items only draw in a valid context and for the correct buttons.
"""
bl_label = "List Item"
bl_idname = "UI_MT_button_context_menu"
def draw(self, context):
# Draw menu entries created with the legacy `WM_MT_button_context` class.
# This is deprecated, and support will be removed in a future release.
if hasattr(bpy.types, "WM_MT_button_context"):
self.layout.menu_contents("WM_MT_button_context")
bpy.utils.register_class(UI_MT_button_context_menu)

View File

@ -1230,7 +1230,7 @@ bool ui_popup_context_menu_for_button(bContext *C, uiBut *but, const wmEvent *ev
}
}
MenuType *mt = WM_menutype_find("WM_MT_button_context", true);
MenuType *mt = WM_menutype_find("UI_MT_button_context_menu", true);
if (mt) {
UI_menutype_draw(C, mt, uiLayoutColumn(layout, false));
}

View File

@ -3028,7 +3028,14 @@ void uiItemMContents(uiLayout *layout, const char *menuname)
if (WM_menutype_poll(C, mt) == false) {
return;
}
bContextStore *previous_ctx = CTX_store_get(C);
UI_menutype_draw(C, mt, layout);
/* Restore context that was cleared by `UI_menutype_draw`. */
if (layout->context) {
CTX_store_set(C, previous_ctx);
}
}
void uiItemDecoratorR_prop(uiLayout *layout, PointerRNA *ptr, PropertyRNA *prop, int index)