-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathwavefront.py
More file actions
56 lines (46 loc) · 1.97 KB
/
wavefront.py
File metadata and controls
56 lines (46 loc) · 1.97 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Wavefront3D:
def __init__(self, file_path):
self.file_path = file_path
self.load()
def load(self):
with open(self.file_path, "r") as f:
self.vertices = []
self.texture_coords = []
self.normals = []
self.faces = []
lines = f.readlines()
for line in lines:
if line.startswith("v "):
line = line.strip().split()
v_x = float(line[1])
v_y = float(line[2])
v_z = float(line[3])
self.vertices.append([v_x, v_y, v_z])
elif line.startswith("vt "):
line = line.strip().split()
u = float(line[1])
v = float(line[2])
self.texture_coords.append([u, v])
elif line.startswith("vn "):
line = line.strip().split()
vn_x = float(line[1])
vn_y = float(line[2])
vn_z = float(line[3])
self.normals.append([vn_x, vn_y, vn_z])
elif line.startswith("f "):
line = line.strip().split()
line[1] = line[1].split("/")
v1 = int(line[1][0])
vt1 = int(line[1][1])
vn1 = int(line[1][2])
line[2] = line[2].split("/")
v2 = int(line[2][0])
vt2 = int(line[2][1])
vn2 = int(line[2][2])
line[3] = line[3].split("/")
v3 = int(line[3][0])
vt3 = int(line[3][1])
vn3 = int(line[3][2])
self.faces.append([[v1,v2,v3],
[vt1,vt2,vt3],
[vn1,vn2,vn3]])