# Alternative to recommended way to respond to volume loading

**URL:** <https://discourse.slicer.org/t/alternative-to-recommended-way-to-respond-to-volume-loading/40906>\
**Category:** Development\
**Created:** [December 30, 2024, 3:12pm UTC](https://discourse.slicer.org/t/alternative-to-recommended-way-to-respond-to-volume-loading/40906 "2024-12-30T15:12:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shai-ikko](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/shai-ikko/32/15765_2.png) [@shai-ikko](https://discourse.slicer.org/u/shai-ikko)\
**Post date:** [December 30, 2024, 3:12pm UTC](https://discourse.slicer.org/t/alternative-to-recommended-way-to-respond-to-volume-loading/40906/1 "2024-12-30T15:12:11Z")

</div>

Hi all,

The script repository recommends, if you want to respond to a volume being loaded, to [respond to the Node-Added event](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#show-volume-rendering-automatically-when-a-volume-is-loaded), but then call the responding method using a timer:

```python
    # Call showVolumeRendering using a timer instead of calling it directly
    # to allow the volume loading to fully complete.
    qt.QTimer.singleShot(0, lambda: showVolumeRendering(node))

```

In my experience (Slicer 5.6.2), this doesn’t work well when the volume loading is not immediate (e.g. loading a 300M Minc2 file typically takes a few seconds on my machine) – the listener is called before loading finishes.

I found an alternative – listen, instead, for changes in the scene’s Selection Node:

(in a module widget’s `setup()`: )

```python
        selectionNode = slicer.app.applicationLogic().GetSelectionNode()
        self.addObserver(selectionNode, vtk.vtkCommand.ModifiedEvent, self.onSelectionModified)

```

The Selection Node is changed only when the volume loading ends, so the responding code can handle it without waiting further.

I’ve seen some posts and comments objecting to the idea of a single selected volume; but the functionality is still there, and using it is still [recommended in a similar context](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#show-a-volume-in-slice-views).

Is there some non-obvious disadvantage to this method?

Thanks!

---

<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:** [December 31, 2024, 4:03pm UTC](https://discourse.slicer.org/t/alternative-to-recommended-way-to-respond-to-volume-loading/40906/2 "2024-12-31T16:03:57Z")

</div>

This makes sense because sometimes the volume loading shows a transient progress bar, and that could be what triggers the singleShot. Using the selection node as a workaround is also reasonable, and it’s nice that it doesn’t rely on Qt.

Another option could be to check the `slicer.mrmlScene.IsImporting()` and not do the volume rendering option until it’s False (i.e. just reset the singleShot until the importing is over). I haven’t tested this but it might work.

It would be great if you could a PR to the script repository with whatever you decide is the best solution.

---

<div class="post-metadata">

**Author:** ![shai-ikko](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/shai-ikko/32/15765_2.png) [@shai-ikko](https://discourse.slicer.org/u/shai-ikko)\
**Post date:** [January 8, 2025, 1:06pm UTC](https://discourse.slicer.org/t/alternative-to-recommended-way-to-respond-to-volume-loading/40906/3 "2025-01-08T13:06:03Z")

</div>

Thanks again for your advice and support.

> <https://github.com/Slicer/Slicer/pull/8127>
>
> This is useful also for responding to volume loading in some cases, so also add …a reference to the new section from the existing "Show volume rendering automatically when a volume is loaded".
> 
> See https://discourse.slicer.org/t/alternative-to-recommended-way-to-respond-to-volume-loading/40906
