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)