Skip to content

Instantly share code, notes, and snippets.

@tovacinni
Last active October 26, 2021 20:59
Show Gist options
  • Save tovacinni/9a2327491d0655ee78d2a8393daa8f55 to your computer and use it in GitHub Desktop.
Save tovacinni/9a2327491d0655ee78d2a8393daa8f55 to your computer and use it in GitHub Desktop.
Filter out safe-to-load watertight STLs
# This simple script will filter out STLs which will not properly load
# with trimesh as a watertight mesh, and save them as space efficient ply files.
import os
import sys
import glob
import trimesh
if __name__ == '__main__':
path = sys.argv[1]
stl_paths = glob.glob(path + "/*")
output_path = sys.argv[2]
if not os.path.exists(output_path):
os.makedirs(output_path)
for stl_path in stl_paths:
try:
mesh = trimesh.load(stl_path)
assert(mesh.is_watertight or "Mesh not watertight!")
name = os.path.basename(os.path.splitext(stl_path)[0])
mesh.export(os.path.join(output_path, name + ".ply"), "ply")
except KeyboardInterrupt:
sys.exit()
except:
print(f"Issues found in {stl_path}, not exporting")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment