saveNode not working properly

Hi. I am trying to use the python interactor tool to make some resampling automatic using the module brainresample.
I have a code that loads the volumes, loads a transform and then gives parameters and calls the module. I am pretty sure this works all fine.

Before setting up the parameters, I create a new node using “AddNewNodeByClass”, and then I want to save the new node as nifti.

resampledNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
parameters = {'inputVolume': itemCT, 'referenceVolume': itemMR, 'outputVolume': resampledNode, 'warpTransform': itemTransform, 'interpolationMode': 'Linear'}

slicer.cli.run(slicer.modules.brainsresample, None, parameters)    
slicer.util.saveNode(resampledNode, output_filename)

I try to run this piece of code (with the previous part defining the volumes and all) as an external file using: ./Slicer --no-main-window --python-script /home/user/Desktop/test_3D_slicer.py. It seems to run the module ok (Resample Image (BRAINS) completed without errors) but then does not save the new image.
What’s strange is that if I input my code line by line in the python interactor (CTRL+3), it works and saves the new image. I wouldn’t want to do that though since the python file contains a for loop and it would just be easier if I could manage it through the external .py file.

Does anybody know why this happens?

You need to use runSync, otherwise you save the output node before CLI execution is completed.

1 Like

@lassoan Thanks! That fixed it.

1 Like