Created
October 3, 2024 14:33
-
-
Save SebastianBitsch/bf1d4df30ae6ee3ca3d2d274e57e7e57 to your computer and use it in GitHub Desktop.
Convert CAD-model to point cloud (`.stl` to `.ply`)
This file contains hidden or 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 open3d as o3d | |
# Convert .stl models to .ply point clouds | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='Process some integers.') | |
parser.add_argument('--file', type=str, help='Path of the file to convert, output will be in the same dir named *.ply') | |
parser.add_argument('--n_points', type=int, default=10000, help='How many points the ply file should contain') | |
parser.add_argument('--draw_output', type=bool, default=False, help='Should the resulting file be shown in plot') | |
parser.add_argument('--write_ascii', type=bool, default=True, help='Whether to write the file as plain text file or in binary') | |
args = parser.parse_args() | |
assert args.file.endswith(".stl"), f"Error: Unsupported file, file should be a `.stl` file, got {args.file}" | |
print("Parsing file ... ") | |
mesh = o3d.io.read_triangle_mesh(args.file) | |
pointcloud = mesh.sample_points_poisson_disk(args.n_points) | |
if args.draw_output: | |
o3d.visualization.draw_geometries([mesh]) # Draw input | |
o3d.visualization.draw_geometries([pointcloud]) # Draw output | |
file_out = args.file.replace(".stl", ".ply") | |
o3d.io.write_point_cloud( | |
file_out, | |
pointcloud, | |
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