Skip to content

Instantly share code, notes, and snippets.

@alvinwan
Last active December 7, 2022 07:28
Show Gist options
  • Save alvinwan/06c875531dc57fdd5dce6cf56ed8cbc3 to your computer and use it in GitHub Desktop.
Save alvinwan/06c875531dc57fdd5dce6cf56ed8cbc3 to your computer and use it in GitHub Desktop.
Converts mesh into a point cloud - accepts .pkl,.off,.xaml,.obj etc.
docopt==0.6.2
trimesh==2.10.18
"""Converts a mesh into point cloud.
This file, as a script, will convert a mesh binary into .npy. It is written for
python3, but if the output directory exists, then remove L28 to use python2.
Usage:
topc.py <input_path> [options]
Options:
--n=<n> Number of points to sample [default: 3000]
--out=<out> Root of output directory [default: ./out]
--scale=<r> Largest distance from origin, to scale to [default: 250]
"""
import docopt
import trimesh
import os
import numpy as np
import glob
from trimesh.sample import sample_surface
def main():
"""Passes command line arguments into utility function."""
arguments = docopt.docopt(__doc__)
input_path = arguments['<input_path>']
output_dir = arguments['--out']
scale = float(arguments['--scale'])
n = int(arguments['--n'])
os.makedirs(output_dir, exist_ok=True)
for path in glob.iglob(input_path):
mesh = trimesh.load(path)
mesh.apply_scale(scale/mesh.scale)
points = sample_surface(mesh, n)
points -= points.mean(axis=0)
output_path = os.path.join(output_dir, os.path.basename(path))
np.save(output_path, points)
if __name__ == '__main__':
main()
@BeenToJupiter
Copy link

Hello, can you please tell me if this will convert .pkl files to .pcd? If not, please guide me. Thank you.

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