<div dir="ltr"><div>I am not fluent with python, </div><div>but should not you replace</div><div> aMeshGrid.GetPointData().GetScalars()</div><div>with something like </div><div>aMeshGrid.GetPointData().SetScalars(stress)</div><div>?</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Aug 3, 2015 at 2:15 PM, roobaru <span dir="ltr"><<a href="mailto:chpradeep.ith@gmail.com" target="_blank">chpradeep.ith@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
I am trying to display stress values on an FEM mesh as color grade using<br>
vtkColorTransferFunction. I am reading the nodal stress values from a file.<br>
I also added a scale widget on the right. The widget shows the colors<br>
correctly but the mesh doesn't display any graded color. I tried to achieve<br>
this using a single hexahedron mesh and it was displaying quite well. But<br>
the fileinput method somehow doesn't do anything. If anybody can see a<br>
glaring mistake and point it out, it will be a great help. I am attaching<br>
the code and the input file. Thanks for any help.<br>
<br>
<br>
#!/usr/bin/env python<br>
# This is a function, which takes a string parameter: file_path. This string<br>
is<br>
# the path of the geometry txt file from which vtk will grab and display<br>
data.<br>
import vtk<br>
def displayMesh(file_path):<br>
import vtk<br>
from vtk import vtkHexahedron<br>
VTK_HEXAHEDRON = vtkHexahedron().GetCellType()<br>
with open(file_path, 'r') as f:<br>
aMeshGrid = vtk.vtkUnstructuredGrid()<br>
aMeshGrid.Allocate(1, 1)<br>
# Get number of mesh points<br>
no_points = int(f.readline())<br>
print no_points<br>
# Set number of points<br>
meshPoints = vtk.vtkPoints()<br>
meshPoints.SetNumberOfPoints(no_points)<br>
# Iterate through point data<br>
for i in range(no_points):<br>
#print i<br>
# Get coord info for each point<br>
point_info = f.readline().split() # I need to split, before I<br>
assign to point_coord<br>
# else the whole thing is split into single numbers<br>
point_ID = (int(point_info[0])-1) # -1 because the IDs need to<br>
start with 0.<br>
point_x = float(point_info[1])<br>
point_y = float(point_info[2])<br>
point_z = float(point_info[3])<br>
# Set coord info in mesh<br>
meshPoints.InsertPoint(point_ID, point_x, point_y, point_z)<br>
# Get number of elements<br>
no_elements = int(f.readline())<br>
# Set number of elements<br>
for i in range(no_elements):<br>
element_info = f.readline().split()<br>
element_ID = (int(element_info[0])-1)<br>
element_ID_list = vtk.vtkIdList()<br>
for j in range(8):<br>
node_no = int(element_info[j+1])<br>
element_ID_list.InsertNextId(node_no -1)<br>
#print j, node_no<br>
cell_type = VTK_HEXAHEDRON<br>
aMeshGrid.InsertNextCell(cell_type, element_ID_list)<br>
stress = vtk.vtkFloatArray()<br>
for i in range(no_points):<br>
stress_value = f.readline()<br>
stress.InsertNextValue(float(stress_value))<br>
aMeshGrid.GetPointData().GetScalars()<br>
aMeshMapper = vtk.vtkDataSetMapper()<br>
<br>
# # colorTransferFunction<br>
min=0.00001<br>
max=0.035<br>
avg=(min+max)/2.0<br>
aMeshMapper.SetInputData(aMeshGrid)<br>
colorTransferFunction = vtk.vtkColorTransferFunction()<br>
colorTransferFunction.AddRGBPoint(min, 0.0, 0.0, 1.0)<br>
colorTransferFunction.AddRGBPoint(avg, 1.0, 1.0, 0.0)<br>
colorTransferFunction.AddRGBPoint(max, 1.0, 0.0, 0.0)<br>
aMeshMapper.SetLookupTable(colorTransferFunction)<br>
<br>
scalar_bar = vtk.vtkScalarBarActor()<br>
scalar_bar.SetOrientationToHorizontal()<br>
scalar_bar.SetLookupTable(colorTransferFunction)<br>
scalar_bar.SetTitle("Stress")<br>
scalar_bar.GetTitleTextProperty().SetColor(1.0,1.0,1.0)<br>
<br>
aMeshGrid.SetPoints(meshPoints)<br>
aMeshActor = vtk.vtkActor()<br>
aMeshActor.AddPosition(0,0,0)<br>
aMeshActor.SetMapper(aMeshMapper)<br>
aMeshActor.GetProperty().SetDiffuseColor(1, 1, 0)<br>
aMeshActor.GetProperty().SetEdgeVisibility(1)<br>
aMeshActor.GetProperty().SetEdgeColor(1, 0, 0)<br>
<br>
# Create the usual rendering stuff.<br>
ren = vtk.vtkRenderer()<br>
renWin = vtk.vtkRenderWindow()<br>
renWin.AddRenderer(ren)<br>
renWin.SetSize(1920, 1080)<br>
iren = vtk.vtkRenderWindowInteractor()<br>
iren.SetRenderWindow(renWin)<br>
<br>
# create the scalar_bar_widget<br>
scalar_bar_widget = vtk.vtkScalarBarWidget()<br>
scalar_bar_widget.SetInteractor(iren)<br>
scalar_bar_widget.SetScalarBarActor(scalar_bar)<br>
scalar_bar_widget.On()<br>
<br>
# CompassWidget<br>
compassRepresentation = vtk.vtkCompassRepresentation()<br>
compassWidget = vtk.vtkCompassWidget()<br>
compassWidget.SetInteractor(iren)<br>
compassWidget.SetRepresentation(compassRepresentation)<br>
<br>
ren.AddActor(aMeshActor)<br>
ren.SetBackground(0.323,0.341,0.431)<br>
# Add Axes<br>
transform = vtk.vtkTransform()<br>
transform.Translate(0.0, 0.0, 0.0)<br>
axes = vtk.vtkAxesActor()<br>
# The axes are positioned with a user transform<br>
axes.SetUserTransform(transform)<br>
ren.AddActor(axes)<br>
ren.GetActiveCamera().SetPosition(-0.6,-0.6,-0.5)<br>
ren.ResetCamera()<br>
renWin.Render()<br>
compassWidget.EnabledOn()<br>
style = vtk.vtkInteractorStyleTrackballCamera()<br>
iren.SetInteractorStyle(style)<br>
## Render the scene and start interaction.<br>
iren.Initialize()<br>
iren.Start()<br>
displayMesh('D:\VTK_Visualization\Beam_colorwidget.txt')<br>
<br>
file:<br>
<a href="https://drive.google.com/file/d/0B1TsQHxIsXV8NWRNNnRBTE5BaWs/view?usp=sharing" target="_blank" rel="noreferrer">https://drive.google.com/file/d/0B1TsQHxIsXV8NWRNNnRBTE5BaWs/view?usp=sharing</a><br>
<br>
<br>
<br>
<br>
--<br>
View this message in context: <a href="http://vtk.1045678.n5.nabble.com/Mapping-nodal-scalar-values-to-graded-color-tp5733238.html" target="_blank" rel="noreferrer">http://vtk.1045678.n5.nabble.com/Mapping-nodal-scalar-values-to-graded-color-tp5733238.html</a><br>
Sent from the VTK - Users mailing list archive at Nabble.com.<br>
_______________________________________________<br>
Powered by <a href="http://www.kitware.com" target="_blank" rel="noreferrer">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" target="_blank" rel="noreferrer">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Please keep messages on-topic and check the VTK FAQ at: <a href="http://www.vtk.org/Wiki/VTK_FAQ" target="_blank" rel="noreferrer">http://www.vtk.org/Wiki/VTK_FAQ</a><br>
<br>
Search the list archives at: <a href="http://markmail.org/search/?q=vtkusers" target="_blank" rel="noreferrer">http://markmail.org/search/?q=vtkusers</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://public.kitware.com/mailman/listinfo/vtkusers" target="_blank" rel="noreferrer">http://public.kitware.com/mailman/listinfo/vtkusers</a><br>
</blockquote></div><br></div>