# How to color a stl file using VTK in python

**URL:** <https://discourse.slicer.org/t/how-to-color-a-stl-file-using-vtk-in-python/26334>\
**Category:** Support\
**Tags:** python, stl\
**Created:** [November 20, 2022, 8:21pm UTC](https://discourse.slicer.org/t/how-to-color-a-stl-file-using-vtk-in-python/26334 "2022-11-20T20:21:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hamid\_Fsian](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hamid_fsian/32/17406_2.png) [@Hamid\_Fsian](https://discourse.slicer.org/u/Hamid_Fsian)\
**Post date:** [November 20, 2022, 8:21pm UTC](https://discourse.slicer.org/t/how-to-color-a-stl-file-using-vtk-in-python/26334/1 "2022-11-20T20:21:10Z")

</div>

Hello,

I am a newbie in VTK.

I have generated a STL file (of bones) using VTK, and now I want to have specific color labels for each slice, for example :

- if the width of slice(n) \> N (number) =\> green,

- if width of slice(n) \< M =\> red,

- if width of M\<slice(n)\<N =\> orange.

Is there’s a way to do it ?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 1, 2022, 6:40am UTC](https://discourse.slicer.org/t/how-to-color-a-stl-file-using-vtk-in-python/26334/2 "2022-12-01T06:40:32Z")

</div>

STL files cannot store color. You can use “Plane cut” tool in “Dynamic modeler” module to cut up a model with slice planes.

What is your overall goal?

---

<div class="post-metadata">

**Author:** ![Hamid\_Fsian](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hamid_fsian/32/17406_2.png) [@Hamid\_Fsian](https://discourse.slicer.org/u/Hamid_Fsian)\
**Post date:** [December 1, 2022, 7:59am UTC](https://discourse.slicer.org/t/how-to-color-a-stl-file-using-vtk-in-python/26334/3 "2022-12-01T07:59:07Z")

</div>

Thank’s for your answer.

I want to parse my 3D object by slices, and check for each point “x” if the width is superior to a certain treshold, if so =\> Color the corresponding slice “GREEN”. Else, color it “RED”.

What follow is my code. But I am coloring the x-axis and not the z-axis.

```python
import vtk, sys
import statistics
# Read in your STL file
f = vtk.vtkSTLReader()
f.SetFileName("airways_no_labels.stl")
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.v
# I believe that the bounds are (xmin, xmax, ymin, ymax, zmin, zmax).
obj = f.GetOutputDataObject(0)
min_x , max_x , min_y , max_y ,min_z, max_z = obj.GetBounds()

lut = vtk.vtkLookupTable()
lut.SetTableRange(min_z, max_z)
lut.Build()

heights = vtk.vtkDoubleArray()
heights.SetName("Z_Value")

Colors = vtk.vtkUnsignedCharArray()
Colors.SetNumberOfComponents(3)
Colors.SetNumberOfTuples(obj.GetNumberOfCells())

for i in range(obj.GetNumberOfPoints()):
    z = obj.GetPoint(i)[-1] # z axis
    x = obj.GetPoint(i)[0] # x axis

# This is just a try but won't work
    if min_x < x < max_x: 
        Colors.InsertVariantValue(255,z)
    if min_x < x < 300 :
        Colors.InsertVariantValue(155,z)
    if 300 < x < max_x :
        heights.InsertVariantValue(0,z)
#end of the "TRY"

    # Add this array to the point data as a scalar.
obj.GetPointData().SetScalars(Colors)

mapper = vtk.vtkPolyDataMapper()
mapper.SetInputDataObject(obj)
mapper.SetScalarRange(min_z, max_z)
mapper.SetLookupTable(lut)
mapper.SetUseLookupTableScalarRange(True)

actor = vtk.vtkActor()
actor.SetMapper(mapper)

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

renw = vtk.vtkRenderWindow()
renw.AddRenderer(renderer)

iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renw)

renw.Render()
iren.Start()

#Then save it as .vtk file

```

---

<div class="post-metadata">

**Author:** ![Jeevigyay\_Srivastava](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jeevigyay_srivastava/32/18081_2.png) [@Jeevigyay\_Srivastava](https://discourse.slicer.org/u/Jeevigyay_Srivastava)\
**Post date:** [January 20, 2023, 5:28am UTC](https://discourse.slicer.org/t/how-to-color-a-stl-file-using-vtk-in-python/26334/4 "2023-01-20T05:28:08Z")

</div>

Hello Sir,  
I am facing an issue. If you are comfortable with mitk I can proceed with my question.
