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?
Bayesian
(Bayesian)
May 17, 2015, 12:02pm
2
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!
Antonioya
(Antonioya)
May 18, 2015, 12:05am
3
Bayesian:
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
CoDEmanX
(CoDEmanX)
May 18, 2015, 12:44pm
4
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
Antonioya
(Antonioya)
May 18, 2015, 11:01pm
5
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
CoDEmanX
(CoDEmanX)
May 19, 2015, 7:40am
6
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).