Problem in loading volume rendering automatically

I would like to load the volume rendering automatically when I open the Slicer.

I follow the code in this link, but the volume rendering cannot load automatically.

How should I solve this error?

The code snippet you linked works for me when I include it in my, with volume rendering being enabled automatically when I load MRHead.

The error/warning displayed in your console may be due to something else in your, since loadNodeFromFile is not part of the code snippet you copied

My main error is the volume rendering cannot be enabled automatically. The warning displayed in my console is the warning of my other functions which is work normally. When I disable those functions, the automatic volume rendering still cannot work.

This is my code in

import SampleData

def reportProgress(msg, level=None):
  # Print progress in the console
  print("Loading... {0}%".format(sampleDataLogic.downloadPercent))
  # Abort download if cancel is clicked in progress bar
  if slicer.progressWindow.wasCanceled:
    raise Exception("download aborted")
  # Update progress window
  # Process events to allow screen to refresh

  volumeNode = None
  slicer.progressWindow = slicer.util.createProgressDialog()
  sampleDataLogic = SampleData.SampleDataLogic()
  sampleDataLogic.logMessage = reportProgress
  loadedNodes = sampleDataLogic.downloadFromURL(
  volumeNode = loadedNodes[0]

def onNodeAdded(caller, event, calldata):
  node = calldata
  if isinstance(node, slicer.vtkMRMLVolumeNode):
    # 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))

def showVolumeRendering(volumeNode):
  print("Show volume rendering of node " + volumeNode.GetName())
  volRenLogic = slicer.modules.volumerendering.logic()
  displayNode = volRenLogic.CreateDefaultVolumeRenderingNodes(volumeNode)
  scalarRange = volumeNode.GetImageData().GetScalarRange()
  if scalarRange[1]-scalarRange[0] < 1500:
    # Small dynamic range, probably MRI
    # Larger dynamic range, probably CT

slicer.mrmlScene.AddObserver(slicer.vtkMRMLScene.NodeAddedEvent, onNodeAdded)

This is the result now.

Is my code has anything wrong?

The decorator @vtk.calldata_type(vtk.VTK_OBJECT) is missing above onNodeAdded

I thought that the first line is useless :joy:.
It works now. Thanks you so much.

1 Like