vtkUnstructedGrid scalar color does not match when SetInterpolateScalarsBeforeMapping(1) and SetInterpolateScalarsBeforeMapping(0)
here is the test code, this vtu file displays right in paraview like this.
#Create a mapper and actor
mapper = vtkDataSetMapper()
mapper.SetLookupTable(lookup_table);
mapper.SetInputData(mesh)
mapper.ColorByArrayComponent(“Vorticity”,2)
mapper.SetScalarRange([-0.5,0.5])
mapper.SetScalarModeToUsePointFieldData() #mapper.SetInterpolateScalarsBeforeMapping(1)
mapper.InterpolateScalarsBeforeMappingOn()
mapper.Update()
#Check the scalar range of “Vorticity”
point_data = mesh.GetPointData()
vorticity_array = point_data.GetArray(“Vorticity”)
print(f"{point_data.GetArray(‘Vorticity’)}“)
min_value = vorticity_array.GetRange(2)[0]
max_value = vorticity_array.GetRange(2)[1]
print(f"Vorticity Scalar Range: {min_value} to {max_value}”)
actor = vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetRepresentationToSurface()
#Create a renderer and set up a background color
renderer = vtkRenderer()
renderer.AddActor(actor)
renderer.SetBackground(1.0, 1.0, 1.0) # Set background to white
#Create a render window
render_window = vtkRenderWindow() #render_window.SetOffScreenRendering(1)
render_window.AddRenderer(renderer)
render_window.SetSize(1080, 1080)
camera = renderer.GetActiveCamera()
camera.SetPosition(0, 0, 30) # Position of the camera
camera.SetFocalPoint(0, 0, 0) # Focus on the center of the object
You almost got it right. Ask the mapper to use the scalar range already given to the lookup table instead of using the mapper’s default ScalarRange which is [0,1]. This is why everything is navy blue in your screenshot.
I have a MESH.DAT file which I read using Tecplot reader. My dataset (.B file) contain arrays which are read through a Fortran code wrapped in Python library. I add those arrays to my mesh block and then create a rendered scene. I have also attached the code and library I am using.
Could you instead send a .vtu file please? .so shared libs are not easily redistributable, it won’t work on my machine.
Instead, can you modify your postprocess_2D.py to generate a .vtu file? add these lines after you get output of gradient filter and share the mesh.vtu file here.
# Extract the gradients of u and v from the gradient filter output
mesh = gradient_filter.GetOutput()
writer = vtkXMLUnstructuredGridWriter()
writer.SetInputData(mesh)
writer.SetFileName("mesh.vtu")
writer.Update()
import vtk
reader = vtk.vtkXMLUnstructuredGridReader(file_name="./output.vtu")
#Create a lookup table
lut = vtk.vtkLookupTable(range=(-0.5, 0.5), hue_range=(0.6667, 0.0))
lut.Build()
mapper = vtk.vtkDataSetMapper(
interpolate_scalars_before_mapping=True,
lookup_table=lut,
scalar_mode=vtk.VTK_SCALAR_MODE_USE_POINT_FIELD_DATA,
use_lookup_table_scalar_range=True,)
mapper.ColorByArrayComponent("Vorticity", 2)
mapper.SetScalarModeToUsePointFieldData()
# create a connection from reader's output to mapper's input
reader >> mapper
actor = vtk.vtkActor(mapper=mapper)
#Create a renderer and set up a background color
renderer = vtk.vtkRenderer(background=(1, 1, 1))
renderer.AddActor(actor)
camera = renderer.GetActiveCamera()
camera.SetPosition(0, 0, 30) # Position of the camera
camera.SetFocalPoint(0, 0, 0) # Focus on the center of the object
#Create a render window
window = vtk.vtkRenderWindow(size=(1080, 1080))
window.AddRenderer(renderer)
# Ensure lookup table uses the correct vector component of "Vorticity"
lut.SetVectorComponent(2)
interactor = vtk.vtkRenderWindowInteractor(render_window=window)
interactor.Start()