Working directory of Slicer

Hi, I always have the problem with the working directory of Slicer.
when I use

nrrd.write(filename,data)

I have no idea where my filename.nrrd is stored.
And everytime I try to load a model or file to Slicer, the relative path(./directory) won’t work. So I have to use the full path. Is there a solution to this problem?

Non-console applications often do not have the notion of a “working directory”. You need to use absolute paths.

You can construct absolute paths from various directories, for example:

  • slicer.mrmlScene.GetRootDirectory(): all nodes are saved relative to this path
  • slicer.app.temporaryPath: write-able folder, you can use this to store any temporary data
  • slicer.app.slicerHome: Slicer core binary folder
  • slicer.app.extensionsInstallPath: Slicer extensions folder
  • slicer.modules.sampledata.path: path of a scripted module (in this example: Sample Data module)

You can also pass any folder name to Slicer via the command-line. For example:

"c:\Program Files\Slicer 4.10.1\Slicer.exe" --python-code "myDataDir='%cd%';loadVolume(myDataDir+'vol.nrd'); (do some processing here...); saveNode(someNode,myDataDir+'processed.nrrd')"

1 Like

Hi, thanks for the answer. This is a very clear explanation. But I still have some problem here.
In my scripted module, I got an array and I want to save it as a .nrrd file. I used nrrd.write but I still can’t find my file. I tried to look it up in slicer.modules.sampledata.path, but it’s not there. Where is it or how can I load this file to Slicer?

Thanks for your time.

If you have a numpy array and you want to show it as an image in Slicer then you don’t need to write it to file. You can directly write it to a volume node using slicer.util.updateVolumeFromArray.

if you want to write a temporary file, you can use slicer.app.temporaryPath, for example:

slicer.util.saveNode(someVolumeNode, slicer.app.temporaryPath+'/tempABC.nrrd')
1 Like

Hi, Thanks for your help. This is an awesome solution.

1 Like