Skip to content

Instantly share code, notes, and snippets.

@Roninkoi
Last active October 24, 2024 06:04
Show Gist options
  • Select an option

  • Save Roninkoi/305de5ce21ace0700abc8d77a98d909d to your computer and use it in GitHub Desktop.

Select an option

Save Roninkoi/305de5ce21ace0700abc8d77a98d909d to your computer and use it in GitHub Desktop.
Convert vtkUnstructuredGrid VTK file to Wavefront OBJ file or STL file
#!/bin/env python3
# Convert vtkUnstructuredGrid VTK file to Wavefront OBJ file or STL file
import sys
import os
import vtk
if len(sys.argv) != 3:
print('Usage: ', sys.argv[0], 'in.vtk out.obj')
sys.exit()
infile = sys.argv[1]
outfile = sys.argv[2]
outext = os.path.splitext(outfile)[1]
# read VTK file
reader = vtk.vtkUnstructuredGridReader()
reader.SetFileName(infile)
reader.Update()
obj = reader.GetOutput()
print(obj)
# convert unstructured mesh
geofilter = vtk.vtkGeometryFilter()
geofilter.SetInputData(obj)
geofilter.Update()
obj = geofilter.GetOutput()
print("Writing", outext)
if outext == ".obj": # write OBJ
writer = vtk.vtkOBJWriter()
if outext == ".stl": # write STL
writer = vtk.vtkSTLWriter()
writer.SetFileName(outfile)
writer.SetInputData(obj)
writer.Write()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment