# Problem in loading volume rendering automatically

**URL:** https://discourse.slicer.org/t/problem-in-loading-volume-rendering-automatically/21747
**Category:** Support
**Tags:** python, volume-rendering
**Created:** [February 2, 2022, 7:25am UTC](https://discourse.slicer.org/t/problem-in-loading-volume-rendering-automatically/21747 "2022-02-02T07:25:58Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![user5](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/user5/32/13217_2.png) [@user5](https://discourse.slicer.org/u/user5)
#### Post date: [February 2, 2022, 7:25am UTC](https://discourse.slicer.org/t/problem-in-loading-volume-rendering-automatically/21747/1 "2022-02-02T07:25:58Z")

</div>

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.

[https://slicer.readthedocs.io/en/latest/developer\_guide/script\_repository.html#show-volume-rendering-automatically-when-a-volume-is-loaded](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#show-volume-rendering-automatically-when-a-volume-is-loaded)

 ![截圖 2022-02-02 下午3.25.28](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/c/ccbda0a463723f46aa258833d53cf20d81ef11e1.jpeg)

How should I solve this error?

---

<div class="post-metadata">

### Author: ![ebrahim](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ebrahim/32/13403_2.png) [@ebrahim](https://discourse.slicer.org/u/ebrahim)
#### Post date: [February 2, 2022, 11:44am UTC](https://discourse.slicer.org/t/problem-in-loading-volume-rendering-automatically/21747/2 "2022-02-02T11:44:51Z")

</div>

The code snippet you linked works for me when I include it in my `.slicerrc.py`, 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 `.slicerrc.py`, since `loadNodeFromFile` is not part of the code snippet you copied

---

<div class="post-metadata">

### Author: ![user5](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/user5/32/13217_2.png) [@user5](https://discourse.slicer.org/u/user5)
#### Post date: [February 2, 2022, 12:24pm UTC](https://discourse.slicer.org/t/problem-in-loading-volume-rendering-automatically/21747/3 "2022-02-02T12:24:08Z")

</div>

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 `.slicerrc.py`

```auto
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
  slicer.progressWindow.show()
  slicer.progressWindow.activateWindow()
  slicer.progressWindow.setValue(int(sampleDataLogic.downloadPercent))
  slicer.progressWindow.setLabelText("Downloading...")
  # Process events to allow screen to refresh
  slicer.app.processEvents()

try:
  volumeNode = None
  slicer.progressWindow = slicer.util.createProgressDialog()
  sampleDataLogic = SampleData.SampleDataLogic()
  sampleDataLogic.logMessage = reportProgress
  loadedNodes = sampleDataLogic.downloadFromURL(
    nodeNames="MRHead",
    fileNames="MR-head25.nrrd",
    uris="https://github.com/Slicer/SlicerTestingData/releases/download/SHA256/cc211f0dfd9a05ca3841ce1141b292898b2dd2d3f08286affadf823a7e58df93",
    checksums="SHA256:cc211f0dfd9a05ca3841ce1141b292898b2dd2d3f08286affadf823a7e58df93")
  volumeNode = loadedNodes[0]
finally:
  slicer.progressWindow.close()

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)
  displayNode.SetVisibility(True)
  scalarRange = volumeNode.GetImageData().GetScalarRange()
  if scalarRange[1]-scalarRange[0] < 1500:
    # Small dynamic range, probably MRI
    displayNode.GetVolumePropertyNode().Copy(volRenLogic.GetPresetByName("MR-Default"))
  else:
    # Larger dynamic range, probably CT
    displayNode.GetVolumePropertyNode().Copy(volRenLogic.GetPresetByName("CT-Chest-Contrast-Enhanced"))

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

```

This is the result now.

 ![截圖 2022-02-02 下午8.34.59](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/9/89fba684af374eeb1e14e3ad4a2f7489525da439.jpeg)

Is my code has anything wrong?

---

<div class="post-metadata">

### Author: ![ebrahim](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ebrahim/32/13403_2.png) [@ebrahim](https://discourse.slicer.org/u/ebrahim)
#### Post date: [February 2, 2022, 12:48pm UTC](https://discourse.slicer.org/t/problem-in-loading-volume-rendering-automatically/21747/4 "2022-02-02T12:48:20Z")

</div>

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

---

<div class="post-metadata">

### Author: ![user5](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/user5/32/13217_2.png) [@user5](https://discourse.slicer.org/u/user5)
#### Post date: [February 2, 2022, 12:58pm UTC](https://discourse.slicer.org/t/problem-in-loading-volume-rendering-automatically/21747/5 "2022-02-02T12:58:03Z")

</div>

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