Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

HvSchoening commented Mar 23, 2021

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