Hi, I am new to VTK and I try to convert my .vtk file generate from OpenFOAM into an image. But my current code cannot generate colorful pressure field although I set up the lookup table and scalar bar. The generated image is white except for the background area. Could anyone help me to solve this question? Thanks!
My source .vtk file has the following format and I cannot upload attachment as a new comer…
# vtk DataFile Version 2.0
sampleSurface
ASCII
DATASET POLYDATA
POINTS 55181 double
0.312848 -0.389703 0
-0.319252 -0.384475 0
0.246285 -0.434844 0
-0.312848 0.389709 0
...
POLYGONS 109010 436040
3 54060 54066 54200
3 54206 54200 54066
3 54204 54065 54200
3 54060 54200 54065
3 53921 53936 54066
...
POINT_DATA 55181
FIELD attributes 1
p 1 55181 float
-0.622929 -0.202063 -0.860382 -0.293298 -0.721486 -0.619987 -1.07553 -0.596293 -0.522474 -0.911477
...
This is my code.
import vtk
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from scipy.interpolate import griddata
from vtk.util.numpy_support import vtk_to_numpy
filePath = '/p_zNormal.vtk'
print(filePath)
reader = vtk.vtkPolyDataReader()
reader.SetFileName(filePath)
reader.ReadAllScalarsOn()
reader.ReadAllColorScalarsOn()
reader.ReadAllNormalsOn()
reader.ReadAllTCoordsOn()
reader.ReadAllVectorsOn()
reader.Update()
vtkdata = reader.GetOutput()
scalar_range = vtkdata.GetScalarRange()
points = vtkdata.GetPoints()
np_coordinates = vtk_to_numpy(points.GetData())
pointData = vtkdata.GetPointData()
fieldData_vtk_array = pointData.GetArray(0)
fieldData_np_array = np.array(vtk_to_numpy(fieldData_vtk_array))
DataMin = np.min(fieldData_np_array)
DataMax = np.max(fieldData_np_array)
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(reader.GetOutputPort())
mapper.SetScalarRange(DataMin, DataMax)
mapper.SetDebug(True)
mapper.SetArrayName('H2O')
mapper.ScalarVisibilityOn()
mapper.SetInterpolateScalarsBeforeMapping(1)
mapper.SetScalarModeToUsePointFieldData()
mapper.SelectColorArray("H2O")
mapper.Update()
actor = vtk.vtkActor()
actor.SetMapper(mapper)
pColorTable = vtk.vtkLookupTable()
pColorTable.SetNumberOfTableValues(31);
pColorTable.SetHueRange(0.67, 0);
pColorTable.SetAlphaRange(1.0, 1.0);
pColorTable.SetValueRange(1, 1);
pColorTable.SetSaturationRange(1, 1);
pColorTable.SetRange(DataMin, DataMax);
pColorTable.Build();
mapper.SetLookupTable(pColorTable);
scalarBar = vtk.vtkScalarBarActor()
scalarBar.SetTitle("p (Pa)");
scalarBar.GetTitleTextProperty().SetColor(0, 0, 0);
scalarBar.GetTitleTextProperty().SetFontFamilyToArial();
scalarBar.GetTitleTextProperty().SetFontSize(20);
scalarBar.GetLabelTextProperty().SetColor(0, 0, 0);
scalarBar.SetLabelFormat("%5.3f");
scalarBar.GetLabelTextProperty().SetFontFamilyToArial();
scalarBar.GetLabelTextProperty().SetFontSize(20);
scalarBar.SetNumberOfLabels(7);
scalarBar.SetUnconstrainedFontSize(1);
scalarBar.SetLookupTable(pColorTable);
ren = vtk.vtkRenderer()
ren.AddActor(actor)
ren.AddActor(scalarBar);
ren.SetBackground(0.2, 0.3, 0.4) # envy background
renwin = vtk.vtkRenderWindow()
renwin.AddRenderer(ren)
renwin.SetWindowName("Test")
renwin.SetSize(800,600)
renwin.Render()
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renwin)
iren.Initialize()
iren.Start()