Bug in NodePath::getChild()?


I wanted to apply an action to all subnodes of render, so I used this code:

for i in range(render.getNumChildren()):
  child = render.getChild(i)

But it errors out at the render.getChild(i) line. It gives this assertion:

Assertion failed: n >= 0 && n < get_num_children(current_thread) at line 387 of panda/src/pgraph/nodePath.I

Which is incorrect, because getNumChildren returns 5 and range(getNumChildren) returns [0,1,2,3,4], so that perfectly fits the assertion.
I’ve looked at the source of nodePath.I, but I don’t see anything suspicious there… I must be making a very silly mistake but can’t see it. :slight_smile:

PS. Yeah, iterating getChildren().asList() works fine. Just found out about that.

Try replacing the loop by:

while i < render.getNumChildren():
    child = render.getChild(i)
    i = i + 1

I bet you won’t get that error but you may get another from a side effect on your code inside the loop that changes the element count of the render node.