forked from retreev/io_scene_mpet
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpet.py
992 lines (766 loc) · 29.9 KB
/
pet.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
# mpet.py - implementation of Ntreev PangYa .mpet and .pet format.
# By John Chadwick <[email protected]>
#
# Special thanks to HSReina for their universal extractor and a few pointers.
# Also, to the developers of the original paktools and mpetmqo tool.
from io import BytesIO
from os.path import splitext
from enum import IntEnum
from .ioutil import (
read_struct, write_struct,
read_cstr, write_cstr,
read_fixed_string, write_fixed_string,
wraptext
)
class eFILE_TYPE(IntEnum):
FT_TEXT= 1,
FT_SMTL= 2,
FT_BONE= 4,
FT_ANIM= 8,
FT_MESH= 16,
FT_FANM= 32,
FT_FRAM= 64,
FT_MOTI= 128,
FT_COLL= 256,
FT_ALL= 511,
FT_SKINLIST= 65536,
FT_EXTR= 131072
class eSPECULAR_MATERIAL_TYPE(IntEnum):
SMTL_BOOL= 0,
SMTL_INT= 1,
SMTL_FLOAT= 2,
SMTL_VECTOR2= 3,
SMTL_VECTOR3= 4,
SMTL_VECTOR4= 5,
SMTL_MATRIX= 6,
SMTL_MATRIXARRAY= 7,
SMTL_TEXTURE= 8,
SMTL_NUM= 9
FILE_PET = eFILE_TYPE.FT_ALL
FILE_APET = eFILE_TYPE.FT_BONE | eFILE_TYPE.FT_ANIM | eFILE_TYPE.FT_FRAM | eFILE_TYPE.FT_MOTI
FILE_BPET = eFILE_TYPE.FT_BONE | eFILE_TYPE.FT_COLL | eFILE_TYPE.FT_EXTR
FILE_MPET = eFILE_TYPE.FT_TEXT | eFILE_TYPE.FT_SMTL | eFILE_TYPE.FT_BONE | eFILE_TYPE.FT_MESH | eFILE_TYPE.FT_FANM | eFILE_TYPE.FT_SKINLIST
FILE_ANIM_AND_BONE = eFILE_TYPE.FT_BONE | eFILE_TYPE.FT_ANIM
class Version:
def __init__(self, major=None, minor=None):
self.major=major
self.minor=minor
self.rest=0xFFFE
def fullVersion(self):
return (self.rest << 16) | (self.major << 8) | self.minor
def load(self, file):
self.minor, = read_struct(file, '<B')
self.major, = read_struct(file, '<B')
self.rest, = read_struct(file, '<H')
if self.rest != 0xFFFE:
print("Version.rest invalid(%d)" % self.rest)
self.rest = 0xFFFE
def save(self, file):
write_struct(file, '<B', self.minor)
write_struct(file, '<B', self.major)
write_struct(file, '<H', self.rest)
def __repr__(self):
return "Version(%d.%d)" % (self.major, self.minor)
VERSION_1_0 = Version(1, 0)
VERSION_1_1 = Version(1, 1)
VERSION_1_2 = Version(1, 2)
VERSION_1_3 = Version(1, 3)
gFileType = FILE_PET
gVersion = VERSION_1_0
def setFileType(filepath):
global gFileType
gFileType = FILE_PET
if filepath == '':
return
base, ext = splitext(filepath)
if ext == '.pet':
gFileType = FILE_PET
elif ext == '.mpet':
gFileType = FILE_MPET
elif ext == '.apet':
gFileType = FILE_APET
elif ext == '.bpet':
gFileType = FILE_BPET
def compareVersions(v1, v2):
if v1.fullVersion() == v2.fullVersion():
return 0
if v1.major == v2.major:
return -1 if v1.minor < v2.minor else 1
return -1 if v1.major < v2.major else 1
class Puppet:
def __init__(self, version=None, smtrls=None, animations=None, fanims=None, frames=None, motions=None, collisions=None, extras=None, bones=None, textures=None, meshes=None, meshid=None):
self.version = version
self.smtrls = smtrls
self.animations = animations
self.face_anims = fanims
self.frames = frames
self.motions = motions
self.collisions = collisions
self.extras = extras
self.bones = bones
self.textures = textures
self.meshes = meshes
self.meshid = meshid
def load(self, file):
self.bones = []
self.textures = []
self.meshes = []
self.smtrls = []
self.animations = []
self.face_anims = []
self.frames = []
self.motions = []
self.collisions = []
self.extras = []
self.is_mpet = True if splitext(file.name)[1] == '.mpet' else False
self.is_apet = True if splitext(file.name)[1] == '.apet' else False
# Reset Version to 1.0
global gVersion
gVersion = VERSION_1_0
setFileType(file.name)
while True:
block = Block()
block.load(file)
# EOF
if not block.is_valid():
return
# Version block
if block.id == b'VERS':
self.version = Version()
self.version.load(block.stream)
gVersion = self.version
print(self.version)
# Specular Material block
elif block.id == b'SMTL':
count, = read_struct(block.stream, '<I')
for i in range(count):
smtl = SpecularMaterial()
smtl.load(block.stream)
self.smtrls.append(smtl)
# Textures block
elif block.id == b'TEXT':
count, = read_struct(block.stream, '<I')
for i in range(count):
texture = Texture()
texture.load(block.stream)
self.textures.append(texture)
# Animations block
elif block.id == b'ANIM':
while True:
bone_id, = read_struct(block.stream, '<B')
if bone_id == 0xFE:
bone_id, = read_struct(block.stream, '<H')
if bone_id == 0xFF or bone_id == 0xFFFF:
break
animation = Animation(bone_id)
animation.load(block.stream)
self.animations.append(animation)
# Bones block
elif block.id == b'BONE':
count, = read_struct(block.stream, '<B')
if count == 0:
count, = read_struct(block.stream, '<H')
for i in range(count):
bone = Bone()
bone.load(block.stream)
self.bones.append(bone)
# Meshes block
elif block.id == b'MESH':
mesh = Mesh()
mesh.load(block.stream, self.is_mpet)
self.meshes.append(mesh)
# Face Animations block
elif block.id == b'FANM':
count, = read_struct(block.stream, '<I')
for i in range(count):
fanim = FaceAnimation()
fanim.load(block.stream)
self.face_anims.append(fanim)
# Frames block
elif block.id == b'FRAM':
count, = read_struct(block.stream, '<I')
for i in range(count):
frame = Frame()
frame.load(block.stream, count)
self.frames.append(frame)
# Motions block
elif block.id == b'MOTI':
count, = read_struct(block.stream, '<I')
for i in range(count):
motion = Motion()
motion.load(block.stream)
self.motions.append(motion)
# Collisions block
elif block.id == b'COLL':
count, = read_struct(block.stream, '<I')
for i in range(count):
collision = Collision()
collision.load(block.stream)
self.collisions.append(collision)
# Extras block
elif block.id == b'EXTR':
count, = read_struct(block.stream, '<I')
for i in range(count):
extra = Extra()
extra.load(block.stream)
self.extras.append(extra)
# Unknown block
else:
print('warning: unknown block type: %s' % block.id)
def save(self, file):
self.is_mpet = True if splitext(file.name)[1] == '.mpet' else False
setFileType(file.name)
# Textures block
block = Block(b'TEXT')
write_struct(block.stream, '<I', len(self.textures))
for texture in self.textures:
texture.save(block.stream)
block.write(file)
# Bones block
block = Block(b'BONE')
write_struct(block.stream, '<B', len(self.bones))
for bone in self.bones:
bone.save(block.stream)
block.write(file)
class Block:
def __init__(self, id=None):
self.stream = BytesIO()
self.id = id
def is_valid(self):
return self.id is not None
def load(self, file):
id = file.read(4)
if id is None or len(id) == 0:
id = None
return
size, = read_struct(file, '<I')
data = file.read(size)
self.id = id
self.stream = BytesIO(data)
def save(self, file):
size = self.stream.tell()
self.stream.seek(0)
data = self.stream.read()
file.write(self.id)
write_struct(file, '<I', size)
file.write(data)
class Extra:
def __init__(self, bone_id=None):
self.bone_id = bone_id
def load(self, file):
self.bone_id, = read_struct(file, '<B')
if self.bone_id == 0xFE:
self.bone_id, = read_struct(file, '<H')
def save(self, file):
if self.bone_id > 0xF0:
write_struct(file, '<BH', 0xFE, self.bone_id)
else:
write_struct(file, '<B', self.bone_id)
def __repr__(self):
return "Extra(%d)" % (self.bone_id)
class Collision:
def __init__(self, shape=None, show=None, scripts=None, area=None):
self.shape=shape
self.show=show
self.scripts=scripts # Sempre uma lista de tamanho 4, {[0] - Box name, [1] - Bone name, [2:3] - Options(bound_box) %s %s}
self.area=area
def load(self, file):
self.scripts = []
self.shape, = read_struct(file, '<I')
self.show, = read_struct(file, '<I')
for i in range(4):
script = read_fixed_string(file)
self.scripts.append(script)
self.area = read_struct(file, '<6f')
def save(self, file):
write_struct(file, '<2I', self.shape, self.show)
for i in range(4):
try:
write_fixed_string(file, self.scripts[i])
except IndexError:
write_fixed_string(file, b'')
write_struct(file, '<6f', *self.area)
def __repr__(self):
return "Collision(%d, %d, %s %s %s %s %f %f %f %f %f %f)" % (self.shape, self.show, *self.scripts, *self.area)
class Motion:
def __init__(self, name=None, frame_start=None, frame_end=None, next_move=None, connection_method=None, connection_time=None, top_version=None):
self.name=name
self.frame_start=frame_start
self.frame_end=frame_end
self.next_move=next_move
self.connection_method=connection_method
self.connection_time=connection_time
self.top_version=top_version
def load(self, file):
self.name = read_fixed_string(file)
self.frame_start, = read_struct(file, '<I')
self.frame_end, = read_struct(file, '<I')
self.next_move = read_fixed_string(file)
self.connection_method = read_fixed_string(file)
self.connection_time, = read_struct(file, '<f')
self.top_version = read_fixed_string(file)
def save(self, file):
write_fixed_string(file, self.name)
write_struct(file, '<I', self.frame_start)
write_struct(file, '<I', self.frame_end)
write_fixed_string(file, self.next_move)
write_fixed_string(file, self.connection_method)
write_struct(file, '<f', self.connection_time)
write_fixed_string(file, self.top_version)
def __repr__(self):
return "Motion(%s, %d, %d, %s, %s, %f, %s)" % (self.name, self.frame_start, self.frame_end, self.next_move, self.connection_method, self.connection_time, self.top_version)
class Frame:
def __init__(self, index=None, messages=None, check=None):
self.index=index
self.messages=messages # Sempre uma lista de tamanho 3, do tipo String
self.check=check
def load(self, file, total):
self.messages = []
self.check = 0
self.index, = read_struct(file, '<I')
for i in range(3):
msg = read_fixed_string(file).decode('euc_kr')
self.messages.append(msg)
if total < 2 and self.messages[0] == '':
return
self.check, = read_struct(file, '<I')
def save(self, file, total):
write_struct(file, '<I', self.index)
if self.messages[0] != '':
self.check = 1
for i in range(3):
try:
write_fixed_string(file, self.messages[i])
except IndexError:
write_fixed_string(file, b'')
if total >= 2:
write_struct(check)
def __repr__(self):
return "Frame(%d, %s, %d)" % (self.index, ''.join(self.messages), self.check)
class FaceAnimation:
def __init__(self, group=None, name=None, material_name=None):
self.group=group
self.name=name
self.material_name=material_name
def load(self, file):
self.group, = read_struct(file, '<B')
self.name = read_struct(file, '<32s')[0].split(b'\x00')[0]
self.material_name = read_struct(file, '<32s')[0].split(b'\x00')[0]
def save(self, file):
write_struct(file, '<B', self.group)
write_struct(file, '<32s', self.name)
write_struct(file, '<32s', self.material_name)
class Position:
def __init__(self, time=None, position=None):
self.time=time
self.position=position
def load(self, file):
self.time, = read_struct(file, '<f')
self.position = read_struct(file, '<3f')
def save(self, file):
write_struct(file, '<4f', self.time, *self.position)
def __repr__(self):
return "Position(%f, %f, %f, %f)" % (self.time, *self.position)
class Rotation:
def __init__(self, time=None, rotation=None):
self.time=time
self.rotation=rotation
def load(self, file):
self.time, = read_struct(file, '<f')
self.rotation = read_struct(file, '<4f')
def save(self, file):
write_struct(file, '<5f', self.time, *self.rotation)
def __repr__(self):
return "Rotation(%f, %f, %f, %f, %f)" % (self.time, *self.rotation)
class Scaling:
def __init__(self, time=None, scaling=None):
self.time=time
self.scaling=scaling
def load(self, file):
self.time, = read_struct(file, '<f')
self.scaling = read_struct(file, '<3f')
def save(self, file):
write_struct(file, '<4f', self.time, *self.scaling)
def __repr__(self):
return "Scaling(%f, %f, %f, %f)" % (self.time, *self.scaling)
class FlagAnim:
def __init__(self, time=None, scale=None):
self.time=time
self.scale=scale
def load(self, file):
self.time, = read_struct(file, '<f')
self.scale, = read_struct(file, '<f')
def save(self, file):
write_struct(file, '<2f', self.time, self.scale)
def __repr__(self):
return "FlagAnim(%f, %f)" % (self.time, self.scale)
class Animation:
def __init__(self, bone_id=None, positions=None, rotations=None, scalings=None, flags=None, animTime=None):
self.bone_id=bone_id
self.positions=positions
self.rotations=rotations
self.scalings=scalings
self.flags=flags
self.animTime=animTime
def load(self, file):
self.positions = []
self.rotations = []
self.scalings = []
self.flags = []
self.animTime = 0.0
count, = read_struct(file, '<I')
for i in range(count):
position = Position()
position.load(file)
self.positions.append(position)
if len(self.positions) > 0:
self.animTime = max(self.animTime, max(self.positions, key=lambda pos: pos.time).time)
count, = read_struct(file, '<I')
for i in range(count):
rotation = Rotation()
rotation.load(file)
self.rotations.append(rotation)
if len(self.rotations) > 0:
self.animTime = max(self.animTime, max(self.rotations, key=lambda rot: rot.time).time)
count, = read_struct(file, '<I')
for i in range(count):
scaling = Scaling()
scaling.load(file)
self.scalings.append(scaling)
if len(self.scalings) > 0:
self.animTime = max(self.animTime, max(self.scalings, key=lambda scal: scal.time).time)
if compareVersions(gVersion, VERSION_1_3) >= 0:
count, = read_struct(file, '<I')
for i in range(count):
flag = FlagAnim()
flag.load(file)
self.flags.append(flag)
if len(self.flags) > 0:
self.animTime = max(self.animTime, max(self.flags, key=lambda flag: flag.time).time)
def save(self, file):
if self.bone_id > 0xF0:
write_struct(file, '<B', 0xFE)
write_struct(file, '<H', self.bone_id)
else:
write_struct(file, '<B', self.bone_id)
write_struct(file, '<I', len(self.positions))
for position in self.positions:
position.save(file)
write_struct(file, '<I', len(self.rotations))
for rotation in self.rotations:
rotation.save(file)
write_struct(file, '<I', len(self.scalings))
for scaling in self.scalings:
scaling.save(file)
if compareVersions(gVersion, VERSION_1_3) >= 0:
write_struct(file, '<I', len(self.flags))
for flag in self.flags:
flag.save(file)
def __repr__(self):
return "Animations(BoneId: %d, Time: %f, Positions: %d, Rotations: %d, Scalings: %d, Flags; %d)" % (
self.bone_id, self.animTime, len(self.positions), len(self.rotations), len(self.scalings), len(self.flags)
)
class SpecularValue:
def __init__(self, name=None, type=None, value=None):
self.name=name
self.type=type
self.value=value
def load(self, file):
self.name = read_fixed_string(file)
self.type, = read_struct(file, '<I')
if self.type == eSPECULAR_MATERIAL_TYPE.SMTL_BOOL:
self.value, = read_struct(file, '<I')
elif self.type == eSPECULAR_MATERIAL_TYPE.SMTL_INT:
self.value, = read_struct(file, '<i')
elif self.type == eSPECULAR_MATERIAL_TYPE.SMTL_FLOAT:
self.value, = read_struct(file, '<f')
elif self.type == eSPECULAR_MATERIAL_TYPE.SMTL_VECTOR2:
self.value = read_struct(file, '<2f')
elif self.type == eSPECULAR_MATERIAL_TYPE.SMTL_VECTOR3:
self.value = read_struct(file, '<3f')
elif self.type == eSPECULAR_MATERIAL_TYPE.SMTL_VECTOR4:
self.value = read_struct(file, "<4f")
elif self.type == eSPECULAR_MATERIAL_TYPE.SMTL_MATRIX:
self.value = read_struct(file, "<16f")
elif self.type == eSPECULAR_MATERIAL_TYPE.SMTL_TEXTURE:
self.value = read_fixed_string(file)
def save(self, file):
write_fixed_string(file, self.name)
write_struct(file, '<I', self.type)
if self.type == eSPECULAR_MATERIAL_TYPE.SMTL_BOOL:
write_struct(file, '<I', self.value)
elif self.type == eSPECULAR_MATERIAL_TYPE.SMTL_INT:
write_struct(file, '<i', self.value)
elif self.type == eSPECULAR_MATERIAL_TYPE.SMTL_FLOAT:
write_struct(file, '<f', self.value)
elif self.type == eSPECULAR_MATERIAL_TYPE.SMTL_VECTOR2:
write_struct(file, '<2f', *self.value)
elif self.type == eSPECULAR_MATERIAL_TYPE.SMTL_VECTOR3:
write_struct(file, '<3f', *self.value)
elif self.type == eSPECULAR_MATERIAL_TYPE.SMTL_VECTOR4:
write_struct(file, '<4f', *self.value)
elif self.type == eSPECULAR_MATERIAL_TYPE.SMTL_MATRIX:
write_struct(file, '<16f', *self.value)
elif self.type == eSPECULAR_MATERIAL_TYPE.SMTL_TEXTURE:
write_fixed_string(file, self.value)
def __repr__(self):
return ("SpecularValue(%s, %d" % (self.name, self.type)) + self.value
class SpecularMaterial:
def __init__(self, name=None, smtlvs=None):
self.name=name
self.smtlvs=smtlvs
def load(self, file):
self.smtlvs = []
self.name = read_fixed_string(file)
count, = read_struct(file, '<I')
for i in range(count):
sv = SpecularValue()
sv.load(file)
self.smtlvs.append(sv)
def save(self, file):
write_fixed_string(file, self.name)
write_struct(file, '<I', len(self.smtlvs))
for sv in self.smtlvs:
sv.save(file)
def __repr__(self):
return "SpacularMaterial(%d)" % (len(self.smtlvs))
class Texture:
def __init__(self, fn=None, flag=None, group=None, diffuse=None, handle=None):
self.fn = fn
self.flag = flag
self.group = group
self.diffuse = diffuse
self.handle = handle
def is_valid(self):
return self.fn is not None
def load(self, file):
# looks like Ntreev left us some stack garbage
self.fn = read_struct(file, '<32s')[0].split(b'\x00')[0]
self.flag, = read_struct(file, '<c')
self.group, = read_struct(file, '<B')
file.read(2) # align 4
self.diffuse, = read_struct(file, '<I')
self.handle, = read_struct(file, '<I')
def save(self, file):
write_struct(file, '<32s', self.fn)
write_struct(file, '<c', self.flag)
write_struct(file, '<B', self.group)
file.write("\x00\x00") # align 4
write_struct(file, '<I', self.diffuse)
write_struct(file, '<I', self.handle)
def __repr__(self):
return "Texture(%s)" % (self.fn)
class Bone:
def __init__(self, name=None, parent=None, matrix=None, float_v13=None):
self.name = name
self.parent = parent
self.matrix = matrix
self.float_v13 = float_v13
def is_valid(self):
return self.name is not None
def load(self, file):
self.name = read_cstr(file)
self.parent, = read_struct(file, '<B')
if self.parent == 0xFE:
self.parent, = read_struct(file, '<H')
if gFileType != FILE_APET and (gFileType == FILE_PET or gFileType == FILE_BPET or gFileType == FILE_MPET):
self.matrix = read_struct(file, '<12f')
if compareVersions(gVersion, VERSION_1_3) >= 0:
self.float_v13, = read_struct(file, "<f")
def save(self, file):
write_cstr(file, self.name)
if self.parent > 0xF0:
write_struct(file, '<BH', 0xFE, self.parent)
else:
write_struct(file, '<B', self.parent)
if gFileType != FILE_APET and (gFileType == FILE_PET or gFileType == FILE_BPET or gFileType == FILE_MPET):
write_struct(file, '<12f', *self.matrix)
if compareVersions(gVersion, VERSION_1_3) >= 0:
write_struct(file, '<f', self.float_v13)
def __repr__(self):
fmt = "Bone(%s, %d, [%f" + (", %f" * 11) + "])"
return fmt % (self.name, self.parent, *self.matrix)
class BoneWeight:
def __init__(self, weight=None, id=None):
self.weight = weight
self.id = id
def load(self, file):
self.weight, self.id = read_struct(file, '<2B')
if self.id == 0xFE:
self.id, = read_struct(file, '<H')
def save(self, file):
write_struct(file, '<B', self.weight)
if self.id > 0xF0:
write_struct(file, '<B', 0xFE)
write_struct(file, '<H', self.id)
else:
write_struct(file, '<B', self.id)
def __repr__(self):
return "BoneWeight(weight=%d, id=%d)" % (self.weight, self.id)
class MpetExtraValue:
def __init__(self, unknown=None, vertice_idx=None, vertice_len=None, poly_idx=None, poly_len=None):
self.unknown=unknown
self.vertice_idx=vertice_idx
self.vertice_len=vertice_len
self.poly_idx=poly_idx
self.poly_len=poly_len
def load(self, file):
self.unknown, = read_struct(file, '<I')
self.vertice_idx, = read_struct(file, '<I')
self.vertice_len, = read_struct(file, '<I')
self.poly_idx, = read_struct(file, '<I')
self.poly_len, = read_struct(file, '<I')
def save(self, file):
write_struct(file, '<I', self.unknown)
write_struct(file, '<I', self.vertice_idx)
write_struct(file, '<I', self.vertice_len)
write_struct(file, '<I', self.poly_idx)
write_struct(file, '<I', self.poly_len)
def __repr__(self):
return "MpetExtraVelue(unknown=%d, vertice_index=%d, vertice_len=%d, polygon_index=%d, polygon_length=%d)" % (
self.unknown, self.vertice_idx, self.vertice_len, self.poly_idx, self.poly_len
)
class Vertex:
def __init__(self, x=None, y=None, z=None, w=None, weight_global=None, bone_weights=None):
self.x, self.y, self.z, self.w = x, y, z, w
self.bone_weights = bone_weights
self.weight_global=weight_global
def load(self, file, is_mpet):
wsum = 0
self.bone_weights = []
self.x, self.y, self.z = read_struct(file, '<3f')
if is_mpet:
self.weight_global, = read_struct(file, '<f')
else:
self.weight_global = 1.0
# Bone weights continue until saturated.
while wsum < 0xFF:
weight = BoneWeight()
weight.load(file)
wsum += weight.weight
self.bone_weights.append(weight)
# Strangely, file leaves space for at least 2.
if len(self.bone_weights) < 2:
file.read(2)
def save(self, file, is_mpet):
write_struct(file, '<3f', self.x, self.y, self.z)
if is_mpet:
write_struct(file, '<f', self.weight_global)
for weight in self.bone_weights:
weight.save(file)
if len(self.bone_weights) < 2:
file.write("\x00\x00")
def __repr__(self):
return "Vertex(%f, %f, %f)" % (self.x, self.y, self.z)
class UVMapping:
def __init__(self, u=None, v=None):
self.u=u
self.v=v
def load(self, file):
self.u, self.v = read_struct(file, '<2f')
def save(self, file):
write_struct(file, '<2f', self.u, self.v)
def __repr__(self):
return "UVMapping(%f, %f)" % (self.u, self.v)
class PolygonIndex:
def __init__(self, index=None, nx=None, ny=None, nz=None, uvMapping=None):
self.index = index
self.nx, self.ny, self.nz = nx, ny, nz
self.uvMapping = uvMapping
def load(self, file):
self.uvMapping = []
self.index, = read_struct(file, '<I')
self.nx, self.ny, self.nz = read_struct(file, '<3f')
count = 1
if compareVersions(gVersion, VERSION_1_2) >= 0:
count, = read_struct(file, '<B')
for i in range(count):
uv = UVMapping()
uv.load(file)
self.uvMapping.append(uv)
def save(self, file):
write_struct(file, '<I', self.index)
write_struct(file, '<3f', self.nx, self.ny, self.nz)
if compareVersions(gVersion, VERSION_1_2) >= 0:
write_struct(file, '<B', len(self.uvMapping))
for uv in self.uvMapping:
uv.save(file)
elif len(self.uvMapping):
self.uvMapping[0].save(file)
else:
write_struct(file, '<2f', 0.0, 0.0)
def __repr__(self):
return "PolygonIndex(index=%d)" % self.index
class Polygon:
def __init__(self, indices=None):
self.indices = indices
def load(self, file):
self.indices = []
for i in range(3):
index = PolygonIndex()
index.load(file)
self.indices.append(index)
def save(self, file):
for index in self.indices:
index.save(file)
class Mesh:
def __init__(self, vertices=None, polygons=None, texmap=None, mpetexs=None, map_v12=None):
self.vertices = vertices
self.polygons = polygons
self.texmap = texmap
self.mpetexs=mpetexs
self.map_v12 = map_v12
def load(self, file, is_mpet):
self.vertices = []
self.polygons = []
self.texmap = []
self.mpetexs = []
self.map_v12 = []
if is_mpet:
num_mpetex, = read_struct(file, '<B')
for i in range(num_mpetex):
mpetEx = MpetExtraValue()
mpetEx.load(file)
print(mpetEx)
self.mpetexs.append(mpetEx)
# Vertices
num_vertices, = read_struct(file, '<I')
#print(num_vertices)
for i in range(num_vertices):
vertex = Vertex()
vertex.load(file, is_mpet)
#print(vertex.x, vertex.y, vertex.z)
self.vertices.append(vertex)
# Polygons
num_polygons, = read_struct(file, '<I')
for i in range(num_polygons):
polygon = Polygon()
polygon.load(file)
self.polygons.append(polygon)
# Mapping of polygons to textures
for i in range(num_polygons):
self.texmap.append(read_struct(file, '<B')[0])
if compareVersions(gVersion, VERSION_1_2) >= 0:
for i in range(num_polygons):
self.map_v12.append(read_struct(file, '<B')[0])
def save(self, file, is_mpet):
if is_mpet:
write_struct(file, '<B', len(self.mpetexs))
for mpetex in self.mpetexs:
mpetex.save(file)
write_struct(file, '<I', len(self.vertices))
for vertex in self.vertices:
vertex.save(file)
write_struct(file, '<I', len(self.polygons))
for polygon in self.polygons:
polygon.save(file)
for tex in self.texmap:
write_struct(file, '<B', tex)
if compareVersions(gVersion, VERSION_1_2) >= 0:
for mapv12 in self.map_v12:
write_struct(file, '<B', mapv12)
def __repr__(self):
return "Mesh(vertices=%s, polygons=%s)" % (
repr(self.vertices),
repr(self.polygons),
)