How delete a bpy_prop_collection element

Hi,

In an object, I have added a PropertyGroup with something like this:

mainobject.MainData.add()
mp = mainobject.MainData[0]


mp.maindata_segments.add()

How can I delete an element of maindata_segments by index?

something like

maindata_segments.delete(5)

I have tried several ideas but nothing works, any idea?

Have you tried del maindata_segments[5]?

If that doesn’t work, this particular list property doesn’t support item assignment/deletion. You should look into the related documentation if there are functions to achieve what you want!

The result is:

TypeError: del bpy_prop_collection[key]: not supported

The object you call .add() on has also a .remove() method, which expects the index number of the element to be removed.

You could do the following:

mp = mainobject.MainData.add() # mainobject.MainData[0]
mp.name = "foo"

idx = mainobject.MainData.find("foo")
if idx > -1:
    mainobject.MainData.remove(idx)

But in certain cases, the following would be more reliable:

for i, elem in enumerate(mainobject.MainData):
    if elem == mp:
        mainobject.MainData.remove(i)
    break

Thanks CoDEmanX, the remove() works. I had tested all combinations with delete() and del, but I didn’t think about remove()

Where did you find the documentation for this? I was looking the API docs and I was not able to find it. I think I’m not use to this api docs :slight_smile:

Auto-complete in Blender’s Python console reveals this function, the API docs don’t list the methods of property collections IIRC (not sure why).