Go to the documentation of this file.00001 import os, sys
00002
00003 def fillPreamble (num_ver, num_faces):
00004 preamble = "ply\nformat ascii 1.0\nelement vertex " + str(num_ver) + "\n" + \
00005 "property float x\nproperty float y\nproperty float z\nelement face " + str(num_faces) + "\n" + \
00006 "property list uchar int vertex_indices\nend_header\n"
00007 return preamble
00008
00009 def convertToPly(in_file, out_file):
00010 print "in_file:", in_file
00011 input = open(in_file)
00012 output = open(out_file, 'w')
00013
00014
00015 vertex_line = ['', '', '']
00016 line_count = 0;
00017 vertices_string_tmp = ''
00018 for s in input.xreadlines():
00019
00020 vertex_x = s.find("x: ")
00021 if vertex_x >= 0:
00022
00023 vertex_line[0] = s.split("x: ")[1].rstrip('\n')
00024 vertex_y = s.find("y: ")
00025 if vertex_y >= 0:
00026
00027 vertex_line[1] = s.split("y: ")[1].rstrip('\n')
00028 vertex_z = s.find("z: ")
00029 if vertex_z >= 0:
00030
00031 vertex_line[2] = s.split("z: ")[1].rstrip('\n')
00032
00033 if vertex_line[0] != '' and vertex_line[1] != '' and vertex_line[2] != '':
00034
00035
00036
00037 vertices_string_tmp = vertices_string_tmp + ' '.join(vertex_line) + '\n'
00038 line_count = line_count + 1
00039 vertex_line = ['', '', '']
00040
00041
00042 faces = s.find("triangles: [")
00043 if faces >= 0:
00044 faces_string = s.split("triangles: [")[1].rstrip("\n").rstrip("]")
00045 faces_list = faces_string.split(', ')
00046
00047
00048 print "#vertices: ", line_count
00049
00050
00051 output.write(fillPreamble(line_count, 0))
00052 output.write(vertices_string_tmp)
00053
00054 for (counter, v) in enumerate(faces_list):
00055 if counter%3 == 0:
00056 output.write('3 ' + faces_list[counter] + ' ' + faces_list[counter + 1] + ' ' + faces_list[counter + 2] + '\n')
00057 print "#faces: ", faces_list.__len__()/3
00058 input.close()
00059 output.close()
00060
00061 if __name__ == "__main__":
00062 in_file = sys.argv[1]
00063 out_file = sys.argv[2]
00064 convertToPly (in_file, out_file)