# Copy Segment from Segmentation Failing

**URL:** <https://discourse.slicer.org/t/copy-segment-from-segmentation-failing/15912>\
**Category:** Support\
**Tags:** python\
**Created:** [February 9, 2021, 9:05am UTC](https://discourse.slicer.org/t/copy-segment-from-segmentation-failing/15912 "2021-02-09T09:05:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pete](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@Pete](https://discourse.slicer.org/u/Pete)\
**Post date:** [February 9, 2021, 9:05am UTC](https://discourse.slicer.org/t/copy-segment-from-segmentation-failing/15912/1 "2021-02-09T09:05:55Z")

</div>

Hi,

Any help greatly appreciated. I am trying to copy a segment from one segmentation to another (Copy segment “Single” from segmentation “Segmentation” to new segmentation “Margin Expansion” to new segment “Margin + Expansion”). The below code seems to work perfectly until the final section and I get error “CopySegmentFromSegmentation: Failed to get segment!”

```
masterVolumeNode = slicer.mrmlScene.GetFirstNodeByClass('vtkMRMLScalarVolumeNode')

# Create segmentation
## Can adjust segmentation name in .AddNewNodeByClass 2nd parameter String
segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode", "Margin Expansion")
segmentationNode.CreateDefaultDisplayNodes() # only needed for display
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)

# Create temporary segment editor to get access to effects
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentEditorNode")
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
segmentEditorWidget.setSegmentationNode(segmentationNode)
segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)

## Create new segment with specified name
addedSegmentID = segmentationNode.GetSegmentation().AddEmptySegment("Single + Margin")
segmentEditorNode.SetSelectedSegmentID(addedSegmentID)

## Copy "Single" segment "Segmentation" to new "Margin Expansion" segmentation

sourceSegmentationNode = getNode('Segmentation')
sourceSegmentation = getNode('Segmentation').GetSegmentation()
sourceSegmentId = "Single"

sourceSegmentationNode.GetSegmentation().CopySegmentFromSegmentation(sourceSegmentation, sourceSegmentId)
```

---

<div class="post-metadata">

**Author:** ![Pete](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@Pete](https://discourse.slicer.org/u/Pete)\
**Post date:** [February 9, 2021, 9:24am UTC](https://discourse.slicer.org/t/copy-segment-from-segmentation-failing/15912/2 "2021-02-09T09:24:31Z")

</div>

I just realised what my error was. I was specifying the name of the segment when it required the ID. So I have adjusted to the following:

```
sourceSegmentationNode = getNode('Segmentation')
sourceSegmentation = getNode('Segmentation').GetSegmentation()
sourceSegmentId = sourceSegmentation.GetSegmentIdBySegmentName("Single")

sourceSegmentationNode.GetSegmentation().CopySegmentFromSegmentation(sourceSegmentation, sourceSegmentId)

```

However, the new segment is created in the original Segmentation, not the new one I just created (“Margin Expansion”). Is there a way to fix that?

Alternatively, I can keep it all in the same segmentation but need the ability to name the copied segment something original.

Many thanks in advance,

Pete

---

<div class="post-metadata">

**Author:** ![Pete](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@Pete](https://discourse.slicer.org/u/Pete)\
**Post date:** [February 9, 2021, 10:24am UTC](https://discourse.slicer.org/t/copy-segment-from-segmentation-failing/15912/3 "2021-02-09T10:24:31Z")

</div>

I seem to be able find the answers to all my own questions today with a lot of trial and error. In case anyone needs the solutions in the future:

```
sourceSegmentationNode = getNode('Segmentation')
sourceSegmentation = getNode('Segmentation').GetSegmentation()
sourceSegmentId = sourceSegmentation.GetSegmentIdBySegmentName("Single")

segmentationNode.GetSegmentation().CopySegmentFromSegmentation(sourceSegmentation, sourceSegmentId)

```

To rename a segment:

```
renamedSegmentId = getNode('Margin Expansion').GetSegmentation().GetSegmentIdBySegmentName("Single")
renamedSegment = getNode('Margin Expansion').GetSegmentation().GetSegment(renamedSegmentId)
renamedSegment.GetName()
renamedSegment.SetName('Test')
```
