# How to get segmentationNode from segment name

**URL:** <https://discourse.slicer.org/t/how-to-get-segmentationnode-from-segment-name/5765>\
**Category:** Support\
**Created:** [February 13, 2019, 7:44pm UTC](https://discourse.slicer.org/t/how-to-get-segmentationnode-from-segment-name/5765 "2019-02-13T19:44:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![angkr](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@angkr](https://discourse.slicer.org/u/angkr)\
**Post date:** [February 13, 2019, 7:44pm UTC](https://discourse.slicer.org/t/how-to-get-segmentationnode-from-segment-name/5765/1 "2019-02-13T19:44:15Z")

</div>

I was wondering how to get the segmentationNode from segment name.

With version 4.5.0, I was using the subject hierarchy function:  
modelLogic = slicer.vtkSlicerSegmentationsModuleLogic()  
segmentSH = slicer.util.getNode(segmentName)  
segmentationNode=modelLogic.GetSegmentationNodeForSegmentSubjectHierarchyNode(segmentSH)

But with version 4.10.1, subject hierarchy doesn’t seem to exist anymore?

Thanks!

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [February 13, 2019, 8:28pm UTC](https://discourse.slicer.org/t/how-to-get-segmentationnode-from-segment-name/5765/2 "2019-02-13T20:28:04Z")

</div>

Subject hierarchy has been completely reimplemented since that version. This specific operation is not obvious in the new system, but here is how you can do it:

```
itemName = 'What you are looking for'
shn = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(slicer.mrmlScene)
sc = shn.GetSceneItemID()
items = vtk.vtkIdList()
shn.GetItemChildren(sc, items, True)
for i in xrange(items.GetNumberOfIds()):
  name = shn.GetItemName(items.GetId(i))
  if name == itemName:
    foundItem = items.GetId(i)
    break
parentItem = shn.GetItemParent(foundItem)
parentNode = shn.GetItemDataNode(parentItem)
```

---

<div class="post-metadata">

**Author:** ![angkr](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@angkr](https://discourse.slicer.org/u/angkr)\
**Post date:** [February 13, 2019, 10:10pm UTC](https://discourse.slicer.org/t/how-to-get-segmentationnode-from-segment-name/5765/3 "2019-02-13T22:10:17Z")

</div>

Thanks!

But it doesn’t seem to work for if there is more than one segmentation.

What I want to do is : from segments (DICOM RTstruct), with the slicerRT extension, I do bool. operation -intersection/expansion - of segments in new segmentation and I want to get the segmentation node for all the segments (original and newly created in new segmentation) based on their segment name.

For example:  
‘structure1’ -\> vtkMRMLSegmentationNode1  
‘Expanded\_5\_5\_5\_structure1’ -\> vtkMRMLSegmentationNode2

Hope it’s clear & possible !

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [February 13, 2019, 10:24pm UTC](https://discourse.slicer.org/t/how-to-get-segmentationnode-from-segment-name/5765/4 "2019-02-13T22:24:45Z")

</div>

The script works for any scene content as long as `itemName` is unique, or the first result is a segment.
