Skip to content

Instantly share code, notes, and snippets.

@jmpinit
Created July 14, 2024 19:24
Show Gist options
  • Save jmpinit/962dc9208e2a997fd88b9af1b29e0ddd to your computer and use it in GitHub Desktop.
Save jmpinit/962dc9208e2a997fd88b9af1b29e0ddd to your computer and use it in GitHub Desktop.
Convert a 3D file to binary PLY format with assimp and the Python plyfile package.
#!/usr/bin/env python3
import os
import subprocess
import sys
import tempfile
from plyfile import PlyData
def convert_to_ply(input_file):
with tempfile.NamedTemporaryFile(delete=False, suffix='.ply') as tmp_file:
tmp_file.close()
subprocess.run(['assimp', 'export', input_file, tmp_file.name], check=True)
return tmp_file.name
def convert_ascii_to_binary_ply(input_path, output_path):
ply_data = PlyData.read(input_path)
ply_data.text = False
with open(output_path, 'wb') as output_file:
ply_data.write(output_file)
def main():
if len(sys.argv) != 3:
print('Usage: python3 to_plyb.py infile.glb outfile.plyb')
sys.exit(1)
infile = sys.argv[1]
outfile = sys.argv[2]
if not os.path.exists(infile):
print(f'Error: {infile} does not exist.')
sys.exit(1)
try:
ascii_ply = convert_to_ply(infile)
convert_ascii_to_binary_ply(ascii_ply, outfile)
print(f'Successfully converted {infile} to binary PLY {outfile}')
finally:
if 'ascii_ply' in locals() and os.path.exists(ascii_ply):
os.remove(ascii_ply)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment