Automate tasks in Python

Is it actually possible to automate all the tasks that can be done through the GUI in Python? I have spent quite some hours trying to do very simple things but I was unsuccessful.

More specifically, I want to use the module “Model to Label Map”. So far, I could:

import slicer
volume = slicer.util.loadVolume(path1)
model = slicer.util.loadModel(path2)

The next step is to change the settings or parameters of this particular module; then click on “Apply”, and once it’s done, saving the output somewhere. Any idea how I can do these steps?


Yes, everything that you do on the GUI can be done using Python scripting, too.

Instead of “Model to Label Map” we generally recommend to use Segmentations module to do all conversions between segmentations, models, and labelmap volumes. You can import a model into a segmentation (that can be edited using Segment Editor effects and can be exported to labelmap) using a single command, as shown here:

You can find lots of additional examples here:

Hello Andras, thank you very much for your quick answer. Some comments/follow-up questions:

  1. What is exactly a “segmentation node”? Is it a voxelized version of the “model node” (the mesh), because getting this is my ultimate goal.

  2. I don’t find very clear how to use the function to convert a “model node” to a “segmentation node”. It appears to be one line but it requires the “polydata” that it’s more unclear how to generate. In that example, it is iterating over certain pre-defined positions (line 24), creating some “SphereSource” objects and appending them to the initially-empty polydata. How would I find these “pre-defined positions”. I’m not sure what this is, and whether I have do the same in my case. Isn’t it another way to get a “segmentation node”? Is there another way around to use the Model To LabelMap functionality?

  3. I couldn’t find any function to do the “Model To LabelMap” with the parameters/settings that I find in the GUI. More specifically, the GUI shows the settings “label value”.

  4. Related to 3), I wanted to ask about another parameter that does not appear in “settings” in the latest version of Slicer. This parameter is “sampling space”, which appears in version 4.3 and I found it crucial in my data. Without this parameter I get empty volumes whereas with this parameter (and an appropriate tuning) I get favorable results.

Thank you very much again for your answers.

See detailed explanation here:

Have a look at examples in the script repository, for example this:

See the example above how to specify output image geometry (origin, spacing, axis directions). Other settings can be controlled by adjusting conversion parameters, see fore example here:

Hi again Andras, I really appreciate your help.

I’m getting closer to the desired solution. The snippet below seems to load the image and the model converted into a labelmap.

# Load the Nifti file, the image
referenceVolumeNode = slicer.util.loadVolume(niftiImage_path)
# Load the mesh
inputModel = slicer.util.loadModel(mesh_path)

seg = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLSegmentationNode')
slicer.modules.segmentations.logic().ImportModelToSegmentationNode(inputModel, seg)

However, in Slice view, the loaded segmentation and the image are not aligned. This is crucial to later save the segmentation. This surprises me because I would expect that the function SetReferenceImageGeometryParameterFromVolumeNode would put the “seg” in the same orientation and coordinates as the referenceVolumeNode (the image). As you can see below, the bottom-right corner of the image is closer to the top-left corner of the segmentaiton.


Is there a way to align these two automatically? I could see that I can “move” the image by changing the “image origin”, but this is done visually and I can’t just do this. If there is no other way, how can I change this “image origin” from python?


This is probably an RAS/LPS coordinate system mismatch. See more information and options to resolve it here:

That was indeed the problem.

Once again, thank you very much Andras :slightly_smiling_face: