# Populating an EnumProperty using a Function

I need to populate an EnumProperty with the names of every group in the scene, so that I can include them in a dropdown (prop_menu_enum) in the interface.

How would one accomplish this? I'm creating a tool that has to do with group instances, and ideally speaking, one could choose the group from which they wanted to instance objects using a dropdown menu. As far as I'm aware, (I could very well be wrong!) the only to do so would be to create an EnumProperty using a function to populate it, but I don't know precisely what that function should contain. I tried making a function that returns a list of containing the group's name as the first two elements, and the third being an empty string, but for whatever reason I can't seem to get it to work!

Any help would be greatly appreciated, thanks!

You can use UILayout.prop_search to assign a group to a string.

import bpy

bpy.types.Object.test = bpy.props.StringProperty(name="Group")

def draw(self, context):
self.layout.prop_search(context.object, "test", bpy.data, "groups")

bpy.types.OBJECT_PT_relations.append(draw)


If you use an EnumProperty blender stores the unique id / number, not the string. So you need a separate StringProperty to keep track of the group, in case the user deletes a group and your enumeration of the groups assigns a different index to the selected group.

def get_items():
#closure - keep a reference to the list
items = None
def func(self, context):
items = [(group.name, group.name, "") for group in bpy.data.groups]
return items
return func

bpy.types.Object.enum_prop = bpy.props.EnumProperty(items=get_items())