# Volume rendering Error

**URL:** https://discourse.vtk.org/t/volume-rendering-error/2690
**Category:** Support
**Created:** [February 24, 2020, 5:26pm UTC](https://discourse.vtk.org/t/volume-rendering-error/2690 "2020-02-24T17:26:57Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Alex1](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@Alex1](https://discourse.vtk.org/u/Alex1)
#### Post date: [February 24, 2020, 5:26pm UTC](https://discourse.vtk.org/t/volume-rendering-error/2690/1 "2020-02-24T17:26:57Z")

</div>

Hi all,  
I have a Problem with the volume rendering. If I try to use the vtkSmartVolumeMapper i get the following Error:

```
ERROR: vtkSmartVolumeMapper (000002A431C3C1B0): Could not find the requested vtkDataArray! 0, 0, -1, 

```

My File: [https://drive.google.com/open?id=1bXapjrNTirGECwRymt9SNChx2lyIpaV8](https://drive.google.com/open?id=1bXapjrNTirGECwRymt9SNChx2lyIpaV8)  
My Code is as follows:

```auto
 #include <vtkRenderWindow.h>
 #include <vtkRenderWindowInteractor.h>
 #include <vtkRenderer.h>
 #include <vtkSmartPointer.h>
 #include <vtkStructuredPointsReader.h>
 #include <vtkPiecewiseFunction.h>
 #include <vtkSmartVolumeMapper.h>
 #include <vtkColorTransferFunction.h>
 #include <vtkVolume.h>
 #include <vtkVolumeProperty.h>
 #include <vtkRectilinearGridReader.h>
 #include <vtkSmartPointer.h>
 #include <vtkNetCDFReader.h>

 int main()
 {

     // Add named color library
     vtkSmartPointer<vtkNamedColors> colors = vtkSmartPointer<vtkNamedColors>::New();

     // Create renderer
     vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();

     // Create a new render window
     vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
     renWin->AddRenderer(renderer);

     // Make the render window interacting
     vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
     iren->SetRenderWindow(renWin);

     vtkSmartPointer<vtkStructuredPointsReader> reader = vtkSmartPointer<vtkStructuredPointsReader>::New();
     
     string filenameNC = "C:\\snapshot_ql_00081.nc";
     vtkSmartPointer<vtkNetCDFReader> cdfReader = vtkSmartPointer<vtkNetCDFReader>::New();
     cdfReader->SetFileName(filenameNC.c_str());
     cdfReader->UpdateInformation();
     cdfReader->SetDimensions("z,y,x");
     cdfReader->SetVariableArrayStatus("ql_3D", 1);
  
     // Add a piece-wise function for color transfer functions. Piece-wise means 
     // adding control (interpolation) points.
     vtkSmartPointer<vtkPiecewiseFunction> opacityTransferFunction = vtkSmartPointer<vtkPiecewiseFunction>::New();
     opacityTransferFunction->AddPoint(0, 0.0);
     opacityTransferFunction->AddPoint(0.342, 1.0);

     // Piece-wise function cannot be used for colors because colors are vectors
     vtkSmartPointer<vtkColorTransferFunction> colorTransferFunction = vtkSmartPointer<vtkColorTransferFunction> ::New();
     colorTransferFunction->AddRGBPoint(0, 0.23, 0.29, 0.75);
     colorTransferFunction->AddRGBPoint(0.171, 0.860, 0.860, 0.860);
     colorTransferFunction->AddRGBPoint(0.342, 0.70, 0.010, 0.140);

     // Set volume rendering properties
     vtkSmartPointer<vtkVolumeProperty> volumeProperty = vtkSmartPointer<vtkVolumeProperty>::New();
     volumeProperty->SetColor(colorTransferFunction);
     volumeProperty->SetScalarOpacity(opacityTransferFunction);
     volumeProperty->ShadeOn();
     volumeProperty->SetInterpolationTypeToLinear();

     // Add a mapper to create graphic primitives from the data
     vtkSmartPointer<vtkSmartVolumeMapper> mapper = vtkSmartPointer<vtkSmartVolumeMapper>:New();
     mapper->SetBlendModeToComposite();
     mapper->SetInputConnection(cdfReader->GetOutputPort());

     // Create a new actor(the actual graphics object) and add the mapped data to 
     // it
     vtkSmartPointer<vtkVolume> volume = vtkSmartPointer<vtkVolume>::New();
     volume->SetMapper(mapper);
     volume->SetProperty(volumeProperty);

     // Add the volume actor to the renderer
     renderer->AddVolume(volume);

     // Set the background color

     renderer->SetBackground(colors->GetColor3d("Black").GetData());

     // Set the size of the render window
     renWin->SetSize(512, 512);

     // Render the data 
     renWin->Render();

     // Start the interactor
     iren->Start();

     return 0;
 }

```

Thanks for your help in advance 🙂

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 26, 2020, 12:57pm UTC](https://discourse.vtk.org/t/volume-rendering-error/2690/2 "2020-02-26T12:57:25Z")

</div>

Hi, Alex!  
That error is thrown by [https://github.com/Kitware/VTK/blob/master/Rendering/VolumeOpenGL2/vtkSmartVolumeMapper.cxx](https://github.com/Kitware/VTK/blob/master/Rendering/VolumeOpenGL2/vtkSmartVolumeMapper.cxx) here:

```auto
    vtkDataArray* scalars = this->GetScalars(input, this->ScalarMode, this->ArrayAccessMode,
    this->ArrayId, this->ArrayName, usingCellColors);

  if (!scalars)
  {
    vtkErrorMacro("Could not find the requested vtkDataArray! "
      << this->ScalarMode << ", " << this->ArrayAccessMode << ", " << this->ArrayId << ", "
      << this->ArrayName);
    this->Initialized = 0;
    return;
  }

```

This implies that the call to `GetScalars()` is returning a null pointer. So, I guess you have to do a `mapper->SelectScalarArray([id of array goes here])`.

regards,

Paulo

---

<div class="post-metadata">

### Author: ![Alex1](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@Alex1](https://discourse.vtk.org/u/Alex1)
#### Post date: [February 29, 2020, 8:39am UTC](https://discourse.vtk.org/t/volume-rendering-error/2690/3 "2020-02-29T08:39:51Z")

</div>

Hi Paulo,  
i tried `mapper->SelectScalarArray([id of array goes here])` an get the same error.

Best regards,  
Alex

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 29, 2020, 9:11pm UTC](https://discourse.vtk.org/t/volume-rendering-error/2690/4 "2020-02-29T21:11:07Z")

</div>

Hi. Which value did you use for id?

---

<div class="post-metadata">

### Author: ![Alex1](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@Alex1](https://discourse.vtk.org/u/Alex1)
#### Post date: [March 3, 2020, 5:31pm UTC](https://discourse.vtk.org/t/volume-rendering-error/2690/5 "2020-03-03T17:31:21Z")

</div>

I tried 0,1,2,3 … but nothing happend. I also tried to use `mapper->SelectScalarArray("ql_3D")`  
the same Array I use with `cdfReader->SetVariableArrayStatus("ql_3D")` but again I get the error:  
`vtkSmartVolumeMapper (000002AF1B0803C0): Could not find the requested vtkDataArray! 0, 1, -1, ql_3D`

---

<div class="post-metadata">

### Author: ![Alex1](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@Alex1](https://discourse.vtk.org/u/Alex1)
#### Post date: [March 7, 2020, 10:28am UTC](https://discourse.vtk.org/t/volume-rendering-error/2690/6 "2020-03-07T10:28:54Z")

</div>

I have a solution for my Problem. I need to use `vtkAssignAttribute` to assign an array which I want to use and change the reader from `vtkNetCDFReader` to `vtkNetCDFCFReader`

````auto
    // Create renderer
    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();

    // Create a new render window
    vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
    renWin->AddRenderer(renderer);

    // Make the render window interacting
    vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    iren->SetRenderWindow(renWin);

    string filenameNC = "C:\\snapshot_ql_00081.nc";
    
    vtkSmartPointer<vtkNetCDFCFReader> cdfCFReader = vtkSmartPointer<vtkNetCDFCFReader>::New();
    cdfCFReader->SetFileName(filenameNC.c_str());
    //cdfCFReader->SetDimensions("z,y,x");
    cdfCFReader->SetVariableArrayStatus("ql_3D", 1);
    cdfCFReader->Update();

	// Assign an Array which we well use 
    vtkSmartPointer<vtkAssignAttribute> assignAttribute = vtkSmartPointer<vtkAssignAttribute>::New();
    assignAttribute->SetInputConnection(cdfCFReader->GetOutputPort());
    assignAttribute->Assign("ql_3D", "SCALARS", "POINT_DATA");
	
 // Piece-wise function cannot be used for colors because colors are vectors
    vtkSmartPointer<vtkPiecewiseFunction> opacityTransferFunction = vtkSmartPointer<vtkPiecewiseFunction>::New();
    opacityTransferFunction->AddPoint(0, 0.0);
    opacityTransferFunction->AddPoint(0.342, 1.0);
	
	// Set volume rendering properties
    vtkSmartPointer<vtkVolumeProperty> volumeProperty = vtkSmartPointer<vtkVolumeProperty>::New();
    volumeProperty->SetColor(colorTransferFunction);
    volumeProperty->SetScalarOpacity(opacityTransferFunction);
    volumeProperty->ShadeOn();
    volumeProperty->SetInterpolationTypeToLinear();
	
	// Add a mapper to create graphic primitives from the data
    vtkSmartPointer<vtkSmartVolumeMapper> mapper = vtkSmartPointer<vtkSmartVolumeMapper>::New();
	mapper->SetBlendModeToComposite();
    mapper->SetInputConnection(assignAttribute->GetOutputPort());
	
	// Create a new actor(the actual graphics object) and add the mapped data to it
    vtkSmartPointer<vtkVolume> volume = vtkSmartPointer<vtkVolume>::New();
    volume->SetMapper(mapper);
    volume->SetProperty(volumeProperty);
    
    // Add the volume actor to the renderer
    renderer->AddVolume(volume);
	
	// Render the data 
    renWin->Render();
	
	// Start the interactor
    iren->Start(); ```
````
