# How to add a observer for monitoring slice views changes?

**URL:** <https://discourse.slicer.org/t/how-to-add-a-observer-for-monitoring-slice-views-changes/20621>\
**Category:** Development\
**Tags:** python\
**Created:** [November 15, 2021, 7:39am UTC](https://discourse.slicer.org/t/how-to-add-a-observer-for-monitoring-slice-views-changes/20621 "2021-11-15T07:39:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luke199208](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/luke199208/32/12902_2.png) [@Luke199208](https://discourse.slicer.org/u/Luke199208)\
**Post date:** [November 15, 2021, 7:39am UTC](https://discourse.slicer.org/t/how-to-add-a-observer-for-monitoring-slice-views-changes/20621/1 "2021-11-15T07:39:23Z")

</div>

system: Windows 10  
Slicer-version: 4.10.0  
Hello,  
I am trying to add a observer to monitor the number of slice views changes. But the right way was not found. Which node and event should be used ?  
Slice views changes like this:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/2/f21fc96db99a51a040d199cc5c49afe98292bb57.png)  
– to → ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/f/cfe0cb42143ff6b9f62779f147a1a57da8f9cd47.png)

I want to trigger my own method after the event has occurred.

Here is the method of slice views changes:

```auto
layoutManager = slicer.app.layoutManager()
      customLayout = """
                <layout type="vertical">
                <item>
                <layout type="horizontal">
                  <item splitSize="300">
                  <view class="vtkMRMLSliceNode" singletontag="Red">
                    <property name="orientation" action="default">Axial</property>
                    <property name="viewlabel" action="default">R</property>
                    <property name="viewcolor" action="default">#F34A33</property>
                  </view>
                  </item>
                  <item splitSize="300">
                  <view class="vtkMRMLSliceNode" singletontag="Red1">
                    <property name="orientation" action="default">Axial</property>
                    <property name="viewlabel" action="default">R</property>
                    <property name="viewcolor" action="default">#F34A33</property>
                  </view>
                  </item>
                </layout>
                </item>
                <item>
                <layout type="horizontal">
                  <item splitSize="300">
                    <view class="vtkMRMLSliceNode" singletontag="Red2">
                        <property name="orientation" action="default">Axial</property>
                        <property name="viewlabel" action="default">R</property>
                        <property name="viewcolor" action="default">#F34A33</property>
                    </view>
                  </item>
                  <item splitSize="300">
                    <view class="vtkMRMLSliceNode" singletontag="Red3">
                        <property name="orientation" action="default">Axial</property>
                        <property name="viewlabel" action="default">R</property>
                        <property name="viewcolor" action="default">#F34A33</property>
                    </view>
                  </item>
                </layout>
                </item>
                </layout>
                """
      # Built-in layout IDs are all below 100, so you can choose any large random number
      # for your custom layout ID.
      customLayoutId = 503

      layoutManager.layoutLogic().GetLayoutNode().AddLayoutDescription(customLayoutId, customLayout)

      # Switch to the new custom layout
      layoutManager.setLayout(customLayoutId)

```

```auto
customLayout = """
                        <layout type="horizontal">
                          <item splitSize="500">
                          <view class="vtkMRMLSliceNode" singletontag="Red">
                            <property name="orientation" action="default">Axial</property>
                            <property name="viewlabel" action="default">R</property>
                            <property name="viewcolor" action="default">#F34A33</property>
                          </view>
                          </item>
                          <item splitSize="500">
                          <view class="vtkMRMLSliceNode" singletontag="Red1">
                            <property name="orientation" action="default">Axial</property>
                            <property name="viewlabel" action="default">R</property>
                            <property name="viewcolor" action="default">#F34A33</property>
                          </view>
                          </item>
                        </layout>
                        """
      # Built-in layout IDs are all below 100, so you can choose any large random number
      # for your custom layout ID.
      customLayoutId = 502

      layoutManager.layoutLogic().GetLayoutNode().AddLayoutDescription(customLayoutId, customLayout)

      # Switch to the new custom layout
      layoutManager.setLayout(customLayoutId)

```

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 15, 2021, 1:30pm UTC](https://discourse.slicer.org/t/how-to-add-a-observer-for-monitoring-slice-views-changes/20621/2 "2021-11-15T13:30:03Z")

</div>

You can add an observer to `layoutManager.layoutLogic().GetLayoutNode()` to get notified about layout changes.

---

<div class="post-metadata">

**Author:** ![Luke199208](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/luke199208/32/12902_2.png) [@Luke199208](https://discourse.slicer.org/u/Luke199208)\
**Post date:** [November 16, 2021, 2:25am UTC](https://discourse.slicer.org/t/how-to-add-a-observer-for-monitoring-slice-views-changes/20621/3 "2021-11-16T02:25:42Z")

</div>

Thank you so much for your support lassoan!  
I used the following method(`slicer.util.getNode("Layout").AddObserver(vtk.vtkCommand.ModifiedEvent, callbackFunc)`) to achieve what I want. But I don’t know if there will be other problems.
