Question with EnumProperty

is it possible to change the items in EnumProperty -menu (for example when you click a button or do something else)

i mean the same what context.scene.coll.add() /.clear() does with a prop_search.
i need that for an other menu

you can supply EnumProperty with a function for the items-parameter, it will be called by blender everytime some could have changed and you can return updated entries.

http://www.blender.org/documentation/blender_python_api_2_70_release/bpy.props.html#bpy.props.EnumProperty

i tried it with an update function but that did not work.

how can i do this?? a sample code could help.

Are you trying to change the items with the update= or return a list with the items=?

i want to change the items (for example when i press a button)

Here is an example that dynamically examines the scene for groups and populates an Enum with the groups.


    def list_update(self, context):
        # Update code here.
        pass
    
    def list_populate(self, context):
        result = []
        if context != None:
            result.append(("NONE", "None","none"))
            for grp in bpy.data.groups:
                result.append((grp.name, grp.name, grp.name))
        return result


    group_list = bpy.props.EnumProperty(items=list_populate, update=list_update)

Here is a simpler setup with fixed values returned to the Enum property.


    def list_update(self, context):
        # Update code here.
        pass
    
    def list_populate(self, context):
        result = [('ADD','Add','Adition'),('SUB','Sub','Subtract'),('MULT','Mult','Multiply'),('DIV','Div','Divide')]
        return result


    function_type = bpy.props.EnumProperty(items=list_populate, description="A list of supported math functions.", update=list_update)

i think that this works. but after some change i have a problem.

i use two files: an init.py and another file called object_linker.py

in the second one i have this code now:

def category_choose(self, context):
    print ("test")

sel_category = bpy.props.EnumProperty(update = category_choose, name = "Category", items = test_items,
default = 'all')

and in the init-file i tried to put menu into the panel with col.prop(…)
i tried col.prop(context.scene, ‘object_linker.sel_category’)
and
col.prop(context.scene, ‘sel_category’) as well.

how can i do this right (because no one of theme works for me)

sel_category is not a registered property, you need to register it on an ID type, e.g. bpy.types.Scene

now i have this code in the init.py

bpy.types.Scene.sel_category = bpy.props.EnumProperty(update = object_linker.category_choose, name = "Category", items =         object_linker.test_items,
    default = 'all')

it uses

test_items = [("all","all","all"), ("test_only","test_only","test_only")]

from my other file called object_linker.py
i tried to add something with append


test_items.append(("test_only2","test_only2","test_only2"))

but that does not work. what do i have to do?

As pointed out by Atom, the dynamic items expects a a function not a list.

One work around is to make the list global and return it from the method

in object_linker.py


test_items = [("all", "all", "all")]

def list_populate(self, context):
    global test_items
    return test_items

in another module



bpy.types.Scene.sel_category = bpy.props.EnumProperty(update = object_linker.category_choose, name = "Category", items =         object_linker.list_populate,
    default = 'all')

(or just define the scene enum_prop in the same module)

As well as the enum_prop update construct you are working on, there is the menu operator construct. http://www.blender.org/documentation/blender_python_api_2_70_0/bpy.types.Menu.html?highlight=menu Depends on your needs.

that was it, but you made one mistake.

default = 'all'

gave me an error
the error message was

TypeError: EnumProperty(…): ‘default’ can’t be set when ‘items’ is a function
Exception in module register(): ‘/home/linux/blender-2.69-linux-glibc211-x86_64/2.69/scripts/addons/object_linker/init.py’

i removed this code and it works now.

AFAIC I made two mistakes, one of which I wont be repeating.