Color/hide blocks on a vtkMultiBlockDataSet using vtkCompositePolyDataMapper2 and the block FieldData?

I’m working with vtkMultiBlockDataSets and using vtkCompositePolyDataMapper2 using Python 3 and VTK 9 on Windows 10

My model has the color of each block associated with the PolyData as FieldData - a 3 component Scalar array - ‘color’
Reading the documentation for the mapper it appears the following should work.

mapper.SetColorModeToDirectScalars()
mapper.SetScalarModeToUseFieldData()
mapper.SelectColorArray("color")  

I’ve tried,

  1. Using three 8 bit ints to define the RGB color values
  2. Using three doubles to define the RGB colors
  3. Using a single integer and a lookup table

But it doesn’t quite work, but I think it’s close.

Does anyone have any ideas about what I’m doing wrong?

Thanks,

Doug

#!/usr/bin/env python

from itertools import cycle
import vtk

def main():
    colors = vtk.vtkNamedColors()
    # Get the color names into a circular list
    colornames = colors.GetColorNames().split('\n')
    color_pool = cycle(colornames)

offset = 5.0
size = 2
grid_count = 5

mbds = vtk.vtkMultiBlockDataSet()
mbds.SetNumberOfBlocks(grid_count*grid_count)

block=0
color_index=0
x=0.0
for i in range(grid_count):
    y=0.0
    for j in range(grid_count):
        sphere = vtk.vtkSphereSource()
        sphere.SetRadius(size)
        sphere.SetCenter(x, y, 0)
        sphere.Update()
        sphere_poly = sphere.GetOutput()
        mbds.SetBlock(block, sphere_poly)
        vtk_color = vtk.vtkFloatArray()
        #vtk_color.SetNumberOfComponents(1)
        vtk_color.SetNumberOfComponents(3)
        vtk_color.SetName('color')
        #vtk_color.InsertNextTuple([color_index])
        vtk_color.InsertNextTuple(colors.GetColor3d(next(color_pool)))
        color_index += 1
        sphere_poly.GetFieldData().AddArray(vtk_color)
        block+=1
        y+=offset
    x+=offset

mapper = vtk.vtkCompositePolyDataMapper2()
mapper.SetInputDataObject(mbds)
cdsa = vtk.vtkCompositeDataDisplayAttributes()
mapper.SetCompositeDataDisplayAttributes(cdsa)

#mapper.SetColorModeToDefault()
#mapper.SetColorModeToMapScalars()
mapper.SetColorModeToDirectScalars()
mapper.SetScalarModeToUseFieldData()
mapper.SelectColorArray("color")

actor = vtk.vtkActor()
actor.SetMapper(mapper)

# Create the Renderer, RenderWindow, and RenderWindowInteractor.
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)

# Enable user interface interactor.
renderer.AddActor(actor)
renderer.SetBackground(colors.GetColor3d("SteelBlue"))
renderWindow.Render()
renderWindowInteractor.Start()

if __name__ == '__main__':
    main()