From d6963630dc64e94b4f2da21d43c1985f77be88aa Mon Sep 17 00:00:00 2001 From: Thomas Beck Date: Fri, 28 Apr 2017 13:01:03 +0200 Subject: Implementation of custom python entries in all right click menus Hi Guys, as one of my clients needs the possibility to have custom menu entries in the general right click menu (all over Blender: in the node editor, properties, toolbars,..) I talked with Campbell about expanding our hard coded menu a bit. This is the outcome. As I only need those two, I support currently a button_prop and a button_pointer. {F540397} I tested the changes with a custom script where I added a custom entry and executed an operator on click - it seems to work exactly how it's intended to. The script: {F540435} As I'm not too experienced in rna stuff I would really appreciate any review. Thanks very much Campbell for his open ears & help on this issue! Reviewers: campbellbarton, mont29 Reviewed By: campbellbarton, mont29 Subscribers: sybren, mont29 Tags: #addons Differential Revision: https://developer.blender.org/D2612 --- doc/python_api/examples/bpy.types.Menu.4.py | 81 +++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 doc/python_api/examples/bpy.types.Menu.4.py (limited to 'doc') diff --git a/doc/python_api/examples/bpy.types.Menu.4.py b/doc/python_api/examples/bpy.types.Menu.4.py new file mode 100644 index 00000000000..a59c710c8e7 --- /dev/null +++ b/doc/python_api/examples/bpy.types.Menu.4.py @@ -0,0 +1,81 @@ +""" +Custom item in the right click 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. + +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 +material properties) and choose "Execute custom action". + +Executing the operator will then dump all values directly to a +console, so make sure to open a terminal by clicking on +"Help >> Toggle System Console" or execute Blender directly +from a terminal on your system of choice) + +""" + +import bpy +from bpy.types import Header, Menu, Panel + +def dump(obj, text): + print('-'*40, text, '-'*40) + for attr in dir(obj): + if hasattr( obj, attr ): + print( "obj.%s = %s" % (attr, getattr(obj, attr))) + +class TEST_OT_Rmb(bpy.types.Operator): + """Right click entry test""" + bl_idname = "object.rmb_test_op" + bl_label = "Execute custom action" + + @classmethod + def poll(cls, context): + return context.active_object is not None + + def execute(self, context): + if hasattr(context, 'button_pointer'): + btn = context.button_pointer + dump(btn, 'button_pointer') + + if hasattr(context, 'button_prop'): + prop = context.button_prop + dump(prop, 'button_prop') + + if hasattr(context, 'button_operator'): + op = context.button_operator + dump(op, 'button_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 = "Add Viddyoze Tag" + + def draw(self, context): + pass + +def menu_func(self, context): + layout = self.layout + layout.separator() + layout.operator("object.rmb_test_op") + +classes = ( + TEST_OT_Rmb, + WM_MT_button_context, +) + +def register(): + for cls in classes: + bpy.utils.register_class(cls) + bpy.types.WM_MT_button_context.append(menu_func) + +def unregister(): + for cls in classes: + bpy.utils.unregister_class(cls) + bpy.types.WM_MT_button_context.remove(menu_func) + +if __name__ == "__main__": + register() \ No newline at end of file -- cgit v1.2.3 From b5298bf81999a0845c6bb98cf8520806271da14b Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sat, 29 Apr 2017 16:20:06 +1000 Subject: Cleanup: menu example remove some redundant checks, imports --- doc/python_api/examples/bpy.types.Menu.4.py | 68 ++++++++++++++--------------- 1 file changed, 34 insertions(+), 34 deletions(-) (limited to 'doc') diff --git a/doc/python_api/examples/bpy.types.Menu.4.py b/doc/python_api/examples/bpy.types.Menu.4.py index a59c710c8e7..04ceb6654da 100644 --- a/doc/python_api/examples/bpy.types.Menu.4.py +++ b/doc/python_api/examples/bpy.types.Menu.4.py @@ -1,81 +1,81 @@ """ -Custom item in the right click menu -+++++++++++++++++++++++++++++++++++ +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, +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. 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 -material properties) and choose "Execute custom action". - -Executing the operator will then dump all values directly to a -console, so make sure to open a terminal by clicking on -"Help >> Toggle System Console" or execute Blender directly -from a terminal on your system of choice) +then right click on an user interface element (maybe a color in the +material properties) and choose *Execute Custom Action*. +Executing the operator will then print all values. """ import bpy -from bpy.types import Header, Menu, Panel +from bpy.types import Menu + def dump(obj, text): - print('-'*40, text, '-'*40) for attr in dir(obj): - if hasattr( obj, attr ): - print( "obj.%s = %s" % (attr, getattr(obj, attr))) + print("%r.%s = %s" % (obj, attr, getattr(obj, attr))) + -class TEST_OT_Rmb(bpy.types.Operator): +class WM_OT_button_context_test(bpy.types.Operator): """Right click entry test""" - bl_idname = "object.rmb_test_op" - bl_label = "Execute custom action" + bl_idname = "wm.button_context_test" + bl_label = "Run Context Test" @classmethod def poll(cls, context): return context.active_object is not None def execute(self, context): - if hasattr(context, 'button_pointer'): - btn = context.button_pointer - dump(btn, 'button_pointer') - - if hasattr(context, 'button_prop'): - prop = context.button_prop - dump(prop, 'button_prop') - - if hasattr(context, 'button_operator'): - op = context.button_operator - dump(op, 'button_operator') - + value = getattr(context, "button_pointer", None) + if value is not None: + dump(value, "button_pointer") + + value = getattr(context, "button_prop", None) + if value is not None: + dump(value, "button_prop") + + value = getattr(context, "button_operator", None) + if value is not None: + dump(value, "button_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 = "Add Viddyoze Tag" + bl_label = "Unused" def draw(self, context): pass + def menu_func(self, context): layout = self.layout layout.separator() - layout.operator("object.rmb_test_op") + layout.operator(WM_OT_button_context_test.bl_idname) classes = ( - TEST_OT_Rmb, + 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) + def unregister(): for cls in classes: bpy.utils.unregister_class(cls) bpy.types.WM_MT_button_context.remove(menu_func) if __name__ == "__main__": - register() \ No newline at end of file + register() -- cgit v1.2.3