[Bf-blender-cvs] [8a799b00f8f] blender-v3.3-release: Fix T100423: Addon's custom context menu entries get overridden by other addons

Luca Rood noreply at git.blender.org
Thu Aug 18 14:55:44 CEST 2022


Commit: 8a799b00f8fa28433ba44cfec09757f77a46ae0d
Author: Luca Rood
Date:   Thu Aug 18 14:42:48 2022 +0200
Branches: blender-v3.3-release
https://developer.blender.org/rB8a799b00f8fa28433ba44cfec09757f77a46ae0d

Fix T100423: Addon's custom context menu entries get overridden by other addons

This introduces a new `UI_MT_button_context_menu` class which is
registered at startup. Addons can append/prepend draw functions to this
class, in order to add their custom context menu entries.

The new class replaces the old `WM_MT_button_context` class, thus
requiring a small change in addons using this feature. This is done
because addons were previously required to register the class
themselves, which caused addons to override each other's context menu
entries.

Now the class registration is handled by Blender, and addons need only
append their draw functions. The new class name ensures that addons
using the old method don't override menu entries made using the new
class.

Menu entries added with the legacy `WM_MT_button_context` class are
still drawn for backwards compatibility, but this class must not be used
going forward, as any addon using it still runs the risk of having its
menu entries overridden, and support for the legacy class is subject to
removal in a future version.

Reviewed By: campbellbarton

Maniphest Tasks: T100423

Differential Revision: https://developer.blender.org/D15702

===================================================================

M	doc/python_api/examples/bpy.types.Menu.4.py
M	release/datafiles/locale
M	release/scripts/addons
M	release/scripts/addons_contrib
M	release/scripts/startup/bl_ui/__init__.py
M	source/blender/editors/interface/interface_context_menu.c
M	source/blender/editors/interface/interface_layout.c
M	source/tools

===================================================================

diff --git a/doc/python_api/examples/bpy.types.Menu.4.py b/doc/python_api/examples/bpy.types.Menu.4.py
index 869def8bfe0..4d1ae2d4a19 100644
--- a/doc/python_api/examples/bpy.types.Menu.4.py
+++ b/doc/python_api/examples/bpy.types.Menu.4.py
@@ -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__":
diff --git a/release/datafiles/locale b/release/datafiles/locale
index a2eb5078914..4d67fb6e277 160000
--- a/release/datafiles/locale
+++ b/release/datafiles/locale
@@ -1 +1 @@
-Subproject commit a2eb507891449a0b67582be9561840075513661d
+Subproject commit 4d67fb6e2773619392b3d2099188ae742ef9662a
diff --git a/release/scripts/addons b/release/scripts/addons
index 7a8502871c3..32baafe44dc 160000
--- a/release/scripts/addons
+++ b/release/scripts/addons
@@ -1 +1 @@
-Subproject commit 7a8502871c34db0343cc7de52d6b49b15a84238a
+Subproject commit 32baafe44dc56edd1baf6e5a16b4439ded8238f2
diff --git a/release/scripts/addons_contrib b/release/scripts/addons_contrib
index 95107484d07..42da56aa737 160000
--- a/release/scripts/addons_contrib
+++ b/release/scripts/addons_contrib
@@ -1 +1 @@
-Subproject commit 95107484d076bc965239942e857c83433bfa86d7
+Subproject commit 42da56aa73726710107031787af5eea186797984
diff --git a/release/scripts/startup/bl_ui/__init__.py b/release/scripts/startup/bl_ui/__init__.py
index c4e3df469b7..a57a2cc5a4c 100644
--- a/release/scripts/startup/bl_ui/__init__.py
+++ b/release/scripts/startup/bl_ui/__init__.py
@@ -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)
diff --git a/source/blender/editors/interface/interface_context_menu.c b/source/blender/editors/interface/interface_context_menu.c
index 16d228708eb..7ed9488950e 100644
--- a/source/blender/editors/interface/interface_context_menu.c
+++ b/source/blender/editors/interface/interface_context_menu.c
@@ -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));
   }
diff --git a/source/blender/editors/interface/interface_layout.c b/source/blender/editors/interface/interface_layout.c
index 3465373c85d..94d17ed3c88 100644
--- a/source/blender/editors/interface/interface_layout.c
+++ b/source/blender/editors/interface/interface_layout.c
@@ -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)
diff --git a/source/tools b/source/tools
index da8bdd7244c..548055f4021 160000
--- a/source/tools
+++ b/source/tools
@@ -1 +1 @@
-Subproject commit da8bdd7244c7b6c2eadf4c949ff391d0cc430275
+Subproject commit 548055f40213c775a6b77025525c91e8466e70d6



More information about the Bf-blender-cvs mailing list