menu to select an enum from an RNA path:
authorCampbell Barton <ideasman42@gmail.com>
Tue, 15 Feb 2011 18:12:41 +0000 (18:12 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Tue, 15 Feb 2011 18:12:41 +0000 (18:12 +0000)
eg,
   bpy.ops.wm.context_menu_enum(data_path="scene.tool_settings.vertex_paint.brush.stroke_method")

This saves us defining operators only for menus.

release/scripts/op/wm.py

index cbc9e3cd55f7783a2eaab1fe69417429deca456f..0210dc912df59b198fa8425c57358aba04334b98 100644 (file)
@@ -384,6 +384,38 @@ class WM_OT_context_cycle_array(bpy.types.Operator):
         return {'FINISHED'}
 
 
+class WM_MT_context_menu_enum(bpy.types.Menu):
+    bl_label = ""
+    data_path = ""  # BAD DESIGN, set from operator below.
+    def draw(self, context):
+        data_path = self.data_path
+        value = context_path_validate(bpy.context, data_path)
+        if value is Ellipsis:
+            return {'PASS_THROUGH'}
+        base_path, prop_string = data_path.rsplit(".", 1)
+        value_base = context_path_validate(context, base_path)
+        
+        values = [(i.name, i.identifier) for i in value_base.bl_rna.properties[prop_string].items]
+
+        for name, identifier in values:
+            prop = self.layout.operator("wm.context_set_enum", text=name)
+            prop.data_path = data_path
+            prop.value = identifier
+
+
+class WM_OT_context_menu_enum(bpy.types.Operator):
+    bl_idname = "wm.context_menu_enum"
+    bl_label = "Context Enum Menu"
+    bl_options = {'UNDO'}
+    data_path = rna_path_prop
+    
+    def execute(self, context):
+        data_path = self.data_path
+        WM_MT_context_menu_enum.data_path = data_path
+        bpy.ops.wm.call_menu(name="WM_MT_context_menu_enum")
+        return {'PASS_THROUGH'}
+
+
 class WM_OT_context_set_id(bpy.types.Operator):
     '''Toggle a context value.'''
     bl_idname = "wm.context_set_id"