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?
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
yes, that would work but I just wanted to know if I could avoid doing that and just move one segment.
Yes, you can copy/move segments between segmentations. You can track down how it is implemented in Slicer core and replicate that in your script.
2 Likes
Do you have an example of the script used to do this?
Thanks in advance.
I needed to figure this out myself - for anyone in the future that needs this:
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)