# Create a mesh in python script

**URL:** <https://discourse.slicer.org/t/create-a-mesh-in-python-script/1543>\
**Category:** Support\
**Tags:** segmentation\
**Created:** [November 28, 2017, 4:31pm UTC](https://discourse.slicer.org/t/create-a-mesh-in-python-script/1543 "2017-11-28T16:31:56Z")\
**Posts on this page:** 19\
**Page:** 2

<div class="post-metadata">

**Author:** ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)\
**Post date:** [December 11, 2017, 10:05am UTC](https://discourse.slicer.org/t/create-a-mesh-in-python-script/1543/21 "2017-12-11T10:05:27Z")

</div>

Thanks Andras, I owe you so much.

---

<div class="post-metadata">

**Author:** ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)\
**Post date:** [December 12, 2017, 5:50pm UTC](https://discourse.slicer.org/t/create-a-mesh-in-python-script/1543/22 "2017-12-12T17:50:19Z")

</div>

Hi,  
Sorry for these 2 new questions:

1. How to save fiducial (F.fcsv) in python?
2. With the wonderful [script](https://gist.github.com/lassoan/1673b25d8e7913cbc245b4f09ed853f9) 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.

 ![problem_slicer](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/b/9b6d4a3f495a4606a2a5ab363a2a3f36b12e5472.jpeg)

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [December 12, 2017, 6:30pm UTC](https://discourse.slicer.org/t/create-a-mesh-in-python-script/1543/23 "2017-12-12T18:30:22Z")

</div>

1. You can use this function from slicer.util [https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/util.py#L423-L439](https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/util.py#L423-L439)
2. There are holes in your segmentation. You’ll need to adjust the threshold parameters (likely reducing MinimumThreshold would help)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 12, 2017, 6:36pm UTC](https://discourse.slicer.org/t/create-a-mesh-in-python-script/1543/24 "2017-12-12T18:36:41Z")

</div>

> [@Frederic](#):
>
> why there is some mistake

What mistake you are referring to?

---

<div class="post-metadata">

**Author:** ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)\
**Post date:** [December 12, 2017, 7:09pm UTC](https://discourse.slicer.org/t/create-a-mesh-in-python-script/1543/25 "2017-12-12T19:09:09Z")

</div>

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).

 ![](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/1X/677f06bdf711468707faf594c72c7993f90d2513.png)

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [December 12, 2017, 7:29pm UTC](https://discourse.slicer.org/t/create-a-mesh-in-python-script/1543/26 "2017-12-12T19:29:43Z")

</div>

@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.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/d/7d53f6a9d5c74243101fb56221e5c2e4f09932a4.jpeg)

---

<div class="post-metadata">

**Author:** ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)\
**Post date:** [December 13, 2017, 11:23am UTC](https://discourse.slicer.org/t/create-a-mesh-in-python-script/1543/27 "2017-12-13T11:23:04Z")

</div>

@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:

> effect.setParameter(“MinimumThreshold”,“100”)  
> effect.setParameter(“MaximumThreshold”,“2000”)

And one of my file was [here (link)](https://wetransfer.com/downloads/4dd4514bf5c74a3696387973e917d70720171213111128/f2ad0df71503377fea4807967eb4b46020171213111128/f8b608).

Could you try with this file please?

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [December 13, 2017, 4:03pm UTC](https://discourse.slicer.org/t/create-a-mesh-in-python-script/1543/28 "2017-12-13T16:03:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)\
**Post date:** [December 13, 2017, 4:36pm UTC](https://discourse.slicer.org/t/create-a-mesh-in-python-script/1543/29 "2017-12-13T16:36:41Z")

</div>

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).

 ![Sans titre](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/7/17cd22c538d4fe7b9c803deca8e21dce8e8fc0be.jpeg)

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [December 13, 2017, 5:07pm UTC](https://discourse.slicer.org/t/create-a-mesh-in-python-script/1543/30 "2017-12-13T17:07:54Z")

</div>

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  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/c/acc77c135a6a6c2179963fb956c48742155162cb.png)

You can also fix the normals themselves in the Surface Toolbox module like this:  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/8/9816bd4e056c72b070f5c93202c22caeede4da83.png)

---

<div class="post-metadata">

**Author:** ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)\
**Post date:** [December 13, 2017, 5:29pm UTC](https://discourse.slicer.org/t/create-a-mesh-in-python-script/1543/31 "2017-12-13T17:29:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 13, 2017, 6:14pm UTC](https://discourse.slicer.org/t/create-a-mesh-in-python-script/1543/32 "2017-12-13T18:14:55Z")

</div>

Surface Toolbox is a Python module, so you can copy-paste the [few lines that updates the normal](https://github.com/Slicer/Slicer/blob/cb844150111d90c277843f46b3ea87f329fa216e/Modules/Scripted/SurfaceToolbox/SurfaceToolbox.py#L429-L436).

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [December 13, 2017, 6:22pm UTC](https://discourse.slicer.org/t/create-a-mesh-in-python-script/1543/33 "2017-12-13T18:22:12Z")

</div>

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()
normals.SetAutoOrientNormals(True)
normals.ConsistencyOn()
normals.SetInputData(surfaceMesh)
normals.Update()
surfaceMesh = normals.GetOutput()

# Write to STL file
writer = vtk.vtkSTLWriter()
writer.SetInputData(surfaceMesh)
writer.SetFileName("d:/something.stl")
writer.Update()
```

---

<div class="post-metadata">

**Author:** ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)\
**Post date:** [December 14, 2017, 11:08am UTC](https://discourse.slicer.org/t/create-a-mesh-in-python-script/1543/34 "2017-12-14T11:08:20Z")

</div>

Thanks to both of you once again. 👏

---

<div class="post-metadata">

**Author:** ![Alexandra](https://avatars.discourse-cdn.com/v4/letter/a/e68b1a/32.png) [@Alexandra](https://discourse.slicer.org/u/Alexandra)\
**Post date:** [April 10, 2018, 10:42am UTC](https://discourse.slicer.org/t/create-a-mesh-in-python-script/1543/35 "2018-04-10T10:42:11Z")

</div>

Hello,  
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!

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [April 10, 2018, 1:47pm UTC](https://discourse.slicer.org/t/create-a-mesh-in-python-script/1543/36 "2018-04-10T13:47:05Z")

</div>

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

> [@Using segmentstatistics module in python script](https://discourse.slicer.org/t/using-segmentstatistics-module-in-python-script/2140):
>
> Dear all, How can I call and use segmentstatistics module in a simple way. I tried this code but I get an error volSeg = slicer.util.getNode('Segmentation') volInput = self.inputSelector.currentNode() segStat = slicer.modules.segmentstatistics.widgetRepresentation().self() segStat.grayscaleNode = volInput segStat.labelNode = volSeg segStat.onApply() segStat.onExportToTable() segTbl = slicer.util.getNode('Segmentation statistics\*') This is the error: S…

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [August 30, 2018, 10:57am UTC](https://discourse.slicer.org/t/create-a-mesh-in-python-script/1543/37 "2018-08-30T10:57:39Z")

</div>

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]]

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 30, 2018, 11:36am UTC](https://discourse.slicer.org/t/create-a-mesh-in-python-script/1543/38 "2018-08-30T11:36:50Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 11, 2019, 1:58pm UTC](https://discourse.slicer.org/t/create-a-mesh-in-python-script/1543/39 "2019-04-11T13:58:17Z")

</div>

A post was split to a new topic: [How to write STL from dicom slices](https://discourse.slicer.org/t/how-to-write-stl-from-dicom-slices/6468)

[Previous page](https://discourse.slicer.org/t/create-a-mesh-in-python-script/1543.md?page=1)
