Last active
October 24, 2024 06:04
-
-
Save Roninkoi/305de5ce21ace0700abc8d77a98d909d to your computer and use it in GitHub Desktop.
Convert vtkUnstructuredGrid VTK file to Wavefront OBJ file or STL file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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