[Volume Rendering] Works via python console but not via extension code

Hi,
When I use the following code in my Slicer Extension (Slicer 4.11.20210226), I am unable to get a volume render in the 3D view (a custom vtkMRMLViewNode).

node = slicer.util.getNode('<nodename>') # a vtkMRMLScalarVolumeNode
volRenLogic = slicer.modules.volumerendering.logic()
displayNode = volRenLogic.CreateDefaultVolumeRenderingNodes(node)
displayNode.SetVisibility(True)

But when I copy paste the same to the python console, it works! Upon checking the volume rendering module, I noticed that my extension code does not set the eye icon to visible, but copy-pasting the code in the python console does that. In my extension, I also use the following displayNode functions to verify if the visibility was set properly and their output is all 1.

print (' ------ [myUtil.set3DBackground()]: ', displayNode.GetVisibility(), displayNode.GetVisibility2D(), displayNode.GetVisibility3D())

What may I be missing here?


Note: Prior to the volume rendering, my extension updates the 3D array that makes up the vtkMRMLScalarVolumeNode being rendered.

May be use a timer callback to set up the volume rendering because you need to give some time to the scalar volume to update I think

Thanks! It worked with the timer!

def set3DBackgroundTimed(node):
    displayNode = volRenLogic.CreateDefaultVolumeRenderingNodes(node)
    displayNode.SetVisibility(True)

qt.QTimer.singleShot(1000, lambda: set3DBackgroundTimed(node))