vtk-9.2.5 python wrapper vtkProp3DCollection.GetNextProp3D() return null

Hello everyone, I’m new to the Forum, please treat me well and let me know if I do something wrong.

I’m having issues writing a simple python function to save the centers of the actors in a vtkassembly.

Here is the function:
def get_spheres_centers(assembly):
print(assembly)
collection=vtk.vtkProp3DCollection()
assembly.GetActors(collection)
print(collection)

for i in range(collection.GetNumberOfItems()):
    actor=collection.GetNextProp3D()
    print(actor)
    centers[i-1,:]=actor.GetCenter()
return centers

GetNextProp3D() always returns NULL, i’ve tried using
actor=collection.GetLastProp3D()
and this work but of course always return the last actor of the collection. Any idea why?

I’m so dumb sorry. I just needed to initialize the position inside the collection. I’ll leave here the solution just in case anyone else look for this:

def get_spheres_centers(assembly):
collection=vtk.vtkProp3DCollection()
assembly.GetActors(collection)
collection.InitTraversal()
for i in range(collection.GetNumberOfItems()):
actor=collection.GetNextProp3D()
centers[i-1,:]=actor.GetCenter()
return centers

Even the VTK developers themselves have trouble with the “Collection” classes.

From Python, the easiest way to use them is like this:

 for i in range(collection.GetNumberOfItems()):
    actor = collection.GetItemAsObject(i)

Thank you mate. My next issue is that changing the pose of the vtkassembly using RotateX() ecc does not change the center of each single actor, any idea why?

edit: I just found another post with the same issue with no answer here https://vtk.org/pipermail/vtkusers/2018-March/101327.html

My next issue is that changing the pose of the vtkassembly using RotateX() ecc does not change the center of each single actor

@will.schroeder might know the answer to that one.

Yes, the stateful iteration is annoying (basically, a call to InitTraversal() is required before GetNext* is “useful”). Also note that any collection can contain any object because of the vtkCollection::AddItem(vtkObject*) method.