Alternative to recommended way to respond to volume loading

Hi all,

The script repository recommends, if you want to respond to a volume being loaded, to respond to the Node-Added event, but then call the responding method using a timer:

    # 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(): )

        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.

Is there some non-obvious disadvantage to this method?

Thanks!

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.

1 Like

Thanks again for your advice and support.

1 Like