Have ParaView use the Active Scalars as the Display Coloring

I am creating Python based file reader algorithms and filters and I am curious how I could properly set the Active Scalars array on the output data object so that ParaView will automaticcaly color by that array. I’ve done this before by accident and deleted the code so I am pretty sure this is possible.

Let’s start with a simple Programmable Source. The following code (taken from this post) creates some point data and adds some random numbers as a data array for vtkPolyData:

import vtk
from random import uniform
import numpy as np
from vtk.util import numpy_support as nps

points = vtk.vtkPoints()
lines = vtk.vtkCellArray()
for i in xrange(600):
  pt1 = points.InsertNextPoint(uniform(0, 100), uniform(0, 100), 0)
  pt2 = points.InsertNextPoint(uniform(0, 100), uniform(0, 100), 0)
  lines.InsertNextCell(2, [pt1, pt2])

# make random cell data
arr = nps.numpy_to_vtk(np.random.randn(600))
arr.SetName('foo')

output.SetPoints(points)
output.SetLines(lines)
output.GetCellData().AddArray(arr)

# Now set the active scalars
output.GetCellData().SetActiveScalars('foo')

Awesome this works! But this same workflow doesn’t work for every data type. For example, when trying to do to set the active scalars on vtkImageData, nothing happens:

Request Data:

import numpy as np
from vtk.util import numpy_support as nps

output.SetDimensions(10, 10, 10)

# make random cell data
arr = nps.numpy_to_vtk(np.random.randn(1000))
arr.SetName('foo')
output.GetCellData().AddArray(arr)

# Now set the active scalars
output.GetCellData().SetActiveScalars('foo')

Request Info:

# Set WHOLE_EXTENT: This is absolutely necessary
from paraview import util
util.SetOutputWholeExtent(self, (0,9,0,9,0,9))

I’m aware that this might have something to do with the fact that the ImageData will be displayed as an outline representation when using a Programmable Source but I am trying to use the active scalars feature in Python algorithm plugins where I can set the default display representation to surface. It essentially follows the same logic in the code… I have poly data sources working but not volumetric data sets like vtkImageData.

Any help on how to properly set the active scalars would be much appreciated!

2 Likes