Children Control

Before you tell me I’m on the wrong forum… hahaha (see title for context)
I have an object which is getting being set as a parent on multiple occasions by about 67 different objects… very messy. However only 2 or 3 are ever parented at a time. And these need to be cleared for the next 2 or 3 children to come in.

So to save myself a tedious amount of work is there just a way to remove all children? Through API searches I could only ever find remove/set parents.

I’m positive there is someone enlightened to this issue.

for child in obj.children[:]:

(Not sure if the [:] to create a copy of the list is necessary, I suspect it is)

Yea, I actually wasn’t sure if it was necessary for a moment either. It turns out it actually isn’t in this case since obj.children is a CListValue and not a regular Python list.

Just to explain for everyone: using the brackets with a colon between them after a list tells Python to take a slice of the entire list, thus creating a copy. If we were dealing with a normal list here, this would actually be necessary since the list is being changed within the for loop. However, since Python iterates through a CListValue differently, it’s ok to not copy the list here.

You can see what I’m talking about by running this simple code

myList = [1, 2, 3, 4]
for i in myList:

The output will be:

Instead of:

This is because after iterating over 2 elements in the list, there are only 2 elements left, so Python thinks it’s iterated through the entire list and stops. When iterating through a CListValue however, I think that python looks at the memory addresses of each element, which remain unchanged when unparenting objects. So it will still iterate through the entire list as it was when the for statement was reached.

Note that for some other structures like Dictionaries and Sets, Python will actually raise a runtime error if you try to modify them during iteration.

Sorry for getting a bit of topic OP, but I couldn’t resist the chance to explain some code :stuck_out_tongue:

Thank you very much for the replies. I have it working all handy dandy.