Skip to content

Instantly share code, notes, and snippets.

@1f0
Created September 29, 2018 06:01
Show Gist options
  • Save 1f0/a7a3cecdd3df747f525888189195f7d4 to your computer and use it in GitHub Desktop.
Save 1f0/a7a3cecdd3df747f525888189195f7d4 to your computer and use it in GitHub Desktop.
reduce vertex number of obj file
from __future__ import print_function
import sys
import vtk
if(len(sys.argv) < 3):
print('Usage: ', sys.argv[0], 'input.obj', 'output.obj')
reader = vtk.vtkOBJReader()
reader.SetFileName(sys.argv[1])
reader.Update()
obj = reader.GetOutput()
decimate = vtk.vtkDecimatePro()
decimate.SetInputData(obj)
decimate.SetTargetReduction(0.9)
decimate.PreserveTopologyOn()
decimate.Update()
output = decimate.GetOutput()
out_obj_file = sys.argv[2]
with open(out_obj_file, 'w') as f:
for i in range(output.GetNumberOfPoints()):
pt = output.GetPoint(i)
f.write('v %.5f %.5f %.5f\n' % (pt[0], pt[1], pt[2]))
for i in range(output.GetNumberOfCells()):
cell = output.GetCell(i)
f.write('f %d %d %d\n'% (cell.GetPointId(0)+1, cell.GetPointId(1)+1, cell.GetPointId(2)+1) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment