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:

https://www.slicer.org/wiki/Documentation/4.10/Training#Tutorials_for_software_developers

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

https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository

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)
button.show()
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")
    self.formFrame.layout().addWidget(button)
    button.connect("clicked()", slicer.util.openAddModelDialog)
    button.show()

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
transformNode=slicer.util.getNode('LinearTransform_3')
referenceVolumeNode=slicer.util.getNode('MRHead')
slicer.modules.transforms.logic().CreateDisplacementVolumeFromTransform(transformNode, referenceVolumeNode, True)

My actual script looks like:

button1 = qt.QPushButton("Load Segmentation")
self.formFrame1.layout().addWidget(button1)
button1.connect("clicked()", slicer.util.openAddSegmentationDialog)
button1.show()

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:

https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Add_a_texture_mapped_plane_to_the_scene_as_a_model

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: