Skip to content

Instantly share code, notes, and snippets.

@thewtex
Created January 5, 2014 01:28
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save thewtex/8263132 to your computer and use it in GitHub Desktop.
Save thewtex/8263132 to your computer and use it in GitHub Desktop.
Convert a .vtk UnstructuredGrid file to .stl file.
#!/usr/bin/env python
"""Convert UnstructuredGrid in .vtk files to STL files."""
import sys
import vtk
if len(sys.argv) < 2 or sys.argv[1] == '-h' or sys.argv[1] == '--help':
print('Usage: vtk-unstructuredgrid-to-stl.py <input.vtk> <output.stl>')
sys.exit(1)
reader = vtk.vtkUnstructuredGridReader()
reader.SetFileName(sys.argv[1])
surface_filter = vtk.vtkDataSetSurfaceFilter()
surface_filter.SetInputConnection(reader.GetOutputPort())
triangle_filter = vtk.vtkTriangleFilter()
triangle_filter.SetInputConnection(surface_filter.GetOutputPort())
writer = vtk.vtkSTLWriter()
writer.SetFileName(sys.argv[2])
writer.SetInputConnection(triangle_filter.GetOutputPort())
writer.Write()
@nschloe
Copy link

nschloe commented Mar 8, 2019

Or you use meshio. 😄

meshio-convert in.vtk out.stl

No need for libvtk either.

@bragostin
Copy link

This script works well, thank you!
@nschloe unfortunately meshio-convert seems not to work for this purpose and produces the following error message
AssertionError: STL can only write triangle cells.

@nschloe
Copy link

nschloe commented Nov 17, 2019

This is fixed now in meshio 3.2.12.

@bragostin
Copy link

@nschloe I had to replace logging.warning("STL can only write triangle cells. Discarding {}.".format(", ".join(keys))) with logging.warning("STL can only write triangle cells. Discarding {}.") otherwise I got the error message TypeError: can only join an iterable

Now it works, thank you! I could visualize the pygalmesh examples, that's a pretty awesome work, thank you for creating it!

@nschloe
Copy link

nschloe commented Nov 17, 2019

@bragostin Ah yes, fixed that now. Will be part of the next release.

@Gost65
Copy link

Gost65 commented Apr 12, 2021

It's not fixed

@nschloe
Copy link

nschloe commented Apr 12, 2021

@Gost65
Copy link

Gost65 commented Apr 12, 2021

@nschloe I am trying to convert a vtu to stl but i have a problem
import meshio

infile='Eprouvette_15_Displacement000000.vtu'
outfile = 'test.stl'

meshio._cli.convert(
[infile, outfile, "--input-format", "vtu-ascii", "--output-format", "stl-ascii"]
)
and I get this message:
SystemExit: 2
/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py:2890: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)``

@nschloe
Copy link

nschloe commented Apr 12, 2021

It's impossible to debug this from the just the message. If you find something doesn't work, best file a bug in meshio and attach an MWE.

@GRASBOCK
Copy link

Nowadays it is meshio convert in.vtk out.stl

@SiriusFuenmayor
Copy link

I am using the command to convert vtk to stl, but I am getting the error:

STL can only write triangle cells. No triangle cells found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment