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

**URL:** https://discourse.vtk.org/t/color-hide-blocks-on-a-vtkmultiblockdataset-using-vtkcompositepolydatamapper2-and-the-block-fielddata/3755
**Category:** Support
**Created:** [July 13, 2020, 2:06am UTC](https://discourse.vtk.org/t/color-hide-blocks-on-a-vtkmultiblockdataset-using-vtkcompositepolydatamapper2-and-the-block-fielddata/3755 "2020-07-13T02:06:40Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![scotsman60](https://discourse.vtk.org/user_avatar/discourse.vtk.org/scotsman60/32/6000_2.png) [@scotsman60](https://discourse.vtk.org/u/scotsman60)
#### Post date: [July 13, 2020, 2:06am UTC](https://discourse.vtk.org/t/color-hide-blocks-on-a-vtkmultiblockdataset-using-vtkcompositepolydatamapper2-and-the-block-fielddata/3755/1 "2020-07-13T02:06:40Z")

</div>

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

 ![image](https://discourse.vtk.org/uploads/default/original/2X/1/101f846c18a92c4bbfa795d7fa051a0ebe715291.jpeg)

```
#!/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()
```
