# Create new segment in Python script

**URL:** <https://discourse.slicer.org/t/create-new-segment-in-python-script/1082>\
**Category:** Development\
**Created:** [September 18, 2017, 5:47pm UTC](https://discourse.slicer.org/t/create-new-segment-in-python-script/1082 "2017-09-18T17:47:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mschumaker](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mschumaker/32/1395_2.png) [@mschumaker](https://discourse.slicer.org/u/mschumaker)\
**Post date:** [September 18, 2017, 5:47pm UTC](https://discourse.slicer.org/t/create-new-segment-in-python-script/1082/1 "2017-09-18T17:47:27Z")

</div>

I’m attempting to create and configure a segment within a new segmentation node via Python script. Here is what I’m attempting:

```
    segNode = slicer.vtkMRMLSegmentationNode()
    slicer.mrmlScene.AddNode(segNode)
    segNode.CreateDefaultDisplayNodes()
    segNode.SetReferenceImageGeometryParameterFromVolumeNode(self.SSFPVolumeNode)
    theSegmentation = segNode.GetSegmentation()
    theSegmentation.AddEmptySegment("test")

```

The result is an error that theSegmentation does not have a method AddEmptySegment. When I check the class type, theSegmentation is a vtkObject, rather than a vtkSegmentation. Is there a reason for this? Do I need to downcast to vtkSegmentation, or is there something I’m doing incorrectly?  
Thanks 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:** [September 18, 2017, 6:15pm UTC](https://discourse.slicer.org/t/create-new-segment-in-python-script/1082/2 "2017-09-18T18:15:21Z")

</div>

You need to import vtkSegmentationCorePython, we usually do it like this:  
import vtkSegmentationCorePython as vtkSegmentationCore

It would be great if it was imported by default, similarly to all the other libraries, like vtkAddon etc.  
@jcfr @pieper I have tried to make that happen, but it turned out to be more complicated than I thought. I’ll submit a PR shortly, I hope you can tell me what’s missing. Thanks!

---

<div class="post-metadata">

**Author:** ![mschumaker](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mschumaker/32/1395_2.png) [@mschumaker](https://discourse.slicer.org/u/mschumaker)\
**Post date:** [September 18, 2017, 6:55pm UTC](https://discourse.slicer.org/t/create-new-segment-in-python-script/1082/3 "2017-09-18T18:55:00Z")

</div>

Great, thanks! That seems to work well so far.

---

<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:** [September 18, 2017, 6:57pm UTC](https://discourse.slicer.org/t/create-new-segment-in-python-script/1082/4 "2017-09-18T18:57:02Z")

</div>

This is the PR I made to make it easier for everybody to use segmentations from python

> <https://github.com/Slicer/Slicer/issues/792>
>
> This issue was created automatically from an original Mantis Issue. Further discussion may take place here.

---

<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:** [September 18, 2017, 7:23pm UTC](https://discourse.slicer.org/t/create-new-segment-in-python-script/1082/5 "2017-09-18T19:23:40Z")

</div>

I’ve tried to implement this before, similarly to Csaba, and failed, too.

@jcfr, I think you implemented this for other libraries - what’s the trick that we missed?

---

<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 31, 2021, 1:36pm UTC](https://discourse.slicer.org/t/create-new-segment-in-python-script/1082/6 "2021-08-31T13:36:33Z")

</div>

A post was split to a new topic: [Prevent a segment from changing when editing other segments](https://discourse.slicer.org/t/prevent-a-segment-from-changing-when-editing-other-segments/19433)
