-
Notifications
You must be signed in to change notification settings - Fork 0
/
term3d_converter.py
75 lines (62 loc) · 2.24 KB
/
term3d_converter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/python3.6
'''
This program is a modified version of "minirt_mesh_converter" by matboivin, which can be found at the link below.
This program is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.
https://github.com/matboivin/minirt_mesh_converter
'''
import os
import sys
def get_triangle(line, d):
i = line.split(' ')
triangle = []
i1 = i[1].split('/')[0]
i2 = i[2].split('/')[0]
i3 = i[3].split('/')[0]
vertex1 = ','.join((d[i1]['x'], d[i1]['y'], d[i1]['z']))
vertex2 = ','.join((d[i2]['x'], d[i2]['y'], d[i2]['z']))
vertex3 = ','.join((d[i3]['x'], d[i3]['y'], d[i3]['z']))
triangle.append(' '.join((vertex1, vertex2, vertex3)))
# if a face line has more than 5 params then its a quad not a tris therefor
# we transform the quad into a tris.. still have to implement other polyons.
# 4-------3
# | /|
# | / |
# | / |
# |/ |
# 1-------2
# 1 2 3 4 into 1 2 3 and 1 3 4
if len(i) >= 5:
i4 = i[4].split('/')[0]
vertex4 = ','.join((d[i4]['x'], d[i4]['y'], d[i4]['z']))
triangle.append('\n')
triangle.append(' '.join((vertex1, vertex3, vertex4)))
return triangle
def parse_polygons(filename, obj_file, d):
with open(filename, 'w') as out:
for line in obj_file:
if line[:2] == 'f ':
triangle = get_triangle(line, d)
for t in triangle:
out.write(t)
out.write("\n")
def parse_vertices(obj_file, d):
count = 1
for line in obj_file:
if line[:2] == 'v ':
coord = line.split(' ')
d[str(count)] = {'x': coord[1], 'y': coord[2], 'z': coord[3][:-1]}
count += 1
return d
def convert_to_tri(filepath):
d = dict()
outname = os.path.dirname(filepath) + "/" + os.path.splitext(os.path.basename(filepath))[0] + ".tri"
with open(filepath, 'r') as f:
obj_file = f.readlines()
d = parse_vertices(obj_file, d)
parse_polygons(outname, obj_file, d)
print('created .tri file in ' + outname)
if len(sys.argv) != 2:
print('Usage: python term3d_converter.py <file.obj>')
else:
filepath = os.path.join(sys.argv[1])
convert_to_tri(filepath)