Repository navigation
Expand file tree
/
Copy path__init__.py
More file actions
2447 lines (2264 loc) · 103 KB
/
Copy path__init__.py
File metadata and controls
2447 lines (2264 loc) · 103 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
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
993
994
995
996
997
998
999
1000
import copy
import operator
bl_info = {
"name": "Star Wars Outlaws Mesh Tool",
"author": "AlexPo",
"location": "Scene Properties > Star Wars: Outlaws Mesh Tool Panel",
"version": (0, 0, 11),
"blender": (5, 0, 0),
"description": "Imports/exports skeletal meshes\n from Star Wars Outlaws's .mmb files",
"category": "Import-Export"
}
import bpy
import bmesh
from struct import unpack, pack
import numpy as np
import math
from mathutils import Matrix, Euler, Vector
from pathlib import Path
import os
import io
import shutil
class ByteReader:
@staticmethod
def int8(f):
b = f.read(1)
i = unpack('<b', b)[0]
return i
@staticmethod
def bool(f):
b = f.read(1)
i = unpack('<b', b)[0]
if i == 0:
return False
elif i == 1:
return True
else:
raise Exception("Byte at {v} wasn't a boolean".format(v=f.tell()))
@staticmethod
def uint8(f):
b = f.read(1)
i = unpack('<B', b)[0]
return i
@staticmethod
def int16(f):
return unpack('<h', f.read(2))[0]
@staticmethod
def uint16(f):
b = f.read(2)
i = unpack('<H', b)[0]
return i
@staticmethod
def hash(f):
b = f.read(8)
return b
@staticmethod
def guid(f):
return f.read(16)
@staticmethod
def int32(f):
b = f.read(4)
i = unpack('<i',b)[0]
return i
@staticmethod
def uint32(f):
b = f.read(4)
i = unpack('<I',b)[0]
return i
@staticmethod
def uint64(f):
b = f.read(8)
i = unpack('<Q',b)[0]
return i
@staticmethod
def int64(f):
b = f.read(8)
i = unpack('<q', b)[0]
return i
@staticmethod
def string(f,length):
b = f.read(length)
return "".join(chr(x) for x in b)
@staticmethod
def name(f):
return br.string(f,br.uint16(f))
@staticmethod
def path(f):
b = f.read(4)
length = unpack('<i', b)[0]
b = f.read(length)
return "".join(chr(x) for x in b)
@staticmethod
def hashtext(f):
b = f.read(4)
length = unpack('<i', b)[0]
f.seek(4,1)
b = f.read(length)
return "".join(chr(x) for x in b)
@staticmethod
def float(f):
b = f.read(4)
fl = unpack('<f',b)[0]
return fl
@staticmethod
def vector3(f):
b = f.read(12)
return unpack('<fff', b)
@staticmethod
def dvector3(f):
#double vector 3
b = f.read(24)
return unpack('<ddd', b)
@staticmethod
def vector4(f):
b = f.read(16)
return unpack('<ffff', b)
@staticmethod
def int16_norm(f):
i = unpack('<H', f.read(2))[0]
v = i ^ 2**15
v -= 2**15
v /= 2**15 - 1
return v
@staticmethod
def uint16_norm(f):
uint16 = unpack('<H', f.read(2))[0]
return uint16 / ((2 ** 16) - 1)
@staticmethod
def uint8_norm(f):
uint8 = unpack('<B', f.read(1))[0]
maxint = (2 ** 8)-1
return uint8 / maxint
@staticmethod
def int8_norm(f):
int8 = unpack('<B', f.read(1))[0]
v = int8 ^ 2**7
v -= 2**7
v /= 2**7 -1
return v
@staticmethod
def X10Y10Z10W2_normalized(f):
i = unpack('<I', f.read(4))[0] # get 32bits of data
x = i >> 0
x = ((x & 0x3FF) ^ 512) - 512
y = i >> 10
y = ((y & 0x3FF) ^ 512) - 512
z = i >> 20
z = ((z & 0x3FF) ^ 512) - 512
w = i >> 30
w = w & 0x1
vectorLength = math.sqrt(x ** 2 + y ** 2 + z ** 2)
# # print(x,y,z)
if vectorLength != 0:
x /= vectorLength
y /= vectorLength
z /= vectorLength
return [x, y, z, w]
@staticmethod
def matrix_4x4(f):
row1 = []
row2 = []
row3 = []
row4 = []
for i in range(4):
for c in range(4):
value = br.float(f)
if c == 0:
row1.append(value)
if c == 1:
row2.append(value)
if c == 2:
row3.append(value)
if c == 3:
row4.append(value)
# print(Matrix((row1,row2,row3,row4)))
matrix = Matrix((row1, row2, row3, row4))#.inverted()
return matrix
class BytePacker:
@staticmethod
def int8(v):
return pack('<b', v)
@staticmethod
def uint8(v):
return pack('<B', v)
@staticmethod
def uint8_norm(v):
if 0.0 <= v <= 1.001:
i = max(0,min(int(v * ((2 ** 8)-1)),255))
else:
raise Exception("Couldn't normalize value as uint8Norm, "
"it wasn't between 0.0 and 1.0. Unknown max value."
+str(v))
return pack('<B', i)
@staticmethod
def int8_norm(v):
if -1.001 <= v <= 1.001:
i = max(-255, min(int(v * ((2 ** 7) - 1)), 255))
else:
raise Exception("Couldn't normalize value as uint8Norm, "
"it wasn't between -1.0 and 1.0. Unknown max value."
+ str(v))
return pack('<b', i)
@staticmethod
def int16(v):
return pack('<h', v)
@staticmethod
def uint16(v):
return pack('<H', v)
@staticmethod
def int16_norm(v):
# print(v)
if -1.0 <= v <= 1.0:
# if v >= 0:
# v = int(abs(v) * (2 ** 15))
# else:
# v = 2 ** 16 - int(abs(v) * (2 ** 15))
v = max(min(int(v * (2 ** 15)),32767), -32768)
else:
raise Exception("Couldn't normalize value as int16Norm, it wasn't between -1.0 and 1.0. Unknown max value.")
return pack('<h', v)
@staticmethod
def uint16_norm(v, exp=16, max_value=0xFFFF):
if 0.0 <= v <= 1.0:
i = max(0, min(int(round(v * ((2 ** exp) - 1))), max_value))
else:
raise Exception("Couldn't normalize value as uint16Norm, it wasn't between 0.0 and 1.0. Unknown max value. " + str(v))
return pack('<H', i)
@staticmethod
def float16(v):
f32 = np.float32(v)
f16 = f32.astype(np.float16)
b16 = f16.tobytes()
return b16
@staticmethod
def int32(v):
return pack('<i', v)
@staticmethod
def uint32(v):
return pack('<I', v)
@staticmethod
def uint64(v):
return pack('<Q', v)
@staticmethod
def int64(v):
return pack('<q', v)
@staticmethod
def float(v):
return pack('<f', v)
@staticmethod
def X10Y10Z10W2(x,y,z,w):
if x >= 0:
x = int(abs(x) * 2 ** 9)
else:
x = 2**10 - int(abs(x) * 2 ** 9)
if y >= 0:
y = int(abs(y) * 2 ** 9)
else:
y = 2**10 - int(abs(y) * 2 ** 9)
if z >= 0:
z = int(abs(z) * 2 ** 9)
else:
z = 2**10 - int(abs(z) * 2 ** 9)
w = int(w)
x = (abs(x) & 0x3FF)
y = (abs(y) & 0x3FF) << 10
z = (abs(z) & 0x3FF) << 20
w = (abs(w) & 0x3) << 30
v = x | y | z | w
return pack("<I", v)
@staticmethod
def matrix_4x4(matrix:Matrix):
out_matrix = b''
for i in range(4):
for c in range(4):
if abs(matrix[c][i]) < 0.0001:
out_matrix += bp.float(0.0)
else:
out_matrix += bp.float(matrix[c][i])
return out_matrix
br = ByteReader
bp = BytePacker
def CopyFile(read,write,offset,size,buffer_size=500000):
read.seek(offset)
chunks = size // buffer_size
for o in range(chunks):
write.write(read.read(buffer_size))
write.write(read.read(size%buffer_size))
def get_merged_mmb(mmb):
files = []
if str(mmb).endswith("mmb"):
files.append(mmb)
else:
i = 0
while True:
current_file = f"{str(mmb)[:-1]}{i}"
if os.path.isfile(current_file):
files.append(current_file)
i += 1
else:
break
f = io.BytesIO()
for file_dir in files:
with open(file_dir, 'rb') as file:
f.write(file.read())
return f
class Asset:
def __init__(self):
self.magic = ""
self.version = 0
self.size = 0
def parse(self,f):
self.magic = br.string(f,3)
self.version = br.uint8(f)
self.size = br.uint32(f)
f.seek(4,1)
def write(self,f):
f.seek(4)
f.write(bp.uint32(self.size))
class SkeletalMeshAsset(Asset):
class Mesh:
class LOD:
def __init__(self, parent_mesh,index):
self.start_offset = 0
self.parent_mesh:SkeletalMeshAsset.Mesh = parent_mesh
self.index = index
self.vertex_count = 0
self.index_count = 0
self.size_a = 0
self.vertex_data_offset_a = 0
self.vertex_data_offset_b = 0
self.face_block_offset = 0
self.data_offset = 0
self.data_size = 0
self.lod_screen_size = 0.0
self.data_x_offset = 0
self.data_x_size = 0
self.data_y_offset = 0
self.data_y_size = 0
self.local_vertex_stride = 0
self.lod_info_type = 0
self.is_header_lod = False
self.vertex_end_bytes = None
self.normals_end_bytes = None
self.faces_end_bytes = None
def parse(self, f, lod_info_type = 0):
self.lod_info_type = lod_info_type
self.start_offset = f.tell()
self.vertex_count = br.uint32(f)
self.index_count = br.uint32(f)
self.size_a = br.uint32(f) # seems to be face_block_offset divided by 2
self.vertex_data_offset_a = br.uint32(f)
self.vertex_data_offset_b = br.uint32(f)
self.face_block_offset = br.uint32(f)
self.data_offset = br.uint32(f)
self.data_size = br.uint32(f)
self.lod_screen_size = br.float(f) # not confirmed screen size
if self.vertex_count > 0:
if lod_info_type == 0:
pass
elif lod_info_type == 12:
unk1 = br.uint32(f)
self.data_x_offset = br.uint32(f)
self.data_x_size = br.uint32(f)
unk2 = br.uint32(f)
self.data_y_offset = br.uint32(f)
self.data_y_size = br.uint32(f)
self.local_vertex_stride = int(self.data_y_size / self.vertex_count)
print("New Vertex Stride = ", self.local_vertex_stride)
else:
f.seek(28,1)
if self.data_offset < self.parent_mesh.parent_sk_mesh.size:
self.is_header_lod = True
print("Lod ", self.index, "is in header.")
def write(self, f):
f.seek(self.start_offset)
f.write(bp.uint32(self.vertex_count))
# print("Written vertex count: ",self.vertex_count)
f.write(bp.uint32(self.index_count))
f.write(bp.uint32(int(self.face_block_offset / 2)))
f.write(bp.uint32(self.vertex_data_offset_a))
f.write(bp.uint32(self.vertex_data_offset_b))
f.write(bp.uint32(self.face_block_offset))
f.write(bp.uint32(self.data_offset))
f.write(bp.uint32(self.data_size))
f.write(bp.float(self.lod_screen_size))
def write_data_offset(self,f):
f.seek(self.start_offset + 24)
f.write(bp.uint32(self.data_offset))
def gather_extra_bytes(self,f):
offset = f.tell()
f.seek(self.data_offset)
real_vertex_size = self.vertex_count * self.parent_mesh.vertex_stride
if self.vertex_data_offset_a == self.vertex_data_offset_b:
extra_bytes_size = self.face_block_offset - self.vertex_data_offset_a - real_vertex_size
else:
extra_bytes_size = self.vertex_data_offset_b - self.vertex_data_offset_a - real_vertex_size
f.seek(real_vertex_size, 1)
# print(f.tell())
if extra_bytes_size > 0:
self.vertex_end_bytes = f.read(extra_bytes_size)
else:
print("No Extra Vertex bytes: ", extra_bytes_size)
# print("Vertex Extra Bytes:",self.vertex_end_bytes)
if not self.vertex_data_offset_a == self.vertex_data_offset_b:
real_normals_size = self.vertex_count * self.parent_mesh.normals_stride
extra_bytes_size = self.face_block_offset - self.vertex_data_offset_b - real_normals_size
f.seek(real_normals_size, 1)
# print(f.tell())
if extra_bytes_size > 0:
self.normals_end_bytes = f.read(extra_bytes_size)
else:
print("No Extra Normals bytes: ", extra_bytes_size)
# print("Normal Extra Bytes:",self.normals_end_bytes)
real_face_size = self.index_count * 2
size_without_face = self.face_block_offset - self.vertex_data_offset_a
extra_bytes_size = self.data_size - size_without_face - real_face_size
f.seek(real_face_size, 1)
# print(f.tell())
if extra_bytes_size > 0:
self.faces_end_bytes = f.read(extra_bytes_size)
# print("Face Extra Bytes:",self.faces_end_bytes)
else:
print("No Extra Face bytes: ", extra_bytes_size)
f.seek(offset)
def get_vertex_positions(self,raw_mesh_file):
vertices = []
if self.local_vertex_stride > 0:
stride = self.local_vertex_stride
else:
stride = self.parent_mesh.vertex_stride
print(stride)
if self.data_y_offset != 0:
print("Vertex Data is in mmb file.")
SWOMT = bpy.context.scene.SWOMT
file = SWOMT.AssetPath
f = open(file,"rb")
print(self.data_y_offset)
f.seek(self.data_y_offset)
else:
f = raw_mesh_file
f.seek(self.vertex_data_offset_a)
pos = (0.0,0.0,0.0)
for v in range(self.vertex_count):
stride_start = f.tell()
if self.parent_mesh.position_type == 0:
x = br.int16_norm(f)
y = br.int16_norm(f)
z = br.int16_norm(f)
w = br.int16(f)
pos = (x*w,y*w,z*w)
elif self.parent_mesh.position_type == 1:
x = br.float(f)
y = br.float(f)
z = br.float(f)
pos = (x,y,z)
f.seek(stride_start + stride)
if v == 0:
print(f.tell())
vertices.append(pos)
return vertices
def get_bone_weights(self, raw_mesh_file):
"""
Read bone weights while preserving fixed slot alignment.
The vertex buffer may physically store more weight/index slots than the
declared logical weight count. Always read the full physical layout so
weight bytes and index bytes remain aligned.
"""
bone_weights = []
if self.lod_info_type == 12:
stride = self.local_vertex_stride
SWOMT = bpy.context.scene.SWOMT
file = SWOMT.AssetPath
f = open(file, "rb")
f.seek(self.data_y_offset)
else:
stride = self.parent_mesh.vertex_stride
f = raw_mesh_file
f.seek(self.vertex_data_offset_a)
pos_length = self.parent_mesh.get_vertex_position_length()
layout = self.parent_mesh.get_vertex_weight_storage_layout()
weight_type = layout['weight_type']
index_type = layout['index_type']
storage_weight_count = layout['count']
if self.parent_mesh.is_rigid_single_binding_float3_layout():
print(
f"Rigid single-binding weight layout detected for "
f"{self.parent_mesh.name}_LOD{self.index}; "
f"assigning all vertices to mesh bone 0 with weight 1.0."
)
for v in range(self.vertex_count):
bone_weights.append({0: 1.0})
return bone_weights
for v in range(self.vertex_count):
vertex_stride_start = f.tell()
f.seek(pos_length, 1)
weights = []
for w in range(storage_weight_count):
if weight_type == 'uint16_norm':
weights.append(br.uint16_norm(f))
else:
weights.append(br.uint8_norm(f))
indices = []
for i in range(storage_weight_count):
if index_type == 'uint16':
indices.append(br.uint16(f))
else:
indices.append(br.uint8(f))
iw = {}
for i in range(storage_weight_count):
if weights[i] > 0.0:
iw[indices[i]] = weights[i]
f.seek(vertex_stride_start + stride)
if v == 0:
print("Weight vertex info vvvvvvvvvvvvvv")
print(f.tell())
print(vertex_stride_start)
print(weight_type, storage_weight_count, index_type)
print(iw)
bone_weights.append(iw)
return bone_weights
def get_triangles(self,raw_mesh_file):
# Bounds-safe triangle index reader.
tris = []
f = raw_mesh_file
f.seek(self.face_block_offset)
print(f.tell())
declared_index_count = self.index_count
index_count = declared_index_count
# In the extracted mesh file, face_block_offset is an offset into
# that extracted stream. data_size is not always a reliable end
# offset for header-resident/proxy LODs, so only use data_size as
# a clamp when it produces a positive amount of face data.
available_face_bytes = None
available_index_count = None
if self.data_size is not None and self.face_block_offset is not None:
candidate_face_bytes = self.data_size - self.face_block_offset
if candidate_face_bytes > 0:
available_face_bytes = candidate_face_bytes
available_index_count = available_face_bytes // 2
# As a fallback clamp, use the remaining bytes in the extracted
# mesh stream. This prevents EOF over-reads while not treating
# data_size == face_block_offset as zero available triangle data.
try:
stream_size = len(f.getbuffer())
except Exception:
old_pos = f.tell()
f.seek(0, 2)
stream_size = f.tell()
f.seek(old_pos)
stream_remaining_face_bytes = max(0, stream_size - self.face_block_offset)
stream_remaining_index_count = stream_remaining_face_bytes // 2
# Legacy fallback for meshes where index_count equals vertex_count.
# Only use the size_a-derived count if it fits inside known
# available face data. This prevents *_CLOTH_RENDER meshes from
# over-reading, e.g. index_count=3, size_a=240 -> 60 guessed
# indices when only one proxy triangle is declared.
if self.vertex_count == self.index_count:
fallback_index_count = int(self.size_a / 4)
print(fallback_index_count, self.size_a)
fits_lod_face_data = (
available_index_count is not None and
fallback_index_count <= available_index_count
)
fits_stream_data = fallback_index_count <= stream_remaining_index_count
use_fallback = (
fallback_index_count > declared_index_count and
fallback_index_count % 3 == 0 and
fits_lod_face_data and fits_stream_data
)
if use_fallback:
print(f"Using size_a-derived triangle index count: {fallback_index_count}")
index_count = fallback_index_count
else:
print(f"Using declared triangle index count: {declared_index_count}")
# Clamp to LOD-local face bytes only when that number is known
# and positive. Always also clamp to remaining stream bytes.
if available_index_count is not None and index_count > available_index_count:
print(
f"WARNING: triangle index count {index_count} exceeds "
f"available LOD face data {available_index_count}; clamping."
)
index_count = available_index_count
if index_count > stream_remaining_index_count:
print(
f"WARNING: triangle index count {index_count} exceeds "
f"remaining stream data {stream_remaining_index_count}; clamping."
)
index_count = stream_remaining_index_count
tris_count = int(index_count / 3)
print("Triangles Count:", tris_count)
for i in range(tris_count):
consumed_from_stream = f.tell() - self.face_block_offset
if stream_remaining_face_bytes - consumed_from_stream < 6:
print(
f"WARNING: stopping triangle read early at triangle {i}; "
f"only {stream_remaining_face_bytes - consumed_from_stream} stream bytes remain."
)
break
f1 = br.uint16(f)
f2 = br.uint16(f)
f3 = br.uint16(f)
tris.append((f1,f2,f3))
return tris
def get_normals_size(self):
if self.parent_mesh.normal_type == 'int8_norm':
return 8
else:
return 28
def get_normals(self,raw_mesh_file):
normals = []
stride = self.parent_mesh.normals_stride
f = raw_mesh_file
f.seek(self.vertex_data_offset_b)
v = Vector((0.0,0.0,1.0))
for i in range(self.vertex_count):
stride_start = f.tell()
if self.parent_mesh.normal_type == "int8_norm":
x = br.int8_norm(f) *-1
y = br.int8_norm(f)
z = br.int8_norm(f)
w = br.int8(f)
v = Vector((x*w, y*w, z*w)).normalized()
v.negate() # TODO not sure about this
elif self.parent_mesh.normal_type == "float":
x = br.float(f) *-1
y = br.float(f)
z = br.float(f)
v = Vector((x,y,z)).normalized()
f.seek(stride_start + stride)
normals.append(v)
return normals
def get_uvs(self,raw_mesh_file, index=0):
"""
Seeks to Lod.vertex_data_offset_b and reads all UV data.
:param raw_mesh_file: file that is exported by SkeletalMeshAsset.Mesh.extract_mesh_file()
:return: a List of Tuples containing 2 floats as UV coordinates.
"""
uvs = []
color_count = self.parent_mesh.color_count
f = raw_mesh_file
print(f'Load Info Type: {self.lod_info_type}')
if self.lod_info_type == 12:
stride = self.parent_mesh.vertex_stride
f.seek(self.vertex_data_offset_a)
for i in range(self.vertex_count):
stride_start = f.tell()
f.seek(4 * color_count, 1)
u = br.int16_norm(f)
v = br.int16_norm(f)
f.seek(stride_start + stride)
uvs.append((u,v))
else:
stride = self.parent_mesh.normals_stride
f.seek(self.vertex_data_offset_b)
for i in range(self.vertex_count):
stride_start = f.tell()
f.seek(self.get_normals_size(), 1) #skip normals
f.seek(4 * color_count, 1) #skip color
f.seek(index * 4, 1) # skip previous uv
if index == 1 and stride - (f.tell() - stride_start) == 8: #TODO this is guesswork
u = br.float(f)
v = br.float(f)
else:
u = br.int16_norm(f)
v = br.int16_norm(f)
f.seek(stride_start + stride)
uvs.append((u,v))
return uvs
def get_color(self,raw_mesh_file, index=0):
"""
Seeks to Lod.vertex_data_offset_b and reads all Color data.
:param raw_mesh_file: file that is exported by SkeletalMeshAsset.Mesh.extract_mesh_file()
:return: a List of Tuples containing 4 floats as RGBA coordinates.
"""
colors = []
f = raw_mesh_file
if self.lod_info_type == 12:
stride = self.parent_mesh.vertex_stride
f.seek(self.vertex_data_offset_a)
for i in range(self.vertex_count):
stride_start = f.tell()
f.seek(index * 4, 1) #skip previous color
r = br.uint8_norm(f)
g = br.uint8_norm(f)
b = br.uint8_norm(f)
a = br.uint8_norm(f)
f.seek(stride_start + stride)
colors.append((r,g,b,a))
else:
stride = self.parent_mesh.normals_stride
f.seek(self.vertex_data_offset_b)
for i in range(self.vertex_count):
stride_start = f.tell()
f.seek(self.get_normals_size(), 1) #skip normals
f.seek(index * 4, 1) #skip previous color
r = br.uint8_norm(f)
g = br.uint8_norm(f)
b = br.uint8_norm(f)
a = br.uint8_norm(f)
f.seek(stride_start + stride)
colors.append((r,g,b,a))
return colors
def write_vertex_position(self,file,pos=(0.0,0.0,0.0),scale=2):
"""
Writes a single vertex position into file at the current position and skips to the end of stride.
:param file: file to write on
:param pos: (x,y,z)
:return:
"""
f = file
x = pos[0]
y = pos[1]
z = pos[2]
if self.local_vertex_stride > 0:
stride = self.local_vertex_stride
else:
stride = self.parent_mesh.vertex_stride
stride_start = f.tell()
if self.parent_mesh.position_type == 0:
f.write(bp.int16_norm(x / scale))
f.write(bp.int16_norm(y / scale))
f.write(bp.int16_norm(z / scale))
f.write(bp.int16(scale))
elif self.parent_mesh.position_type == 1:
f.write(bp.float(x))
f.write(bp.float(y))
f.write(bp.float(z))
f.seek(stride_start + stride)
def __init__(self,parent_sk_mesh,index = 0):
self.parent_sk_mesh:SkeletalMeshAsset = parent_sk_mesh
if self.parent_sk_mesh.bone_count > 0xFF:
self.vertex_weight_index_type = 'uint16' # 1: uint16
else:
self.vertex_weight_index_type = 'uint8' # 0: uint8
self.vertex_weight_type = 'uint8_norm'
self.index = index
self.name = ""
self.lod_count_offset = 0
self.binding_count_offset = 0
self.binding_count = 0
self.lod_count = 0
self.lods = []
self.weight_count = 0
self.vertex_stride = 0
self.normals_stride = 0
self.mesh_bones = {}
self.extra_bones = [] # bones that weren't in mesh_bones but are needed for mod mesh.
self.real_bone_indices_to_mesh_bones = {}
self.color_count = 0
self.uv_count = 0
self.normal_type = 'float' # 0:int8_norm 1:float
self.position_type = 0 # 0:int16_norm 1:float
self.end_bytes = None # bytes from the end of the last LOD to the end of the mesh section.
self.mesh_file = None # BytesIO of reversed ordered LOD mesh data.
def get_vertex_position_length(self):
if self.position_type == 0:
return 8
elif self.position_type == 1:
return 12
return 12
def is_rigid_single_binding_float3_layout(self):
"""Return True for rigid accessory meshes using float3 positions in a 16-byte stride."""
return (self.vertex_stride == 16 and self.weight_count == 1 and
self.binding_count == 1 and self.normal_type == "float")
def get_vertex_weight_storage_layout(self):
"""
Return the physical weight/index layout implied by the vertex stride.
Some meshes declare a logical weight count that is smaller than the
physical number of weight/index slots stored in the vertex buffer. Some
other meshes are ambiguous enough that the old heuristic guessed
uint16_norm weights even though the physical stride matches packed
uint8_norm weights plus uint8 indices.
"""
if self.is_rigid_single_binding_float3_layout():
return {
'count': 1,
'weight_type': 'rigid_single_binding',
'index_type': self.vertex_weight_index_type,
'weight_unit': 0,
'index_unit': 0,
}
index_type = self.vertex_weight_index_type
index_unit = 2 if index_type == 'uint16' else 1
pos_length = self.get_vertex_position_length()
remaining = self.vertex_stride - pos_length
# Some meshes use uint8 mesh-local bone indices even when the full
# skeleton has more than 255 bones. If the declared uint16 index layout
# cannot physically fit in the vertex stride, fall back to packed
# uint8_norm weights + uint8 indices and derive the physical slot count
# from the stride. Example: chain_mesh, stride=20, pos_len=12,
# declared weight_count=3 -> remaining=8 bytes, which cannot fit
# 3x uint8 weights + 3x uint16 indices (=9 bytes), but fits
# 4x uint8 weights + 4x uint8 indices exactly.
if index_type == 'uint16' and self.weight_count > 0 and remaining > 0:
declared_uint16_size = (self.weight_count * 1) + (self.weight_count * 2)
uint8_slot_size = 2 # uint8_norm weight + uint8 mesh-local bone index
if declared_uint16_size > remaining and remaining % uint8_slot_size == 0:
uint8_capacity = remaining // uint8_slot_size
if uint8_capacity >= self.weight_count and uint8_capacity <= 8:
print(f"Detected uint8 mesh-local index layout for {self.name}: "
f"declared index_type=uint16 cannot fit stride, "
f"using {uint8_capacity}xuint8_norm+uint8 slots.")
return {
'count': uint8_capacity,
'weight_type': 'uint8_norm',
'index_type': 'uint8',
'weight_unit': 1,
'index_unit': 1,
}
# If the parser guessed uint16 weights from an odd 3-bytes-per-declared-slot
# layout, also test the common packed uint8 weights + indices layout.
alt_slot_size = 1 + index_unit
if self.weight_count > 1 and remaining > 0 and alt_slot_size > 0 and remaining % alt_slot_size == 0:
alt_capacity = remaining // alt_slot_size
if self.vertex_weight_type == 'uint16_norm' and alt_capacity >= self.weight_count and alt_capacity <= 8:
print(f"Detected packed uint8 weight layout for {self.name}: declared/guessed {self.weight_count}x{self.vertex_weight_type}+{index_type}, stride fits {alt_capacity}xuint8_norm+{index_type}.")
return {'count': alt_capacity, 'weight_type': 'uint8_norm', 'index_type': index_type, 'weight_unit': 1, 'index_unit': index_unit}
weight_type = self.vertex_weight_type
weight_unit = 2 if weight_type == 'uint16_norm' else 1
slot_size = weight_unit + index_unit
count = self.weight_count
if self.weight_count > 1 and remaining > 0 and slot_size > 0 and remaining % slot_size == 0:
capacity = remaining // slot_size
if capacity >= self.weight_count and capacity <= 8:
if capacity != self.weight_count:
print(f"Detected extra physical weight slots for {self.name}: declared weight_count={self.weight_count}, stride stores {capacity} slots.")
count = capacity
return {'count': count, 'weight_type': weight_type, 'index_type': index_type, 'weight_unit': weight_unit, 'index_unit': index_unit}
def get_vertex_weight_storage_count(self):
return self.get_vertex_weight_storage_layout()['count']
def get_vertex_weight_storage_type(self):
return self.get_vertex_weight_storage_layout()['weight_type']
def parse(self, f):
mesh_start_offset = f.tell()
self.name = br.name(f)
print("\n" + "-" * 72)
print(f"LOAD MESH START: {self.name} (mesh_index={self.index})")
print("-" * 72)
print("Mesh Start Offset:", mesh_start_offset)
f.seek(48, 1) # some kind of matrix
f.seek(1, 1)
x_count = br.uint16(f)
f.seek(4 * x_count, 1)
if self.parent_sk_mesh.version != 17:
y_count = br.uint16(f)
f.seek(4 * y_count, 1)
else:
y_count = 0
print("Binding Count Offset:", f.tell())
self.binding_count_offset = f.tell()
self.binding_count = br.uint16(f)
for b in range(self.binding_count):
matrix = br.matrix_4x4(f)
bone_index = br.uint16(f)
self.parent_sk_mesh.bones[bone_index].set_mesh_matrix(matrix)
self.mesh_bones[bone_index] = matrix
self.real_bone_indices_to_mesh_bones[bone_index] = b
# print(bone_index, " = ", b)
if self.parent_sk_mesh.version != 17:
f.seek(2, 1)
else:
f.seek(1,1)
flag_1, flag_2, flag_3 = 0, 0, 0
if self.parent_sk_mesh.version != 17:
if y_count > 0:
flag_1 = br.uint8(f)
flag_2 = br.uint8(f)
flag_3 = br.uint8(f)
lod_info_type = br.uint16(f)
self.lod_count_offset = f.tell()
print("Lod Count Offset: ",self.lod_count_offset)
self.lod_count = br.uint8(f)
f.seek(4, 1)
for l in range(self.lod_count):
lod = self.LOD(self,l)
lod.parse(f, lod_info_type)
self.lods.append(lod)
print("LOD Info End Offset:", f.tell())
#this section could be wrong
self.uv_count = br.uint8(f)
f.seek(4*self.uv_count,1)
self.color_count = br.uint8(f)
f.seek(4*self.color_count,1)
self.weight_count = br.uint32(f)
count_c = br.uint8(f)
f.seek(4*count_c,1)
if lod_info_type == 12:
f.seek(2,1)
print("Vertex Stride Offset:", f.tell())
self.vertex_stride = br.uint16(f)
self.normals_stride = br.uint16(f)
if lod_info_type == 12:
if flag_3 == 1:
f.seek(14, 1)
pca_count = br.uint32(f)
print("PCA Count : ",pca_count)
for pca in range(pca_count):
pca_length = br.uint16(f)
f.seek(pca_length, 1)
f.seek(4, 1)
else:
f.seek(18, 1)
else:
f.seek(20, 1)
print("PCA End Offset:", f.tell())
# Gather extra bytes at the end of each LOD's vertex/normal/face data.
# Must be done after getting stride lengths.
for l in self.lods:
if l.vertex_count > 0:
l.gather_extra_bytes(f)
# guessing the type of normal data int8 or floats
possible_normal_length = self.normals_stride - (4 * self.uv_count) - (4 * self.color_count)
if possible_normal_length > 16:
self.normal_type = "float"
else:
self.normal_type = "int8_norm"
# guessing the type of vertex position data int16 or floats
if self.vertex_stride - 16 == 8 or self.vertex_stride - 8 == 8 or (self.vertex_stride == 12 and self.weight_count == 1):
self.position_type = 0 #int16
position_length = 8
elif self.vertex_stride - 16 == 12 or self.vertex_stride - 8 == 12:
self.position_type = 1 #float
position_length = 12
else:
self.position_type = 1 #float
position_length = 12
# Rigid accessory/metal meshes can use a compact 16-byte vertex layout:
# float3 position + 4 trailing bytes. With weight_count=1 and a single
# mesh binding, stride-only heuristics can misclassify this as
# int16_norm+scale and collapse the mesh in Blender.
if self.is_rigid_single_binding_float3_layout():
if self.position_type != 1:
print(f"Detected rigid float3 position layout for {self.name}: "
f"stride=16, weight_count=1, binding_count=1, normals=float.")