Cleanup: menu example

remove some redundant checks, imports
This commit is contained in:
Campbell Barton 2017-04-29 16:20:06 +10:00
parent 8f66d6826b
commit b5298bf819

@ -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 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, right click menu that you get while hovering over a value field,
color, string, etc. color, string, etc.
To make the example work, you have to first select an object 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 then right click on an user interface element (maybe a color in the
material properties) and choose "Execute custom action". 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)
Executing the operator will then print all values.
""" """
import bpy import bpy
from bpy.types import Header, Menu, Panel from bpy.types import Menu
def dump(obj, text): def dump(obj, text):
print('-'*40, text, '-'*40)
for attr in dir(obj): for attr in dir(obj):
if hasattr( obj, attr ): print("%r.%s = %s" % (obj, attr, getattr(obj, attr)))
print( "obj.%s = %s" % (attr, getattr(obj, attr)))
class TEST_OT_Rmb(bpy.types.Operator):
class WM_OT_button_context_test(bpy.types.Operator):
"""Right click entry test""" """Right click entry test"""
bl_idname = "object.rmb_test_op" bl_idname = "wm.button_context_test"
bl_label = "Execute custom action" bl_label = "Run Context Test"
@classmethod @classmethod
def poll(cls, context): def poll(cls, context):
return context.active_object is not None return context.active_object is not None
def execute(self, context): def execute(self, context):
if hasattr(context, 'button_pointer'): value = getattr(context, "button_pointer", None)
btn = context.button_pointer if value is not None:
dump(btn, 'button_pointer') dump(value, "button_pointer")
if hasattr(context, 'button_prop'): value = getattr(context, "button_prop", None)
prop = context.button_prop if value is not None:
dump(prop, 'button_prop') dump(value, "button_prop")
value = getattr(context, "button_operator", None)
if value is not None:
dump(value, "button_operator")
if hasattr(context, 'button_operator'):
op = context.button_operator
dump(op, 'button_operator')
return {'FINISHED'} return {'FINISHED'}
# This class has to be exactly named like that to insert an entry in the right click menu # This class has to be exactly named like that to insert an entry in the right click menu
class WM_MT_button_context(Menu): class WM_MT_button_context(Menu):
bl_label = "Add Viddyoze Tag" bl_label = "Unused"
def draw(self, context): def draw(self, context):
pass pass
def menu_func(self, context): def menu_func(self, context):
layout = self.layout layout = self.layout
layout.separator() layout.separator()
layout.operator("object.rmb_test_op") layout.operator(WM_OT_button_context_test.bl_idname)
classes = ( classes = (
TEST_OT_Rmb, WM_OT_button_context_test,
WM_MT_button_context, WM_MT_button_context,
) )
def register(): def register():
for cls in classes: for cls in classes:
bpy.utils.register_class(cls) bpy.utils.register_class(cls)
bpy.types.WM_MT_button_context.append(menu_func) bpy.types.WM_MT_button_context.append(menu_func)
def unregister(): def unregister():
for cls in classes: for cls in classes:
bpy.utils.unregister_class(cls) bpy.utils.unregister_class(cls)
bpy.types.WM_MT_button_context.remove(menu_func) bpy.types.WM_MT_button_context.remove(menu_func)
if __name__ == "__main__": if __name__ == "__main__":
register() register()