Skip to content

Instantly share code, notes, and snippets.

@SebastianBitsch
Created March 25, 2025 08:47
Show Gist options
  • Save SebastianBitsch/c8e191804d9f332718f2fdb156c29f61 to your computer and use it in GitHub Desktop.
Save SebastianBitsch/c8e191804d9f332718f2fdb156c29f61 to your computer and use it in GitHub Desktop.
Convert a mesh to a .ply file
import argparse
import open3d as o3d
# Convert mesh file to .ply point clouds
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--input_path', type=str, help='Path of the file to convert, output will be in the same dir if no output is given')
parser.add_argument('--ouput_path', type=str, help='Path of where to save the file')
parser.add_argument('--n_points', type=int, default=100_000, help='How many points the ply file should contain')
parser.add_argument('--draw_output', type=bool, default=True, help='Should the resulting file be shown in plot')
parser.add_argument('--write_ascii', type=bool, default=False, help='Whether to write the file as plain text file or in binary')
args = parser.parse_args()
print("Parsing file ... ")
try:
mesh = o3d.io.read_triangle_mesh(args.input_path)
except Exception as e:
print(f"Error: expected input to a triangle mesh: {e}")
pcd = mesh.sample_points_uniformly(args.n_points)
if args.draw_output:
o3d.visualization.draw_geometries([mesh]) # Draw input
o3d.visualization.draw_geometries([pcd]) # Draw output
file_out = args.ouput_path or args.input_path.replace(".ply", f"_downsampled_{args.n_points}.ply")
o3d.io.write_point_cloud(
file_out,
pcd,
format = "auto",
write_ascii = args.write_ascii,
compressed = False,
print_progress = True
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment