# How to copy one segment from a segmentation node to a different segmentation with code?

**URL:** <https://discourse.slicer.org/t/how-to-copy-one-segment-from-a-segmentation-node-to-a-different-segmentation-with-code/19286>\
**Category:** Support\
**Tags:** python\
**Created:** [August 20, 2021, 3:43pm UTC](https://discourse.slicer.org/t/how-to-copy-one-segment-from-a-segmentation-node-to-a-different-segmentation-with-code/19286 "2021-08-20T15:43:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![akshay\_pillai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/akshay_pillai/32/8301_2.png) [@akshay\_pillai](https://discourse.slicer.org/u/akshay_pillai)\
**Post date:** [August 20, 2021, 3:43pm UTC](https://discourse.slicer.org/t/how-to-copy-one-segment-from-a-segmentation-node-to-a-different-segmentation-with-code/19286/1 "2021-08-20T15:43:17Z")

</div>

I’m trying to copy a segment from one segmentation to another, but I don’t know how to do it automatically in my code. I know that we can drag and drop in the data module, but is there a way to do this with code?

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [August 20, 2021, 4:24pm UTC](https://discourse.slicer.org/t/how-to-copy-one-segment-from-a-segmentation-node-to-a-different-segmentation-with-code/19286/2 "2021-08-20T16:24:33Z")

</div>

A workaround could be to export all the segments of Segmentation1 as LabelMap and import it to Segmentation2, then delete all the undesired segments from Segmentation2

---

<div class="post-metadata">

**Author:** ![akshay\_pillai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/akshay_pillai/32/8301_2.png) [@akshay\_pillai](https://discourse.slicer.org/u/akshay_pillai)\
**Post date:** [August 20, 2021, 4:27pm UTC](https://discourse.slicer.org/t/how-to-copy-one-segment-from-a-segmentation-node-to-a-different-segmentation-with-code/19286/3 "2021-08-20T16:27:05Z")

</div>

yes, that would work but I just wanted to know if I could avoid doing that and just move one segment.

---

<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 20, 2021, 6:51pm UTC](https://discourse.slicer.org/t/how-to-copy-one-segment-from-a-segmentation-node-to-a-different-segmentation-with-code/19286/4 "2021-08-20T18:51:13Z")

</div>

Yes, you can copy/move segments between segmentations. You can [track down how it is implemented in Slicer core](https://slicer.readthedocs.io/en/latest/developer_guide/python_faq.html#how-to-find-a-python-function-for-any-slicer-features) and replicate that in your script.

---

<div class="post-metadata">

**Author:** ![Sergio](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sergio](https://discourse.slicer.org/u/Sergio)\
**Post date:** [August 4, 2023, 6:25pm UTC](https://discourse.slicer.org/t/how-to-copy-one-segment-from-a-segmentation-node-to-a-different-segmentation-with-code/19286/5 "2023-08-04T18:25:29Z")

</div>

Do you have an example of the script used to do this?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![connorh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/connorh/32/79802_2.png) [@connorh](https://discourse.slicer.org/u/connorh)\
**Post date:** [September 17, 2024, 4:50am UTC](https://discourse.slicer.org/t/how-to-copy-one-segment-from-a-segmentation-node-to-a-different-segmentation-with-code/19286/6 "2024-09-17T04:50:47Z")

</div>

I needed to figure this out myself - for anyone in the future that needs this:

```auto
destSeg = getNode("segmentDestination") #the vtkMRMLSegmentationNode you want the target in
sourceSeg = getNode("segmentSource") # the vtkMRMLSegmentationNode that it's coming from
segmentID = "yourDesiredSegID" #the ID of your segment
removeFromSource = False #whether you want to delete the segment from the source
destSeg.GetSegmentation().CopySegmentFromSegmentation(sourceSeg.GetSegmentation(),segmentID,removeFromSource) 

```
