Last active
March 27, 2024 15:33
-
-
Save randomize/a95e2db97b1277bbb49d4f2f59c08f3d to your computer and use it in GitHub Desktop.
Python script to convert *.ply to *.obj (3D formats)
This file contains 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
''' | |
Simple script to convert ply to obj models | |
''' | |
from argparse import ArgumentParser | |
from plyfile import PlyData | |
def main(): | |
parser = ArgumentParser() | |
parser.add_argument('ply_filename') | |
parser.add_argument('obj_filename') | |
args = parser.parse_args() | |
convert(PlyData.read(args.ply_filename), args.obj_filename) | |
def convert(ply, obj): | |
''' | |
Convert given ply data | |
''' | |
with open(obj, 'w') as f: | |
f.write("# OBJ file\n") | |
verteces = ply['vertex'] | |
for v in verteces: | |
p = [v['x'], v['y'], v['z']] | |
c = [ v['red'] / 256 , v['green'] / 256 , v['blue'] / 256 ] | |
a = p + c | |
f.write("v %.6f %.6f %.6f %.6f %.6f %.6f \n" % tuple(a) ) | |
for v in verteces: | |
n = [ v['nx'], v['ny'], v['nz'] ] | |
f.write("vn %.6f %.6f %.6f\n" % tuple(n)) | |
for v in verteces: | |
t = [ v['s'], v['t'] ] | |
f.write("vt %.6f %.6f\n" % tuple(t)) | |
if 'face' in ply: | |
for i in ply['face']['vertex_indices']: | |
f.write("f") | |
for j in range(i.size): | |
# ii = [ i[j]+1 ] | |
ii = [ i[j]+1, i[j]+1, i[j]+1 ] | |
# f.write(" %d" % tuple(ii) ) | |
f.write(" %d/%d/%d" % tuple(ii) ) | |
f.write("\n") | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had the same issue. This is hapenning if your .ply is missing some attributes (which are mandatory in this script).
I fixed these fields to be optional. You may update your gist:
ply_to_obj.py