Hello!!!
I’m using VTK 9.2.6 and Python 3.11.6
I’m trying to display (orient, scale and color) arrows using a vector field and using the vtkGlyph3DMapper (instead of vktGlyph3D).
I have the orientation and scaling working… but I cannot seem to color the vectors by the magnitude of the vector that already using for orientatoin and scale…
Code to demonstrate the issue is shown below. The vector I’m using for orientatin scale and color is “Displacement”. I’m using the vtkGlyph3DMapper “SetScalarModeToUsePointFieldData()” and “SelectColorArray(‘Displacement’)” methods to identify the array to use for coloring and then I’m using the vtkLookupTable
“SetVectorModeToMagnitude()” and setting the range to match the vector values
All this displays all of the vectors in the same color (Red)…
Any ideas wil be greatfully received!!!
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# # noinspection PyUnresolvedReferences
from vtkmodules.vtkCommonDataModel import vtkPolyData
from vtkmodules.vtkCommonCore import vtkLookupTable, VTK_FLOAT
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkRenderer,
vtkGlyph3DMapper,
vtkInteractorStyle
)
from vtkmodules.vtkFiltersSources import vtkArrowSource
from vtkmodules.numpy_interface import dataset_adapter as dsa
from vtkmodules.util.numpy_support import numpy_to_vtk
import numpy as np
if __name__ == "__main__":
poly=vtkPolyData()
poly_np=dsa.WrapDataObject(poly)
poly_np.Points = np.array([[0,0,0],[1,0,0],[1,1,0],[0,0,1]])
disp_field = numpy_to_vtk(np.array([[1,0,0],[2,0,0],[3,0,0],[1,1,1]]), deep=False, array_type=VTK_FLOAT)
disp_field.SetNumberOfComponents(3)
disp_field.SetName('Displacement')
poly.GetPointData().AddArray(disp_field)
# Set up an arrow source.
arrowSource=vtkArrowSource()
arrowSource.SetShaftRadius(0.05)
arrowSource.SetTipLength(0.2)
arrowSource.SetTipRadius(0.1)
# Copy the arrow source to every point on the poly
glyphmapper=vtkGlyph3DMapper()
glyphmapper.SetSourceConnection(arrowSource.GetOutputPort())
glyphmapper.SetInputDataObject(poly)
glyphmapper.SetOrientationArray("Displacement")
glyphmapper.SetScaleModeToScaleByMagnitude()
glyphmapper.SetInputArrayToProcess(0, 0, 0, vtkPolyData.FIELD_ASSOCIATION_POINTS, 'Displacement')
glyphmapper.SetScalarModeToUsePointFieldData()
glyphmapper.SelectColorArray('Displacement')
lut=vtkLookupTable()
lut.SetTableRange (0, 1)
lut.SetHueRange (0.667, 1.0)
lut.SetSaturationRange (1, 1)
lut.SetVectorModeToMagnitude()
lut.SetValueRange (1.0, 3.)
lut.Build()
glyphmapper.SetLookupTable(lut)
# Set up an actor for the arrow glyphs
glyph_actor = vtkActor()
glyph_actor.SetMapper(glyphmapper)
# Render window, renderer and interactor
renderer=vtkRenderer()
renwin=vtkRenderWindow()
renwin.AddRenderer(renderer)
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(renwin)
# Add an InteractorStyle
style=vtkInteractorStyle()
style.SetDefaultRenderer(renderer)
renderer.AddActor(glyph_actor)
# Render
iren.Initialize()
renwin.Render()
iren.Start()