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

**URL:** https://discourse.slicer.org/t/is-there-a-way-to-do-conversion-between-vtkcollection-and-vtkmrmlnode/8243
**Category:** Support
**Created:** [August 30, 2019, 10:03pm UTC](https://discourse.slicer.org/t/is-there-a-way-to-do-conversion-between-vtkcollection-and-vtkmrmlnode/8243 "2019-08-30T22:03:34Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![aiden.zhu](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/aiden.zhu/32/67145_2.png) [@aiden.zhu](https://discourse.slicer.org/u/aiden.zhu)
#### Post date: [August 30, 2019, 10:03pm UTC](https://discourse.slicer.org/t/is-there-a-way-to-do-conversion-between-vtkcollection-and-vtkmrmlnode/8243/1 "2019-08-30T22:03:34Z")

</div>

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:  
GetNodes()  
GetNodesByClass(…)  
GetNodesByClassByName(…, …)  
GetNodesByName(…)  
return “ **vtkCollection** ”, while the following methods:  
GetNodeByID(…)  
GetNthNode(…)  
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.  
Best,  
Aiden

---

<div class="post-metadata">

### Author: ![Sam\_Horvath](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sam_horvath/32/3092_2.png) [@Sam\_Horvath](https://discourse.slicer.org/u/Sam_Horvath)
#### Post date: [August 30, 2019, 10:15pm UTC](https://discourse.slicer.org/t/is-there-a-way-to-do-conversion-between-vtkcollection-and-vtkmrmlnode/8243/2 "2019-08-30T22:15:30Z")

</div>

A vtkCollection is just a list of vtk objects. See: [https://vtk.org/doc/nightly/html/classvtkCollection.html](https://vtk.org/doc/nightly/html/classvtkCollection.html)

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.  
C++: [https://vtk.org/gitweb?p=VTK.git;a=blob;f=Common/Core/Testing/Cxx/TestCollection.cxx](https://vtk.org/gitweb?p=VTK.git;a=blob;f=Common/Core/Testing/Cxx/TestCollection.cxx)  
Python: [https://vtk.org/gitweb?p=VTK.git;a=blob;f=Common/Core/Testing/Python/TestIterateCollection.py](https://vtk.org/gitweb?p=VTK.git;a=blob;f=Common/Core/Testing/Python/TestIterateCollection.py)

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [August 30, 2019, 10:53pm UTC](https://discourse.slicer.org/t/is-there-a-way-to-do-conversion-between-vtkcollection-and-vtkmrmlnode/8243/3 "2019-08-30T22:53:50Z")

</div>

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())
```

---

<div class="post-metadata">

### Author: ![aiden.zhu](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/aiden.zhu/32/67145_2.png) [@aiden.zhu](https://discourse.slicer.org/u/aiden.zhu)
#### Post date: [September 2, 2019, 3:24pm UTC](https://discourse.slicer.org/t/is-there-a-way-to-do-conversion-between-vtkcollection-and-vtkmrmlnode/8243/4 "2019-09-02T15:24:12Z")

</div>

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

---

<div class="post-metadata">

### Author: ![aiden.zhu](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/aiden.zhu/32/67145_2.png) [@aiden.zhu](https://discourse.slicer.org/u/aiden.zhu)
#### Post date: [September 2, 2019, 3:26pm UTC](https://discourse.slicer.org/t/is-there-a-way-to-do-conversion-between-vtkcollection-and-vtkmrmlnode/8243/5 "2019-09-02T15:26:32Z")

</div>

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.
