# How to react to a segmentation node being changed using cut, erase, paint?

**URL:** <https://discourse.slicer.org/t/how-to-react-to-a-segmentation-node-being-changed-using-cut-erase-paint/45201>\
**Category:** Development\
**Tags:** segmentation, python\
**Created:** [November 24, 2025, 4:32am UTC](https://discourse.slicer.org/t/how-to-react-to-a-segmentation-node-being-changed-using-cut-erase-paint/45201 "2025-11-24T04:32:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Victor\_Wayne](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/victor_wayne/32/80778_2.png) [@Victor\_Wayne](https://discourse.slicer.org/u/Victor_Wayne)\
**Post date:** [November 24, 2025, 4:32am UTC](https://discourse.slicer.org/t/how-to-react-to-a-segmentation-node-being-changed-using-cut-erase-paint/45201/1 "2025-11-24T04:32:19Z")

</div>

Hello,

How can I react to a segmentation node being changed using the cut, erase and paint capabilities of the segmentation extension? At first I tried to attach `slicer.vtkMRMLSegmentationNode.SegmentationChangedEvent` this event to the segmentation node and tried to react to it but whenever I changed the segment the control did not go inside my function to react to it. And after that I tried using `vtk.vtkSegmentation.SourceRepresentationModified`, event but this also so did not work. Is the problem is in my code and one of the above mentioned event is the correct one to use? Or is it totally a different event to react to?

Thank you for your help.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [November 24, 2025, 4:58am UTC](https://discourse.slicer.org/t/how-to-react-to-a-segmentation-node-being-changed-using-cut-erase-paint/45201/2 "2025-11-24T04:58:51Z")

</div>

Looks like you need something like:

```auto
segmentationNode.GetSegmentation().AddObserver(slicer.vtkSegmentation.RepresentationModified, print)

```

---

<div class="post-metadata">

**Author:** ![Victor\_Wayne](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/victor_wayne/32/80778_2.png) [@Victor\_Wayne](https://discourse.slicer.org/u/Victor_Wayne)\
**Post date:** [November 24, 2025, 5:02am UTC](https://discourse.slicer.org/t/how-to-react-to-a-segmentation-node-being-changed-using-cut-erase-paint/45201/3 "2025-11-24T05:02:33Z")

</div>

I just tried it. and I also tired `SourceRepresentationModified` too. Both of them did not work.

```python
            # --- Snip ----

            self.targetSegementationNode.GetSegmentation().AddObserver(
                slicer.vtkSegmentation.RepresentationModified,
                self.onSegmentModified
            )
            
            self.segmentBtn.setEnabled(False)
            self.cancelPreviewBtn.setEnabled(False)
            self.previewBtn.setEnabled(False)

            # Disable undo
            disableShortcut("Ctrl+z")
            
        except Exception as e:
            slicer.util.errorDisplay(f"Error - {e}")
            traceback.print_exc()

    def onSegmentModified(self, caller, event):
        """
        Callback for when the segmentation is modified (Paint/Erase/Cut).
        """
        print("Segment content changed (Erase/Paint detected)!")

```

When I used the cut tool it did not print.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [November 24, 2025, 5:09am UTC](https://discourse.slicer.org/t/how-to-react-to-a-segmentation-node-being-changed-using-cut-erase-paint/45201/4 "2025-11-24T05:09:07Z")

</div>

Try something simple and it should work. I just have one segmentation and when I use the code I pasted I get a print when I use the paint effect on the segmentation. Try to debug in the python console before using the approach in more complex code so that you are confident you know a certain call will work.

---

<div class="post-metadata">

**Author:** ![Victor\_Wayne](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/victor_wayne/32/80778_2.png) [@Victor\_Wayne](https://discourse.slicer.org/u/Victor_Wayne)\
**Post date:** [November 24, 2025, 5:55am UTC](https://discourse.slicer.org/t/how-to-react-to-a-segmentation-node-being-changed-using-cut-erase-paint/45201/5 "2025-11-24T05:55:24Z")

</div>

Thanks for the solution. It worked.
