Not Opening a File - Called by an external python Command line

Operating system: Windows Latest version
Slicer version: 4.8.1
Expected behavior: The Slicer program should start running and open the visual file, however it doesn’t do so.
Actual behavior: The Slicer program does not open the file when it is passed through an external Python Command

I am writing a Python (3.6) program which opens an image file directly into 3D Slicer starting from the filepath of the desired image.

When I run the command with the full file path 3D slicer reacts correctly and opens my file:
The line of code which works is the following

subprocess.Popen(['C:/Program Files/Slicer 4.8.1/Slicer.exe', '--python-code' , 'slicer.util.loadVolume("C:/Users/Documents/Untitled Folder/sampledata3D.tif")'] , stdout = PIPE, stderr= PIPE)

However as soon as I try to pass a variable containing the path of the file, and not the direct path it stops working. This is an example of how the program needs 3D Slicer to open the file:

subprocess.Popen(['C:/Program Files/Slicer 4.8.1/Slicer.exe', '--python-code' , 'slicer.util.loadVolume( " %s " )' % openfile ] , stdout = PIPE, stderr= PIPE)

However it opens 3D Slicer but in this case does not directly open the file. I have tried all the possible combinations but it doesn’t work!

This is another format I attempted which doesn’t work:

subprocess.Popen(['C:/Program Files/Slicer 4.8.1/Slicer.exe', '--python-code' , "slicer.util.loadVolume( self.openfilename )"] , stdout = PIPE, stderr= PIPE)

Can anyone help me with this?
It is essential for my project and if this doesn’t work I will have to look for another viewer to support my software!

Make sure to convert backslashes to forward slashes or add r prefix before the first quotation mark to indicate that it is a raw string. You can check if Slicer receives the correct filename by printing the string to Slicer’s Python console:

print("Trying to open file [%s]" % openfile)

Could you please specify what you mean? I attempted specifying the file path with r however it doesn’t accept it.
I do not get any sort of error. 3D slicer opens but simply doesn’t show the file.
This is what I tried under your advice and it still did not work.

self.openfile = C:/Users/Documents/Untitled Folder/sampledata3D.tif

subprocess.Popen(['C:/Program Files/Slicer 4.8.1/Slicer.exe', '--python-code' , "slicer.util.loadVolume(  r' %s'  )" %self.openfile ] , stdout = PIPE, stderr= PIPE)

I made my python console print what it was passing to your 3D slicer and it was the code you can see below. Which seems correct. The problem I think is that your program doesn’t read variables formatted with % for some reason. I tried the .format() mode for the string and this wasn’t read either.

This is what my python console is passing to 3DSlicer command line now. What is wrong with it and why doesn’t it work.
I have checked that the file exists and can be opened if I manually imput the path in the loadVolume command. However my program needs the path to be stored as a variable:

slicer.util.loadVolume( r' C:/Users/Camilla Tac/Documents/Internships/Untitled Folder/sampledata3D.tif ' )

Furthermore I tried to get Slicer to print your line of code once I had opened it from my python program and got this:

error-3DSlicer

Getting the proper quoting pass-through here was a bit tricky, and I used Process Explorer in order to see what was actually passed to Slicer. Here is the working version I ended up with:

args = [r"C:\Program Files\Slicer 4.9.0-2018-09-10\Slicer.exe", '--python-code', r'slicer.util.loadVolume(r"%s")' % openfile]
subprocess.Popen(args, stdout=PIPE, stderr=PIPE)
2 Likes

Thank you very much! Everything sorted!

I would like to pass a command for 3D Slicer to open the Volume rendering automatically from the program python command line. Could you suggest a way of doing it?
I thought the command loadVolume would directly load and open the volume rendering section of 3D Slicer however it doesn’t do so…

Any suggestions on how to run this through the command line???

See