CollectionProperty with unique names

Hi,

how can I ensure that my CollectionProperties uses only unique names. For types like bpy.types.Object or marerials renaming is done automatic (“Cube”, “Cube.001” and so on). If I have a Collection of PropertyGroup, items of can have the same name. It is possible to add a update function to the name property of of the PropertyGroup, which solves the problem in sone cases.

class MyGroup(bpy.types.PropertyGroup):
    def update_name(self, context):
        print("queue update_name - Code to handle double names goes here")

    name = bpy.props.StringProperty(default='name', update=update_name)

If I change the name of the Collection Item on the python console, the update_name function is executed, but if I use the Collection in an UI element like “template_list” and change the name there, the update function is not called, and I still can give the same name to different items.

Pointers are welcome.

Regards offtools.

That is because ‘name’ is reserved for the name of the python collection element (which does not support Blender update events). Try making a string property called ‘entry_name’ or something. Then attach an update to that property. In your draw routine do not display the ‘name’ property. Instead display the ‘entry_name’ property. This way you will get an event and you can handle the name change as you see fit.

Personally I gave up allowing users to rename entries in a list and simply manage the list with automatic naming convention.

Thanks for your reply and the explanation. Pointed me to the right direction, I found another solution by using a custom draw class for the template_list, which calls the update_name callback correct.

class MYGROUP_UL_custom(bpy.types.UIList):
         def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
                 mygroup = data
                 groupitem = item
                 layout.prop(groupitem, 'name', text='', emboss=False)

Changing the name of the element with prop will work and call the update_name callback. Setting Emboss to False make your prop entry look like a label, but is still editable

# UI Code
...
row.template_list("MYGROUP_UL_custom", "mygroup", context.scene, "mygroup", context.scene, "active_group_element", rows=2, maxrows=8)
...

offtools