# Cannot call SetScalar()

**URL:** https://discourse.vtk.org/t/cannot-call-setscalar/14419
**Category:** Support
**Tags:** code
**Created:** [August 15, 2024, 5:04am UTC](https://discourse.vtk.org/t/cannot-call-setscalar/14419 "2024-08-15T05:04:43Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Taco\_Z](https://discourse.vtk.org/user_avatar/discourse.vtk.org/taco_z/32/9184_2.png) [@Taco\_Z](https://discourse.vtk.org/u/Taco_Z)
#### Post date: [August 15, 2024, 5:04am UTC](https://discourse.vtk.org/t/cannot-call-setscalar/14419/1 "2024-08-15T05:04:43Z")

</div>

I’m new to VTK and I try to use VTK9.1 + QT6 to display points cloud.  
I found tutorials and examples for displaying points cloud with different color by calling SetScalar().  
Here’s the code:

vtkSmartPointer points = vtkSmartPointer::New();  
vtkSmartPointer colors = vtkSmartPointer::New();  
colors-\>SetNumberOfComponents(3); // RGB  
colors-\>SetName(“Colors”);

//Here’s code to read points cloud  
points-\>InsertNextPoint(x,y,z);  
//here’s code for color  
…  
unsigned char color[3] = {r, g, b};  
colors-\>InsertNextTypedTuple(color);

//  
// display  
vtkSmartPointer polyData = vtkSmartPointer::New();  
polyData-\>SetPoints(points);

// basically this is the typical way to add colors to points, but I cannot call SetScalars(colors) after “polyData-\>GetPointData()”. I don’t know how to fix this.  
polyData-\>GetPointData()-\>SetScalars(colors);

can anyone help me ? I want a colorful points cloud model in the window.

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [August 15, 2024, 12:25pm UTC](https://discourse.vtk.org/t/cannot-call-setscalar/14419/2 "2024-08-15T12:25:47Z")

</div>

I don’t do much VTK C++, but from [the documentation](https://vtk.org/doc/nightly/html/classvtkDataSetAttributes.html#a426009e9d6ac5a4b2cda670aed78042d), SetScalars requires a vtkDataArray, have you converted `colors` to a vtkDataArray?

---

<div class="post-metadata">

### Author: ![Taco\_Z](https://discourse.vtk.org/user_avatar/discourse.vtk.org/taco_z/32/9184_2.png) [@Taco\_Z](https://discourse.vtk.org/u/Taco_Z)
#### Post date: [August 20, 2024, 5:09am UTC](https://discourse.vtk.org/t/cannot-call-setscalar/14419/3 "2024-08-20T05:09:58Z")

</div>

yes, I found it should be declared by this type rather than use smartpointer

rexthor via VTK \<[noreply@discourse.vtk.org](mailto:noreply@discourse.vtk.org)\> 于2024年8月15日周四 20:25写道：
