Search code examples
pythoncsvvtkparaviewrgbcolor

Get value of a vtkintarray by the field name


I have a Paraview programmable filter written in python, that I am running on a table of points to assign RGB colors as UnsignedCharArray. I'm just stuck in one part of the code to get the value of R, G, B fields in the range. Here is the table example:

xyz table

and here is the sample code:

ids = self.GetInput()
ods = self.GetOutput()

ocolors = vtk.vtkUnsignedCharArray()
ocolors.SetName("colors")
ocolors.SetNumberOfComponents(3)
ocolors.SetNumberOfTuples(ids.GetNumberOfPoints())

inArray = ids.GetPointData().GetArray(0)
for x in range(0, ids.GetNumberOfPoints()):
  rF = inArray.GetValue(x) # here I need something like GetValue(x, "R")
  gF = inArray.GetValue(x) # here I need something like GetValue(x, "G")
  bF = inArray.GetValue(x) # here I need something like GetValue(x, "B")

  ocolors.SetTuple3(x, rF,gF,bF)

ods.GetPointData().AddArray(ocolors)

Does anyone know how I can handle this?


Solution

  • So here is the correct way to do it:

    ids = self.GetInput()
    ods = self.GetOutput()
    
    ocolors = vtk.vtkUnsignedCharArray()
    ocolors.SetName("colors")
    ocolors.SetNumberOfComponents(3)
    ocolors.SetNumberOfTuples(ids.GetNumberOfPoints())
    
    inArray = ids.GetPointData().GetArray(0)
    
    r = ids.GetPointData().GetArray("R")
    g = ids.GetPointData().GetArray("G")
    b = ids.GetPointData().GetArray("B")
    for x in range(0, ids.GetNumberOfPoints()):
      rF = r.GetValue(x) 
      gF = g.GetValue(x) 
      bF = b.GetValue(x) 
    
      # if rgb are between 0-1
      #rC = rF*256
      #gC = gF*256
      #bC = bF*256
    
      ocolors.SetTuple3(x, rF,gF,bF)
    
    ods.GetPointData().AddArray(ocolors)