# Strange behavior between 3D Slicer and pure VTK

**URL:** <https://discourse.slicer.org/t/strange-behavior-between-3d-slicer-and-pure-vtk/16748>\
**Category:** Development\
**Created:** [March 24, 2021, 1:07pm UTC](https://discourse.slicer.org/t/strange-behavior-between-3d-slicer-and-pure-vtk/16748 "2021-03-24T13:07:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackxiong](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@jackxiong](https://discourse.slicer.org/u/jackxiong)\
**Post date:** [March 24, 2021, 1:07pm UTC](https://discourse.slicer.org/t/strange-behavior-between-3d-slicer-and-pure-vtk/16748/1 "2021-03-24T13:07:58Z")

</div>

Hello all,  
I have met a development issue here, I want to coloring different cell with different color, I have draft code with VTK(not in 3D Slicer), the code as below:

```python
import vtk

sphereSource = vtk.vtkSphereSource()
sphereSource.SetCenter(0,0,0)
sphereSource.SetRadius(60.0)
sphereSource.Update()
sphereSource = sphereSource.GetOutput() # vtkPolyData()

cellData = vtk.vtkUnsignedCharArray()
cellData.SetNumberOfComponents(3)
cellData.SetNumberOfTuples(sphereSource.GetNumberOfCells())
cellData.SetName('Colors')
for i in range(0,sphereSource.GetNumberOfCells()):
    cellData.InsertTuple(i,(255,255,255))

sphereSource.GetCellData().SetScalars(cellData)
sphereSource.Modified()
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(sphereSource)
actor = vtk.vtkActor()
actor.SetMapper(mapper)

renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
renderWindowInteractor.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
renderer.AddActor(actor)
renderer.SetBackground((1,1,1))
renderWindow.Render()
renderWindowInteractor.Start()
and the result as below photo:
![图片|302x332](upload://kU4E3fiQzwRcKBvkAUpOsobPYF0.png) 

```

Almost the same code in 3D Slicer python console:

```python
# Hide the cube
v = slicer.util.getNode('View1')
v.SetBoxVisible(False)

# Sphere
sphereSource = vtk.vtkSphereSource()
sphereSource.SetCenter(0,0,0)
sphereSource.SetRadius(60.0)
sphereSource.Update()
sphereSource = sphereSource.GetOutput() # vtkPolyData()

# Create a model with the sphere and add it to scene
modelNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLModelNode')
modelNode.SetAndObservePolyData(sphereSource)
modelNode.CreateDefaultDisplayNodes()
modelNode.SetName('My sphere')

cellData = vtk.vtkUnsignedCharArray()
cellData.SetNumberOfComponents(3)
cellData.SetNumberOfTuples(sphereSource.GetNumberOfCells())
cellData.SetName('Colors')
for i in range(0,sphereSource.GetNumberOfCells()):
    cellData.InsertTuple(i,(255.0, 255.0, 255.0))

sphereSource.GetCellData().SetScalars(cellData)
sphereSource.Modified()

# Set up coloring by selection array
#modelNode.GetDisplayNode().SetActiveScalar("Colors", vtk.vtkAssignAttribute.CELL_DATA)
modelNode.GetDisplayNode().SetAndObserveColorNodeID("")
modelNode.GetDisplayNode().SetScalarVisibility(True)

```

But the result as below photo:

 ![图片](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/c/5cb8958137b6f0e4527a54b264ff8b9534d34dcd.png)

Both code want to set all of the cells on the surface to white color (255.0, 255.0, 255.0), but I got different result, I have referenced c++ code as below link:

[https://kitware.github.io/vtk-examples/site/Cxx/PolyData/ColorCellsWithRGB/](https://kitware.github.io/vtk-examples/site/Cxx/PolyData/ColorCellsWithRGB/)

The result in 3D Slicer is not what I want. Am I missing something or need some extra configuration on 3D Slicer? Look forward to your kindly reply. Thanks in advance!  
environment:  
OS: Win10 x64  
3D Slicer version: 4.11.20200930

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [March 24, 2021, 1:11pm UTC](https://discourse.slicer.org/t/strange-behavior-between-3d-slicer-and-pure-vtk/16748/2 "2021-03-24T13:11:55Z")

</div>

You need to add back the line that you commented out (to set the active scalar); and set scalar range flag (to UseDirectMapping).

---

<div class="post-metadata">

**Author:** ![jackxiong](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@jackxiong](https://discourse.slicer.org/u/jackxiong)\
**Post date:** [March 24, 2021, 2:13pm UTC](https://discourse.slicer.org/t/strange-behavior-between-3d-slicer-and-pure-vtk/16748/3 "2021-03-24T14:13:12Z")

</div>

I have updated the code as your suggestion, it works very well. Thanks a lot for your kindly help!
