3
$\begingroup$

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.

enter image description here

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!

$\endgroup$
5
$\begingroup$

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())

def draw_menu_enum(self, context):
    self.layout.prop_menu_enum(context.object, "enum_prop", "Group")

bpy.types.OBJECT_PT_relations.append(draw_menu_enum)
$\endgroup$
  • $\begingroup$ I'm trying it out right now, but it seems like this is what I'm looking for! Thanks a lot! $\endgroup$ – Cyrus Jan 18 '15 at 4:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.