forked from Tachiorz/mabinogi_blender_addons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_import_mabinogi_frm.py
399 lines (352 loc) · 12.3 KB
/
io_import_mabinogi_frm.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
bl_info= {
"name": "Import Mabinogi Framework",
"author": "Tachiorz",
"version": (0, 3),
"blender": (2, 5, 7),
"location": "File > Import > Mabinogi Framework (.frm)",
"description": "Imports a Mabinogi Framework",
"warning": "",
"wiki_url": "",
"tracker_url": "",
"category": "Import-Export"
}
import os
import struct
import math
import bpy
import mathutils
class MabinogiBone():
"""Framework Bone structure
Used in bone import. Discarded after importing to blender data.
Link -- unknown
name -- bone name, used to bind meshes
quat1 -- unknown
quat2 -- unknown, contains global bone coordinate
"""
GlobalToLocal = mathutils.Matrix()
LocalToGlobal = mathutils.Matrix()
Link = mathutils.Matrix()
name = b''
boneid = 0
parentid = -1
quat1 = mathutils.Quaternion()
quat2 = mathutils.Quaternion()
class MabinogiHash():
"""Hash of bone names"""
count = 0 # number of keys in hash
count2= 0
size1 = 0
size2 = 0
totalsize = 0
h1 = list() #[maxlen][256]
h2 = list() #[maxlen][256]
h3 = list()
maxlen = 0 # max length of key
keys = list()
hash1 = 0
hash2 = 0
def ExportQuerySize(self):
self.size2 = 2*self.count2
self.size1 = self.maxlen*512
self.totalsize = 28 + self.size2 + self.size1*2
return self.totalsize
def GenerateRandomTable(self):
self.h1 = list()
self.h2 = list()
self.h3 = [0] * self.count2
for pos in range(self.maxlen):
self.h1.append(list())
self.h2.append(list())
for i in range(256):
self.h1[pos].append(ord(os.urandom(1)))
self.h2[pos].append(ord(os.urandom(1)))
self.h1[pos][i] %= self.count2
self.h2[pos][i] %= self.count2
def F(self, key):
self.hash1 = 0
self.hash2 = 0
l = len(key)
if l <= self.maxlen:
for i in range(l):
self.hash1 += self.h1[i][ord(key[i])]
self.hash2 += self.h2[i][ord(key[i])]
if self.hash1 >= self.count2:
self.hash1 -= self.count2
if self.hash2 >= self.count2:
self.hash2 -= self.count2
return 1
else:
return 0
def AddKey(self,key):
print("Adding key ",key)
self.keys.append(key)
self.count = len(self.keys)
self.count2 = self.count*2
if self.maxlen < len(key):
self.maxlen = len(key)
def CheckCycle(self):
check1 = [[-1]*self.count2 for _ in range(self.count2)]
check2 = [0] * self.count2
i = 0
for key in self.keys:
self.F(key)
if self.hash1 == self.hash2 or check1[self.hash1][self.hash2] != -1:
return False
check1[self.hash1][self.hash2] = i
check1[self.hash2][self.hash1] = i
i += 1
for i in range(self.count2):
if check2[i] == 0:
self.h3[i] = 0
if self.Traverse(-1, i, check1, check2):
return False
return True
def Traverse(self, xx, y, check1, check2):
check2[y] = 1
if self.count2 <= 0:
return False
for x in range(self.count2):
c = check1[y][x]
if c != -1:
if check2[x] == 1:
if x != xx:
return True
else:
self.h3[x] = c - self.h3[y]
while self.h3[x] < 0:
self.h3[x] += self.count
while self.h3[x] >= self.count:
self.h3[x] -= self.count
if self.Traverse(y, x, check1, check2):
return True
return False
def BuildTable(self):
"""Generates hash from names added by AddKey()"""
result = False
while not result:
print("trying to build hash...")
self.GenerateRandomTable()
result = self.CheckCycle()
def GetHashValue(self, key):
self.F(key)
result = self.h3[self.hash1] + self.h3[self.hash2]
while result < 0:
result += self.count
while result >= self.count:
result -= self.count
return result
def ToFile(self, file):
"""Writes hash to file
BuildTable() must be called first!
"""
file.write(struct.pack("<hhii3ihh", self.count, self.count2,
self.size1, self.size2, 0, 0, 0, self.maxlen, 0))
for n in range(self.maxlen):
[file.write(struct.pack("<h", self.h1[n][i])) for i in range(256)]
for n in range(self.maxlen):
[file.write(struct.pack("<h", self.h2[n][i])) for i in range(256)]
[file.write(struct.pack("<h", self.h3[i])) for i in range(self.count2)]
def load_matrix4x4(file):
m = mathutils.Matrix()
for n in range(4):
m[n][0:4] = struct.unpack("<4f", file.read(16))
return m
def save_matrix4x4(file, m):
for n in range(4):
[file.write(struct.pack("<f", m[n][i])) for i in range(4)]
return m
def load_quaternion(file):
q = mathutils.Quaternion()
q[0:5] = list(struct.unpack("<4f", file.read(16)))
return q
def save_quaternion(file,q ):
[file.write(struct.pack("<f", q[i])) for i in range(4)]
return q
def vec_roll_to_mat3(vec, roll):
target = mathutils.Vector((0,1,0))
nor = vec.normalized()
axis = target.cross(nor)
if axis.dot(axis) > 0.0000000001: # this seems to be the problem for some bones, no idea how to fix
axis.normalize()
theta = target.angle(nor)
bMatrix = mathutils.Matrix.Rotation(theta, 3, axis)
else:
updown = 1 if target.dot(nor) > 0 else -1
bMatrix = mathutils.Matrix.Scale(updown, 3)
rMatrix = mathutils.Matrix.Rotation(roll, 3, nor)
mat = rMatrix * bMatrix
return mat
def mat3_to_vec_roll(mat):
vec = mat.col[1]
vecmat = vec_roll_to_mat3(mat.col[1], 0)
vecmatinv = vecmat.inverted()
rollmat = vecmatinv * mat
roll = math.atan2(rollmat[0][2], rollmat[2][2])
return vec, roll
def load_frm(filename,
context):
'''Read and import the FRM file.'''
name, ext= os.path.splitext(os.path.basename(filename))
file= open(filename, 'rb')
try:
magic, version, bones_count = struct.unpack("<4shh", file.read(8))
except:
print("Error parsing file header!")
file.close()
return
if magic != b'pf!\x00':
print("Not a supported file type!")
file.close()
return
if version != 1:
print("Not a supported version!")
file.close()
return
print("bones count = ", bones_count)
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.armature_add()
arm_object= bpy.context.active_object
arm_object.name= "ARM_" + name
arm_object.location = (0,0,0)
arm_object.data.name= arm_object.name
bpy.ops.object.mode_set(mode='EDIT')
bones = arm_object.data.edit_bones
for b in bones:
bones.remove(b)
bone_space = mathutils.Matrix(((0, 1, 0, 0),
(0, 0, 1, 0),
(1, 0, 0, 0),
(0, 0, 0, 1)))
bone = list()
for b in range(bones_count):
bone.append(MabinogiBone())
bone[b].GlobalToLocal = load_matrix4x4(file)
bone[b].LocalToGlobal = load_matrix4x4(file)
bone[b].Link = load_matrix4x4(file)
bone[b].name, bone[b].boneid, bone[b].parentid, empty = struct.unpack("<32sbbh", file.read(36))
bone[b].name = bone[b].name.decode(encoding="ascii").strip('\x00').strip(' ')
bone[b].quat1 = load_quaternion(file)
bone[b].quat2 = load_quaternion(file)
mat = bone[b].LocalToGlobal * bone_space
pos = mat.to_translation()
axis, roll = mat3_to_vec_roll(mat.to_3x3())
nb = bones.new(str(b) + "__" + bone[b].name)
nb.head = pos
nb.tail = pos + axis
nb.roll = roll
nb.use_connect = False
if bone[b].parentid != -1:
nb.parent = bone[bone[b].parentid].nb
bone[b].nb = nb
bpy.ops.object.mode_set(mode='OBJECT')
file.close()
def save_frm(filename,
context):
"""Export to FRM file.
Doesn't work at this moment
"""
name, ext= os.path.splitext(os.path.basename(filename))
arm_object = bpy.context.active_object
if arm_object.type != 'ARMATURE':
print("Select armature please")
return
try:
file = open(filename, 'wb')
except:
print("Can't create file: " + filename)
return
if arm_object.data.bones.find('-com') == -1:
print("Armature don't have '-com' bone!")
#return
bpy.ops.object.mode_set(mode='EDIT')
bones = arm_object.data.edit_bones[0].children_recursive
bones[:0] = [arm_object.data.edit_bones[0]]
bones_count = len(bones)
#write header
file.write(struct.pack('<4shh',b'pf!\x00',1,bones_count))
hash = MabinogiHash()
#write bones
for b in range(bones_count):
m1 = mathutils.Matrix.Translation(bones[b].tail)
m2 = mathutils.Matrix.Translation(bones[b].tail)
m2.invert()
save_matrix4x4(file, m1)
save_matrix4x4(file, m2)
if bones[b].parent == None:
parentid = -1
m3 = bones[b].matrix
else:
parentid = bones.index(bones[b].parent)
m3 = mathutils.Matrix.Translation(bones[b].parent.head - bones[b].tail)
save_matrix4x4(file, m3)
file.write(struct.pack("<32sbbh",bones[b].name.encode("utf-8"), b, parentid, 0))
save_quaternion(file,[0,0,0,1])
save_quaternion(file,list(bones[b].tail[:]) + [1])
hash.AddKey(bones[b].name[1:])
#write hash
its_good = False
while not its_good:
hash.BuildTable()
i = 0
for k in hash.keys:
if i != hash.GetHashValue(k):
its_good = False
print("It's not good, recalculate =|")
break
else:
print(i)
its_good = True
i += 1
file.write( struct.pack("<i",hash.ExportQuerySize()) )
hash.ToFile(file)
file.close()
from bpy.props import StringProperty
class IMPORT_MABINOGI_frm(bpy.types.Operator):
'''Import FRM Operator.'''
bl_idname= "import.frm"
bl_label= "Import FRM"
bl_description= "Import a Mabinogi Framework file"
bl_options= {'REGISTER', 'UNDO'}
filepath= StringProperty(name="File Path", description="Filepath used for importing the FRM file", maxlen=1024, default="")
filter_glob = StringProperty(
default = "*.frm",
options = {'HIDDEN'},
)
def execute(self, context):
load_frm(self.filepath,
context)
return {'FINISHED'}
def invoke(self, context, event):
wm= context.window_manager
wm.fileselect_add(self)
return {'RUNNING_MODAL'}
class EXPORT_MABINOGI_frm(bpy.types.Operator):
'''Export FRM Operator.'''
bl_idname= "export.frm"
bl_label= "Export FRM"
bl_description= "Export a Mabinogi Framework file"
bl_options= {'REGISTER', 'UNDO'}
filepath= StringProperty(name="File Path", description="Filepath used for exporting the FRM file", maxlen=1024, default="")
def execute(self, context):
save_frm(self.filepath,
context)
return {'FINISHED'}
def invoke(self, context, event):
wm= context.window_manager
wm.fileselect_add(self)
return {'RUNNING_MODAL'}
def menu_func(self, context):
self.layout.operator(IMPORT_MABINOGI_frm.bl_idname, text="Mabinogi Framework (.frm)")
def menu_func2(self, context):
self.layout.operator(EXPORT_MABINOGI_frm.bl_idname, text="Mabinogi Framework (.frm)")
def register():
bpy.utils.register_module(__name__)
bpy.types.INFO_MT_file_import.append(menu_func)
bpy.types.INFO_MT_file_export.append(menu_func2)
def unregister():
bpy.utils.unregister_module(__name__)
bpy.types.INFO_MT_file_import.remove(menu_func)
bpy.types.INFO_MT_file_export.remove(menu_func2)
if __name__ == "__main__":
register()