# Python conversion between model and segment

**URL:** <https://discourse.slicer.org/t/python-conversion-between-model-and-segment/10765>\
**Category:** Support\
**Created:** [March 20, 2020, 3:52pm UTC](https://discourse.slicer.org/t/python-conversion-between-model-and-segment/10765 "2020-03-20T15:52:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Franz](https://avatars.discourse-cdn.com/v4/letter/f/c37758/32.png) [@Franz](https://discourse.slicer.org/u/Franz)\
**Post date:** [March 20, 2020, 3:52pm UTC](https://discourse.slicer.org/t/python-conversion-between-model-and-segment/10765/1 "2020-03-20T15:52:49Z")

</div>

Hi,  
thanks for providing this great Software.

I am trying to change Models to Segments and Segments to Models using Python. This can easily be done using the GUI.  
Model to Segmentation

 ![Model_to_segment](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/4/549f3f34019e5dbd3e46041b8648cbbf66a3e944.png)

Segmentation to Model

 ![Segments_to_models](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/e/ce7f3407bcb9410ae8f21f79208a44ea46a83dca.png)

This is what I have found so far.  
Converting Models to Segments

# Create segmentation

```
segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
segmentationNode.CreateDefaultDisplayNodes() 
addedSegmentID = segmentationNode.GetSegmentation().AddEmptySegment("Segment")

modelNode = slicer.util.getNode('Model')
segmentationNode = slicer.util.getNode('Segmentation')

slicer.modules.segmentations.logic().CreateSegmentFromModelNode(modelNode, segmentationNode)

```

The segmentation is then empty somehow.

Thanks in advance

---

<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:** [March 20, 2020, 3:56pm UTC](https://discourse.slicer.org/t/python-conversion-between-model-and-segment/10765/2 "2020-03-20T15:56:24Z")

</div>

I would recommend to use higher-level (import/export) methods of [segmentations logic](http://apidocs.slicer.org/master/classvtkSlicerSegmentationsModuleLogic.html). See examples in the [script repository](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Segmentations).
