Update UI widget after programmatically set values in volume node

I adapted the code from the activate hanging protocol example to set the color window/levels. (Script repository — 3D Slicer documentation))

The volume updates correctly and shows the correct color levels, but the UI widgets are not updated. Specifically the window/level widgets in the Volume UI.

This code sets the values in the volume

petNode = slicer.util.getNodesByClass('vtkMRMLScalarVolumeNode'):
petColor = slicer.mrmlScene.GetFirstNodeByName('PET-Heat')
petNode.GetVolumeDisplayNode().SetAndObserveColorNodeID(petColor.GetID())
petNode.GetVolumeDisplayNode().SetWindowLevelMinMax(0, 20)

How do the UI widgets get notified of the change?

thanks.

The GUI updates automatically when the MRML nodes are modified. Volumes module probably showed a different volume than what you modified. Or maybe you modified the volume rendering display node. This code snippet changes scalar volume display nodes of all loaded volumes:

petColor = slicer.mrmlScene.GetFirstNodeByName('PET-Heat')
petNodes = slicer.util.getNodesByClass('vtkMRMLScalarVolumeNode')
for petNode in petNodes:
    displayNode = petNode.GetScalarVolumeDisplayNode()
    displayNode.SetAndObserveColorNodeID(petColor.GetID())
    displayNode.SetWindowLevelMinMax(0, 20)

Thanks, much appreciated. The UI widgets were being updated.

I worked out I needed to explicitly turn off auto window level before setting the window/level values so they weren’t immediately overridden in the UI widget.

displayNode = petNode.GetScalarVolumeDisplayNode()
displayNode.SetAndObserveColorNodeID(petColor.GetID())
displayNode.AutoWindowLevelOff()
displayNode.SetWindowLevelMinMax(0, 20)

It works now. Thanks.

1 Like