Load Dicom Image and a mesh

Hi, how can I create a .py script in order to create a button for loading a .mha, .hdr files and a button for .stl and .vtk files, please?

Start by looking through the programming tutorials:


The script repository also has lots of examples to learn from:


Thank you for your answer. I checked both references but I did not find how can I import an .stl and .vtk files using a button and Python programming language. Could you please send me an exemple of the code?

I don’t think there’s an existing example that does exactly that - but it doesn’t sound like a very difficult task if you learn how to do it. The best is to start with examples that do something similar and then experiment.

Why do you need this program? What do you intend to do with it?

You can load a model from stl or vtk file using slicer.util.loadModel(). You may also use slicer.util.openAddModelDialog() to show a model file selector dialog and load the selected file:

button = qt.QPushButton("Load!")
button.connect("clicked()", slicer.util.openAddModelDialog)
1 Like

Thank you Andras for your help,
After using the code which you suggested :

# button for importing mesh files
    button = qt.QPushButton("Load Mesh")
    button.connect("clicked()", slicer.util.openAddModelDialog)

I have got the mesh in the 3D Widget and out of the original image (dicom). The mesh is not visuliazed in 2D widgets.
Could I have any solution to get the mesh inside of the original image for the 4 slicer widgets?
Thank you in advance.mesh mesh_correct

Yes. Enable slice intersections in the model node’s display node - see SetSliceIntersectionVisibility, SetSliceIntersectionOpacity, SetSliceIntersectionThickness.

Mulitple display modes are available: intersection and various projections - see details here.

Thank you Andras.
For now I have two issues:
1- The mesh needs a linear transformation in order to be in the perfect location (correct and incorrect example is attached).
2- How can I use the slicer fill feature from the Segmentations module as a python code in order to transform the region into a contour?

Thank you in advancecorrect incorrect

1 Like

At a first glance that looks like an RAS/LPS issue - see this for background: https://www.slicer.org/wiki/Coordinate_systems

Adding a transform diag(-1,-1,1,1) would fix that.

Thank you Steve. However, I could not have an access to the node in order to apply the example of the linear transformation below:

# Linear Transformation
slicer.modules.transforms.logic().CreateDisplacementVolumeFromTransform(transformNode, referenceVolumeNode, True)

My actual script looks like:

button1 = qt.QPushButton("Load Segmentation")
button1.connect("clicked()", slicer.util.openAddSegmentationDialog)

Could you please assist me in this matter?
Thank you in advance.

Yes, you’ll need to get the model node, maybe by using a node combo box.

Then you can add a linear transform, like the last block of code in this example:


Except that you need to use the diag(-1,-1, 1,1). Probably you want to do it manually with the GUI and then map that onto the script.

1 Like

Thank you Steve. I solved it finally :slight_smile: