Skip to content

Instantly share code, notes, and snippets.

@assafrabin
Forked from randomize/ply2obj.py
Last active December 7, 2023 03:07
Show Gist options
  • Save assafrabin/ca110dba829dc960c48ad9ab90b8081e to your computer and use it in GitHub Desktop.
Save assafrabin/ca110dba829dc960c48ad9ab90b8081e to your computer and use it in GitHub Desktop.
Python script to convert *.ply to *.obj (3D formats)
"""
Simple script to convert ply to obj models
"""
import os
from argparse import ArgumentParser
from plyfile import PlyData
def parse_args():
parser = ArgumentParser()
parser.add_argument('ply_path')
parser.add_argument('--obj_path', default=None, required=False)
args = parser.parse_args()
return args.ply_path, args.obj_path
def ply_path_to_obj_path(ply_path):
"""
Replaces the .ply extension with .obj extension
"""
return os.path.splitext(ply_path)[0] + '.obj'
def convert(ply_path, obj_path=None):
"""
Converts the given .ply file to an .obj file
"""
obj_path = obj_path or ply_path_to_obj_path(ply_path)
ply = PlyData.read(ply_path)
with open(obj_path, 'w') as f:
f.write("# OBJ file\n")
verteces = ply['vertex']
for v in verteces:
p = [v['x'], v['y'], v['z']]
if 'red' in v and 'green' in v and 'blue' in v:
c = [v['red'] / 256, v['green'] / 256, v['blue'] / 256]
else:
c = [0, 0, 0]
a = p + c
f.write("v %.6f %.6f %.6f %.6f %.6f %.6f \n" % tuple(a))
for v in verteces:
if 'nx' in v and 'ny' in v and 'nz' in v:
n = (v['nx'], v['ny'], v['nz'])
f.write("vn %.6f %.6f %.6f\n" % n)
for v in verteces:
if 's' in v and 't' in v:
t = (v['s'], v['t'])
f.write("vt %.6f %.6f\n" % 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")
def main():
ply_path, obj_path = parse_args()
obj_path = ply_path_to_obj_path(ply_path)
print(f"Converting {ply_path} to .obj...")
convert(ply_path, obj_path)
print(f"Conversion finished successfully. Output path: {obj_path}")
if __name__ == '__main__':
main()
@wangg12
Copy link

wangg12 commented Sep 4, 2019

Hey, how to convert ply file with texture_uv (vertex texture) to obj file?

@assafrabin
Copy link
Author

Honestly - I don't remember... I had some 3D project back then, but I left it.. Good Luck!

@santhu937
Copy link

Traceback (most recent call last):
File "ply2obj.py", line 77, in
main()
File "ply2obj.py", line 72, in main
convert(ply_path, obj_path)
File "ply2obj.py", line 58, in convert
for i in ply['face']['vertex_indices']:
File "/Users/santhosh/Desktop/iisc/san/lib/python2.7/site-packages/plyfile.py", line 770, in getitem
return self.data[key]
ValueError: no field of name vertex_indices

@assafrabin can u please help me with the following issue

@assafrabin
Copy link
Author

Honestly - I don't remember... I had some 3D project back then, but I left it.. Good Luck!

@wdwzytc
Copy link

wdwzytc commented May 20, 2021

@santhu937
try to replace "ply['face']['vertex_indices']" with "ply['face']['vertex_index']"

maybe you can also check "ply['face']" to see what field of names it has

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