# What is the corresponding event for 'changing the number of 3D widget' in python script?

**URL:** <https://discourse.slicer.org/t/what-is-the-corresponding-event-for-changing-the-number-of-3d-widget-in-python-script/30934>\
**Category:** Development\
**Created:** [August 2, 2023, 2:26pm UTC](https://discourse.slicer.org/t/what-is-the-corresponding-event-for-changing-the-number-of-3d-widget-in-python-script/30934 "2023-08-02T14:26:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick\_Jo](https://avatars.discourse-cdn.com/v4/letter/n/58956e/32.png) [@Nick\_Jo](https://discourse.slicer.org/u/Nick_Jo)\
**Post date:** [August 2, 2023, 2:26pm UTC](https://discourse.slicer.org/t/what-is-the-corresponding-event-for-changing-the-number-of-3d-widget-in-python-script/30934/1 "2023-08-02T14:26:10Z")

</div>

Hello. I’ve been using the SlicerCustomAppTemplate to customize the slicer based on our needs.

I want to apply the customization for ‘every’ 3D widget. I found the following code from the script repository.

```auto
viewNode = slicer.app.layoutManager().threeDWidget(0).mrmlViewNode()
viewNode.SetBoxVisible(False) #Hide the 3d cube

controller = slicer.app.layoutManager().threeDWidget(0).threeDController()
controller.setBlackBackground() #Set the background color to be black

```

The problem is that this only applies to a single 3D widget. That is, if I change the layout type to ‘Dual 3D’ or ‘tripled 3D’, my customization only applies to the widget index 0.

So I change the above code as follows.

```auto
  num_3d_widgets = slicer.app.layoutManager().threeDViewCount
  # print(f"The number of 3d widgets is: {num_3d_widgets}")

  for i in range(num_3d_widgets):
    viewNode = slicer.app.layoutManager().threeDWidget(i).mrmlViewNode()
    viewNode.SetBoxVisible(False) #Hide the 3d cube

    controller = slicer.app.layoutManager().threeDWidget(0).threeDController()
    controller.setBlackBackground() #Set the background black

```

But this still is not perfect, because that script is applied only once when the program starts.  
So, if I change the layout from single 3D widget to dual 3D widget, the customization for the second 3D widget is not applied.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/9/a92bb8584df83f342fe7bcaf02c0990408a60286.png)

How can I write the python script so that whenver the number of 3D widgets change, the above customization automatically applies to all 3D widgets?

I’ve been trying to find the proper event (such as slicer.vtkMRMLScene.NewSceneEvent) for this and the proper usage of slicer.mrmlScene.AddObserver, but this is pretty difficult to me. How should I handle this?

Thank you in advance!

---

<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:** [August 2, 2023, 2:27pm UTC](https://discourse.slicer.org/t/what-is-the-corresponding-event-for-changing-the-number-of-3d-widget-in-python-script/30934/2 "2023-08-02T14:27:57Z")

</div>

You can define any layout you want (any number of any kind of views arranged in any way you prefer). See detailed instructions and examples in the [script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#customize-view-layout).
