diff options
author | Campbell Barton <ideasman42@gmail.com> | 2017-04-29 09:32:25 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2017-04-29 09:32:30 +0300 |
commit | 33a5248b6c82d7930089544be10480fa0a25aa80 (patch) | |
tree | 8ee84267fae97ac57bdd9d6b3c396e793d77d6fe /doc | |
parent | de9e45a88bd8ceee4af1ea907bb93a3bf22024b9 (diff) | |
parent | 6d73e2d3cf3c9a8fa4c73e04d02caecfd716e962 (diff) |
Merge branch 'master' into blender2.8
Diffstat (limited to 'doc')
-rw-r--r-- | doc/python_api/examples/bpy.types.Menu.4.py | 81 |
1 files changed, 81 insertions, 0 deletions
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..04ceb6654da --- /dev/null +++ b/doc/python_api/examples/bpy.types.Menu.4.py @@ -0,0 +1,81 @@ +""" +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. + +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 print all values. +""" + +import bpy +from bpy.types import Menu + + +def dump(obj, text): + for attr in dir(obj): + print("%r.%s = %s" % (obj, attr, getattr(obj, attr))) + + +class WM_OT_button_context_test(bpy.types.Operator): + """Right click entry test""" + 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): + 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 = "Unused" + + def draw(self, context): + pass + + +def menu_func(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) + + +def unregister(): + for cls in classes: + bpy.utils.unregister_class(cls) + bpy.types.WM_MT_button_context.remove(menu_func) + +if __name__ == "__main__": + register() |