-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathObjParser.py
More file actions
32 lines (27 loc) · 1.14 KB
/
Copy pathObjParser.py
File metadata and controls
32 lines (27 loc) · 1.14 KB
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
import numpy as np
def parsOBJ(path):
print('loading', path)
vertices = []
texture_coordinates = []
faces = []
f = open(path, 'r')
for line in f:
if line[0] != '#':
line_parts = line.replace('\n', '').split(' ')
if line_parts[0] == 'v':
vertex = [float(line_parts[1]), float(line_parts[2]), float(line_parts[3])]
vertices.append(vertex)
elif line_parts[0] == 'vt':
tex_coord = [float(line_parts[1]), float(line_parts[2])]
texture_coordinates.append(tex_coord)
elif line_parts[0] == 'f' and len(line_parts) == 5:
face = [int(line_parts[1].split('/')[0])-1, int(line_parts[2].split('/')[0])-1, int(line_parts[3].split('/')[0])-1, int(line_parts[4].split('/')[0])-1]
faces.append(face)
f.close()
gl_vertices = []
for face in faces:
gl_vertices.append(vertices[face[0]])
gl_vertices.append(vertices[face[1]])
gl_vertices.append(vertices[face[2]])
gl_vertices.append(vertices[face[3]])
return np.asarray(gl_vertices)