Skip to content

Instantly share code, notes, and snippets.

@thomasballinger
Created October 12, 2011 15:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thomasballinger/1281457 to your computer and use it in GitHub Desktop.
Save thomasballinger/1281457 to your computer and use it in GitHub Desktop.
vtk2vtp.py
#!/usr/bin/env python
"""File format conversion
category: vtk, file conversion, tomb"""
import os, sys
import vtk
def vtk2vtp(invtkfile, outvtpfile, binary=False):
"""What it says on the label"""
reader = vtk.vtkPolyDataReader()
reader.SetFileName(invtkfile)
writer = vtk.vtkXMLPolyDataWriter()
writer.SetFileName(outvtpfile)
if binary:
writer.SetFileTypeToBinary()
writer.SetInput(reader.GetOutput())
writer.Update()
if __name__ == '__main__':
args = sys.argv
binary = False
if '-b' in args:
args.remove('-b')
binary = True
if len(args) < 2:
print('Batch converts vtk files to vtp files.\nUsage:\n vtk2vtp.py model1.vtk model2.vtk ...')
print(' [-b] causes output to be in binary format, much smaller vtp file size, if it happens to work')
sys.exit()
infiles = args[1:]
for vtkfile in infiles:
if vtkfile[-4:] != '.vtk':
print vtkfile, "doesn't look like a vtk file, won't convert"
continue
vtk2vtp(vtkfile, vtkfile[:-4]+'.vtp', binary=binary)
@skyhq12
Copy link

skyhq12 commented Feb 26, 2021

add 12 lines code , reader.Update() can work well

@HvSchoening
Copy link

I second the comment of skyhq12.
Add
"reader.Update()"
in line 12.
Else, the vtkXMLPolyDataWriter writes an empty .vtp-file.

Best,
Hendrik

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