Got the wrong color on the cell

Hi guys,

I’m new to Slicer and VTK library, for some special requirements, I need to set different cell to different color on the surface, so I have such draft code as below:

# Hide the cube
v = slicer.util.getNode('View1')

# Sphere
sphereSource = vtk.vtkSphereSource()
sphereSource = sphereSource.GetOutput() # vtkPolyData()

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

colors = vtk.vtkUnsignedCharArray()
for c in range(sphereSource.GetNumberOfCells()):
    #Set color to green, but got red color actually
    colors.SetTuple(c, [0.0, 255.0, 0.0])

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

As the above code, I want to set the cell’s color to green, but I got red color actually. I really don’t know what’s wrong with the code.

environment information:

OS: Win10 x64
3D Slicer version: 4.11.20200930

Thanks a lot for your great help!

Best regards

Jack X.

See a complete working example for this in the script repository.

Thanks a lot for your quick reply. I managed to update the example script and run in my environment, unfortunately, it didn’t solve my problem, my question is: 1. I have markupsNode composed by many points in it, each point has its own attribute(for example, Cardiac mapping point LAT etc.), I want to coloring the closest cell of each point with different color according to the point’s different attribute(maybe the color table is composed by some colors gradient from red to purple or whatever). But seems the example script cannot do it, I got the screenshot as below(each cell coloring with the same color):

I have also update the SetAndObserveColorNodeID() method’s parameter from “vtkMRMLColorTableNodeWarm1” to other value, it got almost the same result.
are there any solutions for it? look forward to your kindly reply. Thank you so much!

Hi Andras, thanks a lot for your quick response. Could you pls check my comments as below. Thanks!

If you want to show different colors then you have two options:

  • Option A: set different scalar values in the cell data and set colors in the Color node associated with the model
  • Option B: if you want to assign arbitrary color to each surface patch (that cannot be derived from a single scalar value using a colormap) then set the number of components in the cell data to 3 and specify RGB values (and choose direct mapping scalar mode in the display node)
1 Like

Hi Andras, thanks a lot for your help! I will try it.