[vtkusers] Color mapping on stl file

Meehan, Bernard MEEHANBT at nv.doe.gov
Mon Jul 13 12:50:23 EDT 2015

There are a few examples on the Wiki page that could be contorted into what you were looking for, some of it is not very straightforward for beginners with VTK though. The following is a python script that could be run from the command line with “python display_stl_with_color.py your_stl_file.stl” It isn’t hard to convert this to C if that is what you were working in.

Here starts the script:

# A lot of this was taken from the example on the Wiki:
# http://www.vtk.org/Wiki/VTK/Examples/Cxx/Meshes/ColoredElevationMap

import vtk, sys

# Read in your STL file
f = vtk.vtkSTLReader()
f.Update() # This is necessary to have the data ready to read.

# The vtkSTLReader reads in your file as a vtkPolyData, 'obj' is a reference to
# that output. I'm using the bounds that are automatically calculated during
# the import phase to give a range for the height of the points in the file.
# I believe that the bounds are (xmin, xmax, ymin, ymax, zmin, zmax).
obj = f.GetOutputDataObject(0)
min_z, max_z = obj.GetBounds()[4:]

# I am creating a lookup table to correspond to the height field. I am using
# the default values. Remember that the lookup table is a rather complex and
# handy object, and there are lots of options to set if you need something
# special.
lut = vtk.vtkLookupTable()
lut.SetTableRange(min_z, max_z)

# This is an array that I am creating to store the heights of the points. I
# will use this as a scalar field on the 'obj' so that the lookup table can be
# used to color it. You could obviously make the array anything you wanted,
# such as ‘x’ or ‘y’ or squared distance from some other point, for instance.
heights = vtk.vtkDoubleArray()

# Loop through the points in the vtkPolyData and record the height in the
# 'heights' array.
for i in range(obj.GetNumberOfPoints()):
z = obj.GetPoint(i)[-1]

# Add this array to the point data as a scalar.

# Visualization stuff ... you need to tell the mapper about the scalar field
# and the lookup table. The rest of this is pretty standard stuff.
mapper = vtk.vtkPolyDataMapper()
mapper.SetScalarRange(min_z, max_z)

actor = vtk.vtkActor()

renderer = vtk.vtkRenderer()
renderer.SetBackground(.1, .2, .4)

renw = vtk.vtkRenderWindow()

iren = vtk.vtkRenderWindowInteractor()


From: vtkusers <vtkusers-bounces at vtk.org<mailto:vtkusers-bounces at vtk.org>> on behalf of Anusha Balasubramoniam <anushaba at buffalo.edu<mailto:anushaba at buffalo.edu>>
Date: Monday, July 13, 2015 at 7:17 AM
To: "vtkusers at vtk.org<mailto:vtkusers at vtk.org>" <vtkusers at vtk.org<mailto:vtkusers at vtk.org>>
Subject: [vtkusers] Color mapping on stl file

Dear All,

I am not able to get a color map on an stl file.
Please, kindly help.
I am not able to figure out how to do it.

Thank you,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20150713/bb90c44e/attachment.html>

More information about the vtkusers mailing list