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')
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 cube and add it to scene
modelNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLModelNode')
modelNode.SetAndObservePolyData(sphereSource)
modelNode.CreateDefaultDisplayNodes()
modelNode.SetName('My sphere')

colors = vtk.vtkUnsignedCharArray()
colors.SetName('selection')
colors.SetNumberOfComponents(3)
colors.SetNumberOfTuples(sphereSource.GetNumberOfCells())
for c in range(sphereSource.GetNumberOfCells()):
    #Set color to green, but got red color actually
    colors.SetTuple(c, [0.0, 255.0, 0.0])
sphereSource.GetCellData().SetScalars(colors)

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

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.

Hi Andras,
I somehow could not find how to set direct mapping scalar mode in the display node. I am using a model node and try to assign different colors for each point as well. Thank you!

Please provide a sample data set and your current script and we can have a look.

Thank you for the reply! I found the API I need. It is SetScalarRangeFlag() and sets the flag to UseDirectMapping. Then it shows the specified RGB values(0-255).

1 Like