-
Notifications
You must be signed in to change notification settings - Fork 0
/
OBJ.py
90 lines (60 loc) · 2.58 KB
/
OBJ.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class OBJFastV:
def __init__(self, filename):
self.vertices = []
for line in open(filename, "r"):
if line.startswith('#'): continue
values = line.split()
if not values: continue
if values[0] == 'v':
v = float(values[1]),float(values[2]),float(values[3])
self.vertices.append(v)
class OBJ:
def __init__(self, filename):
self.vertices = []
self.normals = []
self.texcoords = []
self.faces = []
self.mtlfile = []
self.material = []
material = None
for line in open(filename, "r"):
# if line starting with #, skip
if line.startswith('#'): continue
# split line (separate each info between spaces)
values = line.split()
# if empty line, skip
if not values: continue
# if line starting with v
if values[0] == 'v':
v = float(values[1]),float(values[2]),float(values[3])
self.vertices.append(v)
# if line starting with vn
elif values[0] == 'vn':
vn = float(values[1]),float(values[2]),float(values[3])
self.normals.append(vn)
# if line starting with vt
elif values[0] == 'vt':
vt = float(values[1]),float(values[2])
self.texcoords.append(vt)
# if line starting with usemtl / usemat
elif values[0] in ('usemtl', 'usemat'):
material = values[1]
self.material = material
# if line starting with f
elif values[0] == 'f':
face = []
texcoords = []
norms = []
for f in values[1:]:
# split line again (separate each info between / )
val = f.split('/')
face.append(int(val[0]))
if len(val) >= 2 and len(val[1]) > 0:
texcoords.append(int(val[1]))
else:
texcoords.append(0)
if len(val) >= 3 and len(val[2]) > 0:
norms.append(int(val[2]))
else:
norms.append(0)
self.faces.append((face, norms, texcoords, material))