Create a mesh in python script


Thanks Andras, I owe you so much.

1 Like

Sorry for these 2 new questions:

  1. How to save fiducial (F.fcsv) in python?
  2. With the wonderful script of @lassoan, I obtain great segmentation of my data (cf. my picture, left part), but I don’t know why there is some mistake during the saving of the stl (cf. my picture, rigth part). This mistake just occur with my data. Does someone have any idea what may cause this?

Thanks in advance.

(Csaba Pinter) #23
  1. You can use this function from slicer.util
  2. There are holes in your segmentation. You’ll need to adjust the threshold parameters (likely reducing MinimumThreshold would help)
(Andras Lasso) #24

What mistake you are referring to?


Thanks @cpinter.
My data.stl was not like the segmentation mesh, they are all deformated (On my figure, the mesh and the data.stl are in the same point of view).

(Csaba Pinter) #26

@lassoan I think he’s referring to the artifacts in the grey model that are caused by the holes.

@Frederic In your screenshot it can be seen that the segmentation is not solid (see the holes I was talking about), and those show up inside the head, looking quite strange. Once you fill the holes by adjusting the threshold parameters (or just filling them manually first with a brush to test my theory), the mesh on the right should look fine.


@cpinter I dont think that the cause is the holes, the segmentation of Andras script’s have holes too and the data.stl was fine.
My script is the same that Andras, I have just change:


And one of my file was here (link).

Could you try with this file please?

(Csaba Pinter) #28

The difference between the green and gray is that the green has full opacity, and the gray is semi-transparent, and full opacity hides the holes, semi-transparency lets them show. If you change the segmentation’s opacity to 50% or so, or change the model’s opacity to 100%, then they will look the same.


Thanks @cpinter , but It’s not that. Below, on the left my data (opacity: 100%, the holes are always here), on the right, the data of the script of Andras (opacity: 70%, with holes too).

(Csaba Pinter) #30

I see. The problem is the surface normals. If you change Visible Slides to “Back-facing” in the Models module, then it will show up the same way

You can also fix the normals themselves in the Surface Toolbox module like this:

1 Like

Great, you are right @cpinter ! really thanks!
And how to access to is module in python (to perform that you say following the script of Andras) please?

(Andras Lasso) #32

Surface Toolbox is a Python module, so you can copy-paste the few lines that updates the normal.

(Csaba Pinter) #33

Yes. Unfortunately the module is poorly designed to be used more easily form the outside (for reference the “state” class is not exposed, and the UI widgets are not members).

Replace the last section of Andras’ code with this:

# Fix normals
surfaceMesh = segmentationNode.GetClosedSurfaceRepresentation(addedSegmentID)
normals = vtk.vtkPolyDataNormals()
surfaceMesh = normals.GetOutput()

# Write to STL file
writer = vtk.vtkSTLWriter()
1 Like

Thanks to both of you once again. :clap:

1 Like

Thanks for thoses informations. Do you know how to get the model properties like volume, surface, etc. With a python script ?
Is there a documentation for the python scripts?
Thank you very much!

(Csaba Pinter) #36

You can use the Segment Statistics module to do that. See for example

(saima safdar) #37

Hi Andras,
I gone through the example segmention. I dont understand how to select the seed values? is it random selection? can it be automated as well?

tumorSeed.SetCenter(-6, 30, 28)
backgroundSeedPositions = [[0,65,32], [1, -14, 30], [0, 28, -7], [0,30,64], [31, 33, 27], [-42, 30, 27]]

(Andras Lasso) #38

You usually define seeds manually, using paint or draw effect. For fully automatic registration, you can define seeds on an atlas image; then you can transfer these seeds segments to a new image using registration (e. g., General registration (BRAINS) or General registration (Elastix) modules).

(Andras Lasso) split this topic #39

A post was split to a new topic: How to write STL from dicom slices