import vtk
filename = "test.vtk"
#reader
reader = vtk.vtkPolyDataReader()
reader.SetFileName(filename)
reader.Update()
grid = reader.GetOutput()
field = "mag(U)"
data_type = "cell"
magU = grid.GetPointData().GetArray("VelocityMagnitude")
array_name = "VelocityMagnitude"
array = magU
# lookup table
lut = vtk.vtkLookupTable()
lut.SetRange(array.GetRange())
lut.SetHueRange(0.667, 0)
lut.Build()
# mapper
mapper = vtk.vtkDataSetMapper()
mapper.SetInputConnection(reader.GetOutputPort())
mapper.SetScalarModeToUsePointData()
mapper.SelectColorArray(array_name)
mapper.SetColorModeToDirectScalars()
mapper.SetScalarRange(array.GetRange())
mapper.ScalarVisibilityOn()
mapper.SetLookupTable(lut)
# actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.SetOrientation(0,0,0)
# prop = actor.GetProperty()
# prop.SetAmbient(0.5)
# prop.EdgeVisibilityOn()
# prop.SetEdgeColor(0, 0, 0)
# prop.SetLineWidth(2)
# prop.SetRepresentationToSurface()
# renderer
ren = vtk.vtkRenderer()
ren.AddActor(actor)
ren.SetBackground(1, 1, 1)
ren_win = vtk.vtkRenderWindow()
ren_win.AddRenderer(ren)
ren_win.SetSize(640, 480)
# interactor
inter = vtk.vtkRenderWindowInteractor()
inter.SetRenderWindow(ren_win)
#inter.SetInteractorStyle(MouseInteractorStyle())
# scalar bar
scalar_bar = vtk.vtkScalarBarActor()
scalar_bar.SetOrientationToVertical()
scalar_bar.SetLookupTable(lut)
prop = vtk.vtkTextProperty()
prop.SetColor(0, 0, 0)
prop.SetFontSize(20)
scalar_bar.UnconstrainedFontSizeOn()
scalar_bar.SetTitleTextProperty(prop)
scalar_bar.SetLabelTextProperty(prop)
scalar_bar.SetTitle(array_name)
ren.AddActor(scalar_bar)
# text
text = vtk.vtkTextActor()
text.SetInput(array_name)
text.GetTextProperty().SetColor(0, 0, 0)
text.GetTextProperty().SetFontSize(24)
text.SetPosition(300, 30)
ren.AddActor(text)
ren_win.Render()
inter.Initialize()
inter.Start()