Created
February 14, 2024 15:06
-
-
Save MarilynKeller/963a9488993329c28bf477e54f547851 to your computer and use it in GitHub Desktop.
Convert a folder of .ply meshes to a folder of .vtp meshes that can be loaded by OpenSim
This file contains 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
import argparse | |
import os | |
# import pyvista | |
# import meshio | |
import vtk | |
from pyvista import examples | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Convert a folder of vtp files to a folder of ply files') | |
parser.add_argument('src_folder', help='folder containing the vtp files to convert', default="/home/kellerm/Dropbox/MPI/TML/Fullbody_TLModels_v2.0_OS4x/Geometry/", type=str) | |
parser.add_argument('dst_folder', help='folder to save the ply files', default=None, type=str) | |
args = parser.parse_args() | |
src = args.src_folder | |
if src[-1] != "/": | |
src += "/" | |
if args.dst_folder is None: | |
target = src + "../Geometry_vtp/" | |
else: | |
target = args.dst_folder | |
if target[-1] != "/": | |
target += "/" | |
os.makedirs(target, exist_ok=True) | |
# for each file in src | |
for filename in os.listdir(src): | |
ext = os.path.splitext(filename)[-1] | |
if ext not in [".ply"]: | |
print("Skipping " + filename) | |
continue | |
ply_reader = vtk.vtkPLYReader() | |
ply_reader.SetFileName(src + filename) | |
# Calculate normals | |
normals = vtk.vtkPolyDataNormals() | |
normals.SetInputConnection(ply_reader.GetOutputPort()) | |
normals.ComputePointNormalsOn() | |
normals.ComputeCellNormalsOff() | |
normals.SplittingOff() | |
normals.ConsistencyOff() | |
new_filename = filename.replace(".ply", ".vtp") | |
new_filename = new_filename.replace(".vtp.vtp", ".vtp") | |
out_name = target + new_filename | |
# Write the VTP file | |
vtp_writer = vtk.vtkXMLPolyDataWriter() | |
vtp_writer.SetInputConnection(normals.GetOutputPort()) | |
vtp_writer.SetFileName(out_name) | |
vtp_writer.SetDataModeToAscii() | |
vtp_writer.Write() | |
print("Converted mesh: " + out_name) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment