Problems with getNode in a for loop

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:

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  =
    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:
    # Display volume rendering
    logic = slicer.modules.volumerendering.logic()
    volumeNode = slicer.mrmlScene.GetNodeByID('vtkMRMLScalarVolumeNode1')
    displayNode = logic.CreateVolumeRenderingDisplayNode()
    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:

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

The subject hierarchy is this one:

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?

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.

Thanks for the tip, it worked!

I added the next code as the last line:


1 Like