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
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?
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.