5
$\begingroup$

I've created a menu using an Enum Property. Now I looking for a way to update the menu every time I call an specific operator: Load_New_List_Operator.

import bpy 

from bpy.props import (EnumProperty,
                       PointerProperty,)
from bpy.types import (Panel,
                       PropertyGroup,)


List = ['1','2','3','4']

class menu_list(PropertyGroup):
    menu = EnumProperty(
        name="menu:",
        description="Choice item menu",
        items = (List),     
        )

def Import_List():
    New_List = ['A','B','C','D']
    return New_List

class Load_New_List_Operator(bpy.types.Operator):
    bl_idname = "object.load_new_list_operator"
    bl_label = "Load new list items"

    def execute(self, context):
        Replace_List = Import_List()

        #ERROR
        menu_list.menu[2] = Replace_List #ERROR tuple does not suport item assignment
        return {'FINISHED'}


def register():
    bpy.utils.register_class(Load_New_List_Operator)
    bpy.utils.register_module(__name__)
    bpy.types.Scene.menu_tool = PointerProperty(type=menu_list)


def unregister():
    bpy.utils.unregister_class(Load_New_List_Operator)
    bpy.utils.unregister_module(__name__)
    del bpy.types.Scene.menu_tool

if __name__ == "__main__":
    register()
    bpy.ops.object.load_new_list_operator()
    print (menu_list.menu)
$\endgroup$
5
  • $\begingroup$ maybe set items to a function instead of a list like items = get_items $\endgroup$
    – Chebhou
    Commented Apr 25, 2015 at 15:48
  • $\begingroup$ I don't know if I've do it properly but seems like it doesn't work. Thank you @Chebhou $\endgroup$ Commented Apr 25, 2015 at 16:00
  • $\begingroup$ Update should replace the whole list or adding items? $\endgroup$
    – p2or
    Commented Apr 25, 2015 at 16:29
  • $\begingroup$ @poor yes, the Update must replace the whole list. $\endgroup$ Commented Apr 25, 2015 at 16:36
  • $\begingroup$ related: blender.stackexchange.com/a/7408/47 $\endgroup$
    – zeffii
    Commented Apr 25, 2015 at 16:55

1 Answer 1

1
$\begingroup$

Hopefully this is concise and shows how to work with the callback and the custom PropertyGroup. A subtle detail is that each property in the group must have a name (never undefined or None). See PropertyGroup docs

Where the information that populates the menu is taken from, be it a list, or some function call is none of bpy's business.

import random
import bpy 

from bpy.props import (EnumProperty, PointerProperty)
from bpy.types import (Operator, Panel, PropertyGroup)

own_name_space = {}
own_name_space['list 0'] = ['m']
own_name_space['list 1'] = ['a', 'b', 'c']
own_name_space['list 2'] = ['d', 'e', 'f']
own_name_space['list 3'] = ['g', 'h', 'i']


def get_menu_items(self, context):
    items = [(t, t, "") for t in own_name_space[self.menu_key]]
    return items


class CustomMenuProps(PropertyGroup):

    menu_key = bpy.props.StringProperty(
        name='custom menu key',
        default='list 0',
        options={'HIDDEN'})

    some_selection = EnumProperty(
        name='custom menu result',
        items=get_menu_items,
        description='choose menu item')


class CallbackChangeList(Operator):

    bl_idname = "scene.callback_change_enum"
    bl_label = "Load new list items"

    def execute(self, context):
        key = random.choice(list(own_name_space.keys()))
        context.scene.menu_tool.menu_key = key
        return {'FINISHED'}


class LayoutDemoPanel(Panel):
    bl_label = "Layout Demo"
    bl_idname = "SCENE_PT_layout"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"

    def draw(self, context):
        layout = self.layout
        col = layout.column()
        col.label(text=" Simple Row:")
        col.prop(context.scene.menu_tool, 'some_selection', text='')
        col.operator('scene.callback_change_enum')


def register():
    bpy.utils.register_module(__name__)
    bpy.types.Scene.menu_tool = PointerProperty(type=CustomMenuProps)


def unregister():
    bpy.utils.unregister_module(__name__)
    del bpy.types.Scene.menu_tool


if __name__ == "__main__":
    register()
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .