欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

PlytoObj(python“.ply”文件转“.obj”格式)

时间:2023-05-25

import osimport numpy as npfrom glob import globfrom plyfile import PlyDatafrom tqdm import tqdmdef write_obj(verts, faces, obj_path): """ Write .obj file """ assert obj_path[-4:] == '.obj' with open(obj_path, 'w') as fp: for v in verts: fp.write('v %f %f %fn' % (v[0], v[1], v[2])) for f in faces+1: fp.write('f %d %d %dn' % (f[0], f[1], f[2]))def ply2obj(ply_name, obj_name): plydata = PlyData.read(ply_name) pc = plydata['vertex'].data # print(pc) faces = plydata['face'].data # print(faces) try: pc_array = np.array([[x, y, z] for x,y,z,_,_,_ in pc]) except: pc_array = np.array([[x, y, z] for x,y,z in pc]) face_array = np.array([face[0] for face in faces], dtype=np.int) write_obj(pc_array, face_array, obj_name)if __name__ == "__main__": filelist = glob(r'路径*.ply') # print(filelist) for i in tqdm(range(len(filelist))): if os.path.exists(filelist[i].replace('.ply','.obj')): continue ply2obj(filelist[i], filelist[i].replace('.ply','.obj')) #目标文件与源文件保存在同一目录

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。