-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.py
202 lines (169 loc) · 6.76 KB
/
parser.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import bpy
import bmesh
import mathutils
import os
import shlex
class Material():
def __init__(self, index, name, path):
self.index = index
self.name = name
self.texture_path = path
class Object():
def __init__(self, index):
self.faces = []
self.index = index
self.name = "UnnamedObject_%d" % (index)
class Face():
def __init__(self, object_index, material_index):
self.object_index = object_index
self.material_index = material_index
self.vertex = []
class Influence():
def __init__(self, index, weight):
self.weight = weight
self.index = index
class Vertex():
def __init__(self):
self.offset = mathutils.Vector()
self.normal = mathutils.Vector()
self.uv = mathutils.Vector((0, 0))
self.influences = []
class Bone():
def __init__(self, index, parent, tag):
self.tag = tag
self.index = index
self.parent = parent
self.offset = mathutils.Vector()
self.x = mathutils.Vector()
self.y = mathutils.Vector()
self.z = mathutils.Vector()
self.scale = mathutils.Vector((1, 1, 1))
self.vertices = []
class Parser():
def __init__(self):
self.numbones = -1
self.numfaces = -1
self.bones = []
self.vertices = []
self.materials = []
self.objects = []
self.current_object = None
self.current_face = None
self.filepath = None
def error(self, msg):
raise Exception(msg)
def parse_version(self, ver):
if ver != "6":
self.error("Invalid version %s" % (ver))
def parse_numfaces(self, nf):
self.numfaces = int(nf)
def parse_numbones(self, nb):
self.numbones = int(nb)
def parse_vert(self, vertex_index):
if self.numfaces == -1: # vertex definition
self.vertices.append(Vertex())
self.current_object = self.vertices[-1]
else:
self.current_face.vertex.append(int(vertex_index))
self.current_object = self.vertices[int(vertex_index)]
#print("setting vert to %d" % (int(vertex_index)))
def parse_set_current_face_index(self, object_index_str, material_index, c1, c2):
obj_idx = int(object_index_str)
if obj_idx >= len(self.objects):
self.objects.append(Object(obj_idx))
obj = self.objects[-1]
obj.faces.append(Face(obj_idx, int(material_index)))
self.current_face = obj.faces[-1]
def parse_bone_definition(self, index, parent, tag):
self.bones.append(Bone(int(index), int(parent), tag[1:-1]))
def parse_set_current_bone_index(self, index):
self.current_object = self.bones[int(index)]
def parse_vector_string(self, x, y, z):
v = mathutils.Vector((0, 0, 0))
v[:] = float(x[0:-1]), float(y[0:-1]), float(z[0:-1])
return v
def parse_offset(self, x, y, z):
self.current_object.offset = self.parse_vector_string(x, y, z)
def parse_x(self, x, y, z):
self.current_object.x = self.parse_vector_string(x, y, z)
def parse_y(self, x, y, z):
self.current_object.y = self.parse_vector_string(x, y, z)
def parse_z(self, x, y, z):
self.current_object.z = self.parse_vector_string(x, y, z)
def parse_object(self, index, name):
self.objects[int(index)].name = name[1:-1]
def parse_normal(self, x, y, z):
if not isinstance(self.current_object, Vertex):
self.error("Current object is not of type Vertex")
self.current_object.normal = mathutils.Vector((float(x), float(y), float(z)))
def parse_uv(self, c, u, v):
if not isinstance(self.current_object, Vertex):
self.error("Current object is not of type Vertex")
uv = mathutils.Vector((float(u), float(v)))
while uv.x < 0.0:
uv.x += 1.0
while uv.y < 1.0:
uv.y += 1.0
while uv.x > 1.0:
uv.x -= 1.0
while uv.y > 1.0:
uv.y -= 1.0
self.current_object.uv = mathutils.Vector((uv.x, 1.0 - uv.y))
#print("setting uv to %f %f" % (float(u), float(v)))
def parse_material(self, index, material, shading, image):
name = material[1:-1]
path = image[1:-1]
# nvm, just manually fix the materials
#if len(path) != 0:
# path = path.split("color:")[1].replace("\\\\", "/") # get path after color:
# path = os.path.dirname(self.filepath) + "/" + path
# path = path.replace("~-g", "")
m = Material(int(index), name, path)
self.materials.append(m)
def parse_vertex_bone_weight(self, bone_index, weight):
if not isinstance(self.current_object, Vertex):
self.error("Current object is not of type Vertex")
bi = int(bone_index)
b = self.bones[bi]
self.current_object.influences.append(Influence(bi, float(weight)))
b.vertices.append(self.current_object)
def read_file(self, path):
self.filepath = path
with open(path, "r", encoding="utf-8") as f:
lines = f.read().splitlines()
for l in lines:
if len(l) == 0:
continue
if l[0] == '/' and l[1] == '/':
continue
#sp = l.strip().split(" ")
sp = shlex.split(l.strip(), posix=False)
if len(sp) == 0:
continue
key = sp[0]
args = sp[1:]
parsers = {
"VERSION": self.parse_version,
"NUMBONES": self.parse_numbones,
"OFFSET": self.parse_offset,
"X": self.parse_x,
"Y": self.parse_y,
"Z": self.parse_z,
"NORMAL": self.parse_normal,
"UV": self.parse_uv,
"MATERIAL": self.parse_material,
"TRI": self.parse_set_current_face_index,
"BONE": {
4: self.parse_bone_definition,
2: self.parse_set_current_bone_index,
3: self.parse_vertex_bone_weight,
},
"OBJECT": self.parse_object,
"NUMFACES": self.parse_numfaces,
"VERT": self.parse_vert,
}
if key in parsers:
if callable(parsers[key]):
parsers[key](*args)
else:
parsers[key][len(sp)](*args)