# Combining multiple segments by name (python)

**URL:** <https://discourse.slicer.org/t/combining-multiple-segments-by-name-python/36331>\
**Category:** Development\
**Tags:** segmentation, python\
**Created:** [May 22, 2024, 4:44pm UTC](https://discourse.slicer.org/t/combining-multiple-segments-by-name-python/36331 "2024-05-22T16:44:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rkraaijveld](https://avatars.discourse-cdn.com/v4/letter/r/b9bd4f/32.png) [@rkraaijveld](https://discourse.slicer.org/u/rkraaijveld)\
**Post date:** [May 22, 2024, 4:44pm UTC](https://discourse.slicer.org/t/combining-multiple-segments-by-name-python/36331/1 "2024-05-22T16:44:49Z")

</div>

Hi!

Whenever I have my segmentations for several organs, such as Liver, Lungs, Kidney, I tend to split them into the connected components. I want to make an additional button in the Segment Editor where after splitting them into connected components, that I also am able to combine them.

For example, Lungs and Lungs\_2 will become Lungs, Kidney and Kidney\_2 will become Kidney etc.

This is what I’ve tried so far and it hasnt worked, can someone help out?

Below, organ\_segments is simply:  
{Lungs: [“Lungs”, “Lungs\_2”], Kidney: [“Kidney”, “Kidney\_2”], Liver: [“Liver”]}

```auto
segmentation_node = self.editor.segmentationNode()

 self.editor.setActiveEffectByName("Logical operators")
 effect = self.editor.activeEffect()

 for organ in segmented_organs:
            main_segment = organ
            for segment in organ_segments[organ]:
                if segment != organ:
                    self.editor.setCurrentSegmentID(main_segment)
                    effect.setParameter("Operation", "UNION")
                    effect.setParameter("ModifierSegmentID", segment)
                    effect.self().onApply()
                    
                    print("combined:", segment, organ)
                    segmentation_node.RemoveSegment(segment)

```

---

<div class="post-metadata">

**Author:** ![mikebind](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mikebind](https://discourse.slicer.org/u/mikebind)\
**Post date:** [May 22, 2024, 7:09pm UTC](https://discourse.slicer.org/t/combining-multiple-segments-by-name-python/36331/2 "2024-05-22T19:09:26Z")

</div>

You may be running into the fact that the segmentID is not always the same as the segment name, depending on how it was created. This can be a bit confusing because the segmentID is just another string, and it often does coincide with the segment name. However, I think segmentIDs must always be unique within a segmentation, whereas segment names are allowed to be nonunique. Try modifying your code to explicitly get and use the segment IDs rather than relying on the assumption that the segment ID is the same as the segment name. You can get the segment ID from the name using ` segmentation_node.GetSegmentation().GetSegmentIdBySegmentName(segment_name)`  
For example, you could change the line getting the main segment id to read `main_segment_id = segmentation_node.GetSegmentation().GetSegmentIdBySegmentName(organ)`

---

<div class="post-metadata">

**Author:** ![rkraaijveld](https://avatars.discourse-cdn.com/v4/letter/r/b9bd4f/32.png) [@rkraaijveld](https://discourse.slicer.org/u/rkraaijveld)\
**Post date:** [May 23, 2024, 7:07am UTC](https://discourse.slicer.org/t/combining-multiple-segments-by-name-python/36331/3 "2024-05-23T07:07:28Z")

</div>

Okay, thanks for the tips!

Maybe I’m missing something but when I add in your suggestion I seem to be getting a different error. This is the code:

```auto
for organ in segmented_organs:
            main_segment = segmentation_node.GetSegmentation().GetSegmentIdBySegmentName(organ)
            
            for segment in organ_segments[organ]:
                if segment != organ:
                    segment_id = segmentation_node.GetSegmentation().GetSegmentIdBySegmentName(segment)
                    
                    self.editor.setCurrentSegmentID(main_segment)
                    self.editor.setActiveEffectByName("Logical operators")
                    effect = self.editor.activeEffect()
                    effect.setParameter("ModifierSegmentID", segment_id)
                    effect.setParameter("Operation", "UNION")
                    effect.setParameter("BypassMasking", 1)
                    effect.self().onApply()

```

I still get the error: [Python] Operation UNION requires a selected modifier segment

Do you have any suggestions? Thanks in advance!

---

<div class="post-metadata">

**Author:** ![mikebind](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mikebind](https://discourse.slicer.org/u/mikebind)\
**Post date:** [May 23, 2024, 3:23pm UTC](https://discourse.slicer.org/t/combining-multiple-segments-by-name-python/36331/4 "2024-05-23T15:23:40Z")

</div>

Give this a try:

```auto
for organ in segmented_organs:
            main_segment_id = segmentation_node.GetSegmentation().GetSegmentIdBySegmentName(organ)
            
            for segment in organ_segments[organ]:
                segment_id = segmentation_node.GetSegmentation().GetSegmentIdBySegmentName(segment)
                if segment_id != main_segment_id:
                    self.editor.setCurrentSegmentID(main_segment_id)
                    self.editor.setActiveEffectByName("Logical operators")
                    effect = self.editor.activeEffect()
                    effect.setParameter("ModifierSegmentID", segment_id)
                    effect.setParameter("Operation", "UNION")
                    effect.setParameter("BypassMasking", 1)
                    effect.self().onApply()

```

It just finishes the process of using segment IDs instead of names. Possibly, you were running into the error when the current segment and the modifier segment were the same. If this doesn’t work, then the error is due to something else.

---

<div class="post-metadata">

**Author:** ![rkraaijveld](https://avatars.discourse-cdn.com/v4/letter/r/b9bd4f/32.png) [@rkraaijveld](https://discourse.slicer.org/u/rkraaijveld)\
**Post date:** [May 24, 2024, 7:35am UTC](https://discourse.slicer.org/t/combining-multiple-segments-by-name-python/36331/5 "2024-05-24T07:35:12Z")

</div>

Perfect!! This works, thanks!!

---

<div class="post-metadata">

**Author:** ![Hamburgerfinger](https://avatars.discourse-cdn.com/v4/letter/h/eb9ed0/32.png) [@Hamburgerfinger](https://discourse.slicer.org/u/Hamburgerfinger)\
**Post date:** [July 13, 2025, 1:55am UTC](https://discourse.slicer.org/t/combining-multiple-segments-by-name-python/36331/6 "2025-07-13T01:55:59Z")

</div>

I am using Slicer 5.4.0 and can’t get this to work… I started creating a python function that looks like so:

def combine\_segments(“segmentation\_name”, [“first\_segment”, “second\_segment”, “third\_segment”, …], “combined\_segment\_name”):  
code to combine the segments

Any help appreciated!

---

<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:** [July 28, 2025, 1:47pm UTC](https://discourse.slicer.org/t/combining-multiple-segments-by-name-python/36331/7 "2025-07-28T13:47:42Z")

</div>

I cannot really help with 5.4 specific issues, but if you explain how it does not work then I can probably make suggestions. Are you getting an error message in the console when running the script?
