# How do you call a function when a segmentation is modified?

**URL:** <https://discourse.slicer.org/t/how-do-you-call-a-function-when-a-segmentation-is-modified/29774>\
**Category:** Development\
**Tags:** segmentation, python\
**Created:** [June 1, 2023, 2:51pm UTC](https://discourse.slicer.org/t/how-do-you-call-a-function-when-a-segmentation-is-modified/29774 "2023-06-01T14:51:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guillaumeee](https://avatars.discourse-cdn.com/v4/letter/g/6de8d8/32.png) [@Guillaumeee](https://discourse.slicer.org/u/Guillaumeee)\
**Post date:** [June 1, 2023, 2:51pm UTC](https://discourse.slicer.org/t/how-do-you-call-a-function-when-a-segmentation-is-modified/29774/1 "2023-06-01T14:51:18Z")

</div>

Hello,

I’m looking to create an observer that will allow me to call a method when the segmentation is modified via the segment editor.

Here’s a class I’ve created:

```python
import slicer
from vtk import vtkCommand

class SegmentationWatcher:
    SAVE_FOLDER = 'tmp'
    def __init__ (self, extension_path: str, segmentationNode):
        self.__extension_path = extension_path
        self.segmentationNode = segmentationNode
        self.callbackObject = None

    def segmentationModifiedCallback(self, caller, event):
        print("Modified")

    def startWatching(self):
        if self.callbackObject is None:
            self.callbackObject = self.segmentationNode.AddObserver(vtkCommand.ModifiedEvent, self.segmentationModifiedCallback)

    def stopWatching(self):
        if self.callbackObject:
            self.segmentationNode.RemoveObserver(self.callbackObject)
            self.callbackObject = None

```

However, the function is never called. I deduced that the event was not good.  
I saw that the SegmentationModifiedCallbackCommand attribute existed.

So I did this:

```python
import slicer

class SegmentationWatcher:
    SAVE_FOLDER = 'tmp'
    def __init__ (self, extension_path: str, segmentationNode):
        self.__extension_path = extension_path
        self.segmentationNode = segmentationNode
        self.callbackObject = None

    def segmentationModifiedCallback(self, caller, event):
        print("Modified")

    def startWatching(self):
        if self.callbackObject is None:
            self.callbackObject = self.segmentationNode.AddObserver(slicer.vtkMRMLSegmentationNode.SegmentationModifiedCallbackCommand,
                                                                    self.segmentationModifiedCallback)

    def stopWatching(self):
        if self.callbackObject:
            self.segmentationNode.RemoveObserver(self.callbackObject)
            self.callbackObject = None

```

But I have this error :

```auto
AttributeError: 'MRMLCore.vtkMRMLSegmentationNode' object has no attribute 'SegmentationModifiedCallbackCommand'

```

This is normal because the attribute is protected. I’ve tried to make the class an inheritance of slicer.vtkMRMLSegmentationNode but I still have error.

I’d like to know if it’s possible to access it or if another method is more interesting?

Thank you very much in advance for your help and have a nice day!

---

<div class="post-metadata">

**Author:** ![Sunderlandkyl](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sunderlandkyl/32/79987_2.png) [@Sunderlandkyl](https://discourse.slicer.org/u/Sunderlandkyl)\
**Post date:** [June 1, 2023, 3:57pm UTC](https://discourse.slicer.org/t/how-do-you-call-a-function-when-a-segmentation-is-modified/29774/2 "2023-06-01T15:57:28Z")

</div>

I think that you are looking for [vtkMRMLSegmentationNode::SegmentationChangedEvent](https://apidocs.slicer.org/main/classvtkMRMLSegmentationNode.html#af9c38e41e18005c0448a257f5dfb85c4add8f2a12345ec147a14cb87758130e8b).

---

<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:** [June 2, 2023, 8:44am UTC](https://discourse.slicer.org/t/how-do-you-call-a-function-when-a-segmentation-is-modified/29774/3 "2023-06-02T08:44:58Z")

</div>

I tend to use this event:

`segmentationNode.AddObserver(slicer.vtkSegmentation.RepresentationModified, self.onSegmentContentModified)`

In this case the callback looks like this so you can see which segment was just modified:

```auto
  @vtk.calldata_type(vtk.VTK_STRING)
  def onSegmentContentModified(self, caller, eventId, callData=None):
    segmentID = callData
    ...

```

Also just for completeness I checked SegmentationChangedEvent and it seems to be for something else:

> <https://github.com/Slicer/Slicer/blob/5e7d60c87711a14902f9001330321e1e494a7e6b/Libs/MRML/Core/vtkMRMLSegmentationNode.h#L303>
