-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadOBJ.py
81 lines (71 loc) · 2.81 KB
/
loadOBJ.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
# consider cgkit.objmtl.OBJReader
import operator
def loadOBJvertices(filename):
vertices = []
for line in open(filename, "r"):
vals = line.strip().split()
if len(vals) > 0:
if vals[0] == "v":
v = map(float, vals[1:4])
vertices.append(v)
return vertices
def loadOBJverticesANDfaces(filename):
vertices = []
faces = []
for line in open(filename, "r"):
vals = line.strip().split()
if len(vals) > 0:
if vals[0] == "v":
v = map(float, vals[1:4])
vertices.append(v)
if vals[0] == "f":
face = line[2:].strip().split()
f = map(int, face)
f = map(operator.add, f, [-1, -1, -1])
faces.append(f)
return vertices, faces
def loadOBJ(filename):
vertices = []
normals = []
faces = []
for line in open(filename, "r"):
vals = line.strip().split()
if len(vals) > 0:
if vals[0] == "v":
v = map(float, vals[1:4])
vertices.append(v)
if vals[0] == "vn":
n = map(float, vals[1:4])
normals.append(n)
if vals[0] == "f":
face = line[2:].strip().split()
f = map(int, face)
f = map(operator.add, f, [-1, -1, -1])
# fix this part once there are normals
# for i in range(0, len(face)) :
# face[i] = face[i].split('/')
# # OBJ indexies are 1 based not 0 based hence the -1
# # convert indexies to integer
# for j in range(0, len(face[i])) : f[i][j] = int(face[i][j]) - 1
faces.append(f)
return vertices, normals, faces
def loadOBJtextures(filename):
vertices = []
normals = []
faces = []
for line in open(filename, "r"):
vals = line.strip().split()
if len(vals) > 0:
if vals[0] == "v":
v = map(float, vals[1:4])
vertices.append(v)
if vals[0] == "vn":
n = map(float, vals[1:4])
normals.append(n)
if vals[0] == "f":
face = line[2:].strip().split()
f = map(int, face)
f = map(operator.add, f, [-1, -1, -1])
# fix this part once there are normals and textures
faces.append(f)
return vertices, normals, faces