How to export ParaView colormap into a format that could be read by matplotlib?

I’m wondering is it possible to export ParaView colormap in RGB format to be able to read it in matplotlib? I know that ParaView could export colormaps in .json format but I don’t know how to convert it in a suitable format to be able to read it by using matplotlib.colors.ListedColormap function. Any idea or help is appreciated.

Sure, here is a little function that can export the color map in a table:

def ExportPreset(lutName):
  from paraview import servermanager
  presets = paraview.servermanager.vtkSMTransferFunctionPresets()

  lut = GetColorTransferFunction('dummy')
  lut.ApplyPreset(lutName)

  import vtk
  helper = vtk.vtkUnsignedCharArray()

  dctf = lut.GetClientSideObject()
  dataRange = dctf.GetRange()

  for i in range(0, 256):
    x = (i/255.0) * (dataRange[1]-dataRange[0]) + dataRange[0]
    helper.SetVoidArray(dctf.MapValue(x), 3, 1)
    r = helper.GetValue(0)
    g = helper.GetValue(1)
    b = helper.GetValue(2)
    print("%d %d %d" % (r, g, b))

lutName is a string with the name of the colormap you’d like to export.

Evaluate and call this function in the Python Shell (View -> Python Shell). You can adapt the script to produce the type of array needed for the matplotlib function.

2 Likes

Thanks Cory, it worked smoothly!

I’m trying to export the opacities but have some trouble, they always return as fully opaque for me. Came across this old response. Should this also return the opacities in the MapValue() function? Or do I need to call the GetOpacityTransferFunction() and do similar operations in a second loop? Thx