# Problems with getNode in a for loop

**URL:** <https://discourse.slicer.org/t/problems-with-getnode-in-a-for-loop/33925>\
**Category:** Support\
**Tags:** segmentation, dicom, python\
**Created:** [January 23, 2024, 12:04pm UTC](https://discourse.slicer.org/t/problems-with-getnode-in-a-for-loop/33925 "2024-01-23T12:04:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![paleomariomm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/paleomariomm/32/68475_2.png) [@paleomariomm](https://discourse.slicer.org/u/paleomariomm)\
**Post date:** [January 23, 2024, 12:04pm UTC](https://discourse.slicer.org/t/problems-with-getnode-in-a-for-loop/33925/1 "2024-01-23T12:04:44Z")

</div>

I have a `for` loop that:

- load DICOM files in subfolders
- show volume rendering of each CT
- segment using thresholding
- save in ply format

The way I develop the `for` loop is:

```auto
import os
from DICOMLib import DICOMUtils

yourpath = r"C:/Users/mario.modesto/Desktop/DICOM"

#walk through DICOM directory
for dir in os.scandir(yourpath):
    # Load DICOM files
    dicomDataDir = dir.path # path to input folder with DICOM files
    baboon_skull = dir.name
    loadedNodeIDs = [] # this list will contain the list of all loaded node IDs

    with DICOMUtils.TemporaryDICOMDatabase() as db:
        DICOMUtils.importDicom(dicomDataDir, db)
        patientUIDs = db.patients()
        for patientUID in patientUIDs:
            loadedNodeIDs.extend(DICOMUtils.loadPatientByUID(patientUID))
    # Display volume rendering
    # https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#display-volume-using-volume-rendering
    logic = slicer.modules.volumerendering.logic()
    volumeNode = slicer.mrmlScene.GetNodeByID('vtkMRMLScalarVolumeNode1')
    displayNode = logic.CreateVolumeRenderingDisplayNode()
    displayNode.UnRegister(logic)
    slicer.mrmlScene.AddNode(displayNode)
    volumeNode.AddAndObserveDisplayNodeID(displayNode.GetID())
    logic.UpdateDisplayNodeFromVolumeNode(displayNode, volumeNode)
    
    # find the files NodeID
    volumeNode = getNode('2: Facial Bones 0.75 H70h')

< more code here - not all displayed >

```

As you can see, each CT has a volume called `2: Facial Bones 0.75 H70h`, and can be seen in the last line of previous code.

If I have 3 CTs in the DICOM directory, as in here:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/8/a84a1a00915d430e2b53dea7195a579c30780d7c.png)

When I run the full code, I have three `*.ply` files in that directory with exactly the same size:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/0/40ab98f54cdd5f698bb98532801d901253968eb7.png)

The subject hierarchy is this one:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/2/42d07173a05fbb79ca8a76af2463ba6540e65bf6.png)

As you can see, the volumes of W102 and W103 were renamed with `2: Facial Bones 0.75 H70h_1` and `2: Facial Bones 0.75 H70h_2` and inserted in the first skull [1X3805 (101)].

Obviously, the three `*.ply` files are the same skull (the first one).

I am almost sure that the problem comes from the name of the volume in the last line of the code I copied above:

`volumeNode = getNode('2: Facial Bones 0.75 H70h')`

Then the loop goes to the second it works only with that volume and not with the other two that were renamed.

Any suggestion to solve this?

---

<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:** [January 23, 2024, 11:16pm UTC](https://discourse.slicer.org/t/problems-with-getnode-in-a-for-loop/33925/2 "2024-01-23T23:16:48Z")

</div>

It seems that you have not changed the source volume in the segment editor but you segmented the same image 3 times.

If you are processing independent subjects then it makes sense to clear the scene after each subject.

---

<div class="post-metadata">

**Author:** ![paleomariomm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/paleomariomm/32/68475_2.png) [@paleomariomm](https://discourse.slicer.org/u/paleomariomm)\
**Post date:** [January 24, 2024, 8:10am UTC](https://discourse.slicer.org/t/problems-with-getnode-in-a-for-loop/33925/3 "2024-01-24T08:10:23Z")

</div>

Thanks for the tip, it worked!

I added the next code as the last line:

`slicer.mrmlScene.Clear(0)`
