Is there a way to do conversion between vtkCollection and vtkMRMLNode?

Excuse my silly question if it is silly or it has been addressed somewhere else (please forward a link to me then, Thanks.). Anyway I have been confused a lot by this–>

In the class vtkMRMLScen, the following methods:
GetNodesByClassByName(…, …)
return “vtkCollection”, while the following methods:
GetNthNodeByClass(…, …)
retrun “vtkMRMLNode”.

So would you please get me any explanation ? is there a way to get converted to each other? Thanks a lot in advance.

A vtkCollection is just a list of vtk objects. See:

A vtkMRMLNode is a subclass of vtkObject, and so can be stored in a vtkCollection. You can access the members of a collection through a variety of functions (by index, iterating, etc). Example in C++ of accessing an object at index i as a vtkMRMLNode:

vtkMRMLNode * node = vtkMRMLNode::SafeDownCast(collection->GetItemAsObject(i))

The tests for this class should give a good example of how to iterate through a collection.

1 Like

What would you like to do?

There are helper functions, such as slicer.util getNodesByClass that returns nodes in a Python list instead of a VTK collection.

You can also convert a vtkCollection to a Python list by using list():

allNodesAsPythonList = list(slicer.mrmlScene.GetNodes())

Thanks a lot. That works me out of this puzzle.

Thanks a lot.
I was confused/puzzled by these similar methods/functions which have almost same-type names but return different types. I was always lost while I tried to use one or another such method function.