Strange behavior between 3D Slicer and pure VTK

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:

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:

# 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:
图片

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/

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

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

1 Like

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

1 Like