-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
75 lines (58 loc) · 2.09 KB
/
model.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
import numpy as np
import pywavefront
class Model:
def __init__(self, filepath):
self.filepath = self.sanitize_filepath(filepath)
self.vertices, self.lines, self.faces = self.read_model()
def sanitize_filepath(self, path):
if not path.endswith(".mdl"):
path = path + ".mdl"
if not path.startswith("data/models/"):
path = "data/models/" + path
return path
def read_model(self):
mfile = open(self.filepath, "r")
mlines = mfile.readlines()
vertices = []
lines = []
faces = []
for l in mlines:
# vertices
if l.startswith("V|"):
# get rid of 'V|'
l = l[2:]
# get rid of spaces
l = l.replace(" ", "")
l = l.replace("\n", "")
l = l.split(",")
vertices.append(np.array([float(l[0]),
float(l[1]),
float(l[2])]))
# lines
elif l.startswith("L|"):
l = l[2:]
l = l.replace(" ", "")
l = l.replace("\n", "")
l = l.split(",")
lines.append([int(l[0]) - 1, int(l[1]) - 1])
# faces
elif l.startswith("F|"):
l = l[2:]
l = l.replace(" ", "")
l = l.replace("\n", "")
l = l.split(",")
new_face = []
for i in l:
new_face.append(int(i) - 1)
faces.append(new_face)
return vertices, lines, faces
class WFModel:
def __init__(self, filepath):
self.filepath = self.sanitize_filepath(filepath)
self.modelObj = pywavefront.Wavefront(self.filepath, collect_faces=True)
def sanitize_filepath(self, path):
if not path.endswith(".obj"):
path = path + ".obj"
if not path.startswith("data/models/"):
path = "data/models/" + path
return path