Hi guys,
I have to do a python script that must open a DICOM series from a folder and load a segmentation in that same scene. It has a part that loads the series and other that loads a segmentation. Both parts work perfectly when used isolated, but when combined it fails sometimes.
I think it is because it tries to load the .nrrd before the DICOM is completely loaded. I’ve been trying to introduce some comprobations to see if it is loaded completely but it didn’t work.
Here is my complete code:
def main():
# ----- Load of DICOM series ----------
loadedNodeIDs = [] # this list will contain the list of all loaded node IDs
if len(sys.argv) > 1:
dicomDataDir = sys.argv[1]
dicomDataDir.encode('unicode_escape')
else:
dicomDataDir = r'C:\Users\Sergio\Desktop\Slicer\OriginalDICOM'
DICOMUtils.importDicom(dicomDataDir)
dicomFiles = slicer.util.getFilesInDirectory(dicomDataDir)
loadablesByPlugin, loadEnabled = DICOMUtils.getLoadablesFromFileLists([dicomFiles])
loadedNodeIDs = DICOMUtils.loadLoadables(loadablesByPlugin)
#loadedNodeIDs.getNodesByClass("vtkMRMLScalarVolumeNode")
while(not slicer.util.getNodesByClass("vtkMRMLScalarVolumeNode")): # Kind of comprobation to see if there is any volume loaded so it doesnt try to load the segmentation so early, I think it crashes if it does so
sys.sleep(2)
continue
# ------ Load of segmentation ----------
seg = slicer.util.loadSegmentation(r'C:\Users\Sergio\Desktop\Slicer\Python\F0QalYvbzsoZhi.nrrd')
seg.GetSegmentation().SetConversionParameter("Smoothing factor", "0.30")
seg.CreateClosedSurfaceRepresentation()
slicer.app.connect("startupCompleted()",main()) ### SEGUNDO PARAMETRO LAS INSTRUCCIONES A REALIZAR, EN ESTE CASO HABRÍA QUE HACER UNA FUNCIÓN
It just crashes, does not show any error message, and it crashes less times when the .nrrd is smaller and more when it is bigger.