-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
1084 lines (848 loc) · 38.4 KB
/
__init__.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
993
994
995
996
997
998
999
1000
bl_info = {
"name": "Alan Wake 2 .binFBX Importer",
"author": "Mike2023, erik945, DKDave, Volfix, riverence, Cheturbator",
"version": (2, 0, 0),
"blender": (3, 5, 0),
"location": "File > Import > Alan Wake 2 Mesh (.binFBX)",
"description": "Import binFBX meshes from Alan Wake 2",
"warning": "",
"wiki_url": "",
"tracker_url": "",
"category": "Import-Export"}
import bpy
import struct
import math
from mathutils import Matrix, Quaternion, Vector
import os
import sys
icons_dir = os.path.join(os.path.dirname(__file__), "icons")
custom_icon_col = {}
def import_binfbx(context, file_path, import_rig, file_structure, smooth):
print("reading mesh...")
target_lod = 0 # Target LOD value
if file_structure and import_rig:
skel_path = file_path.replace('data_pc', 'data')
skel_path = skel_path.replace('.binfbx', '.binskeleton')
elif import_rig and not file_structure:
skel_path = file_path.replace('.binfbx', '.binskeleton')
# Define offset values based on filename
mltMsh = 5
mesh_skeleton_offset = 0x10
table_offset = 0
lod0_offset = 0
# Check the file name and assign offset values
# if "typewriter.binfbx" in file_path:
# table_offset = 0x75FB
# lod0_offset = 0x28D7CC
# block_offset = 119
# vertex_size = 12
# if "plaza_alice_wake_statue_a.binfbx" in file_path:
# table_offset = 0xF0
# lod0_offset =
# block_offset = 115
# vertex_size = 8
# if "typewriter.binfbx" in file_path:
# table_offset = 0x75FB
# lod0_offset = 0x28D7CC
if "subway_train_crush_sim.binfbx" in file_path:
table_offset = 0x523DCA
lod0_offset = 0x5242EF
mesh_skeleton_offset = 0x4AEE7C
# if "dark_presence_intro_paper_simulation.binfbx" in file_path:
# table_offset = 0x5D8D1
# lod0_offset = 0
# if "clicker_a.binfbnx" in file_path:
# table_offset = 0xA0
# lod0_offset =
if "leadpipe_default_publish.binfbx" in file_path:
table_offset = 0x353
lod0_offset = 0xE685
if "flashbang_default_publish.binfbx" in file_path:
table_offset = 0x68D
lod0_offset = 0x70C
if "casey_pistol_default_publish.binfbx" in file_path:
table_offset = 0xB09
lod0_offset = 0xB8C
if "crossbow_default_publish.binfbx" in file_path:
table_offset = 0x1D86
lod0_offset = 0x1FF5
if "crossbow_default_publish_physx.binfbx" in file_path:
table_offset = 0x2CFC
lod0_offset = 0x2F6B
if "crossbow_double_publish_physx.binfbx" in file_path:
table_offset = 0x41B1
lod0_offset = 0x149E93
if "doublebarrelshotgun_default_publish.binfbx" in file_path:
table_offset = 0x1478
lod0_offset = 0x1576
if "fbiservicepistol_compensator_publish.binfbx" in file_path:
table_offset = 0x573
lod0_offset = 0x5F2
if "fbiservicepistol_default_publish.binfbx" in file_path:
table_offset = 0xD12
lod0_offset = 0xE10
if "fbiservicepistol_magazine_extended_publish.binfbx" in file_path:
table_offset = 0x613
lod0_offset = 0x709
if "flare_default_publish.binfbx" in file_path:
table_offset = 0x4DE
lod0_offset = 0x561
if "flaregun_default_publish.binfbx" in file_path:
table_offset = 0x6ED
lod0_offset = 0x76C
if "flashlight_default_publish.binfbx" in file_path:
table_offset = 0x1920D
lod0_offset = 0x193F1
mesh_skeleton_offset = 0x18C4C
if "lamp_default_publish.binfbx" in file_path:
table_offset = 0x40E
lod0_offset = 0x5F2
if "lamp_physx.binfbx" in file_path:
table_offset = 0x94A
lod0_offset = 0xB3E
if "nightingale_treeweapon.binfbx" in file_path:
table_offset = 0x426
lod0_offset = 0x82CB4
if "pumpactionshotgun_compensator_publish.binfbx" in file_path:
table_offset = 0x96C
lod0_offset = 0x9EB
if "pumpactionshotgun_default_publish_physx.binfbx" in file_path:
table_offset = 0x2442
lod0_offset = 0x25BB
if "pumpactionshotgun_stock_pouch_publish.binfbx" in file_path:
table_offset = 0x8A4
lod0_offset = 0x923
if "revolver_default_publish.binfbx" in file_path:
table_offset = 0x1517
lod0_offset = 0x160D
if "rifle_cheek_pad_publish.binfbx" in file_path:
table_offset = 0xF75
lod0_offset = 0x1073
if "rifle_default_publish.binfbx" in file_path:
table_offset = 0x1536
lod0_offset = 0x1634
if "rifle_default_publish_physx.binfbx" in file_path:
table_offset = 0x2471
lod0_offset = 0x256F
if "rifle_scope_publish.binfbx" in file_path:
table_offset = 0xF75
lod0_offset = 0x106B
if "sawnoffshotgun_default_publish.binfbx" in file_path:
table_offset = 0x1514
lod0_offset = 0x1612
if "sawnoffshotgun_physx.binfbx" in file_path:
table_offset = 0x260B
lod0_offset = 0x2709
if "abigail_default_publish_physx.binfbx" in file_path:
table_offset = 0x2efe6
lod0_offset = 0xb30aae
if "ahti_default_publish_physx.binfbx" in file_path:
table_offset = 0x379ef
lod0_offset = 0x5cb9dd
if "alan_brightfalls_noplaid_publish_physx.binfbx" in file_path:
table_offset = 0x253f9
lod0_offset = 0x20df985
if "alan_brightfalls_publish_physx.binfbx" in file_path:
table_offset = 0x303f2
lod0_offset = 0x218e521
if "alan_default_dirty_01_publish_physx.binfbx" in file_path:
table_offset = 0x2f5ff
lod0_offset = 0x2356e64
if "alan_default_dirty_02_no_satchel_publish_physx.binfbx" in file_path:
table_offset = 0x33bde
lod0_offset = 0x10188b4
if "alan_default_dirty_02_publish_physx.binfbx" in file_path:
table_offset = 0x351a5
lod0_offset = 0x10da56d
if "alan_default_publish_physx.binfbx" in file_path:
table_offset = 0x2f5f3
lod0_offset = 0x232566a
if "alan_no_satchel_publish_physx.binfbx" in file_path:
table_offset = 0x2e028
lod0_offset = 0x2288458
if "alan_scratch_player_publish_physx.binfbx" in file_path:
table_offset = 0x2efcc
lod0_offset = 0x81A524
if "alan_wake_scratch_publish_physx.binfbx" in file_path:
table_offset = 0x6a541f
lod0_offset = 0x24ea0b5
mesh_skeleton_offset = 0x678968
if "alex_casey_dark_place_publish_physx.binfbx" in file_path:
table_offset = 0x2c6f4
lod0_offset = 0x1b6307e
if "alex_casey_default_publish_physx.binfbx" in file_path:
table_offset = 0x2d08d
lod0_offset = 0x1eccaa0
if "alex_casey_suit_publish.binfbx" in file_path:
table_offset = 0x22e5f
lod0_offset = 0x1B59BE2
if "alex_casey_suit_rough_publish_physx.binfbx" in file_path:
table_offset = 0x26f90
lod0_offset = 0x628DAC
if "charlie_koskela_mascot_publish_physx.binfbx" in file_path:
table_offset = 0x223c2
lod0_offset = 0x2aae00
if "charline_koskela_mascot_publish_physx.binfbx" in file_path:
table_offset = 0x226c3
lod0_offset = 0x2955a9
if "cynthia_weaver_dead_publish_physx.binfbx" in file_path:
table_offset = 0x440ab
lod0_offset = 0xf0d180
if "cynthia_weaver_sweater_publish_physx.binfbx" in file_path:
table_offset = 0x2e966
lod0_offset = 0xbd7938
if "deputy_mulligan_cultist_publish_physx.binfbx" in file_path:
table_offset = 0x26411
lod0_offset = 0xbc6e74
if "deputy_mulligan_default_publish_physx.binfbx" in file_path:
table_offset = 0x26ade
lod0_offset = 0xb64576
if "deputy_mulligan_guardian_publish_physx.binfbx" in file_path:
table_offset = 0x237e7
lod0_offset = 0xf45a19
mesh_skeleton_offset = 0x539e70
if "deputy_thorton_cultist_publish_physx.binfbx" in file_path:
table_offset = 0x2b112
lod0_offset = 0x1039915
if "deputy_thorton_default_publish_physx.binfbx" in file_path:
table_offset = 0x2b24d
lod0_offset = 0xf9f308
if "deputy_thorton_guardian_publish_physx.binfbx" in file_path:
table_offset = 0x569ddc
lod0_offset = 0x1920a21
if "donna_default_publish_physx.binfbx" in file_path:
table_offset = 0x241a8
lod0_offset = 0xa42115
if "ed_booker_deerfest_publish_physx.binfbx" in file_path:
table_offset = 0x2cfb2
lod0_offset = 0x74ff4b
if "ed_booker_default_publish_physx.binfbx" in file_path:
table_offset = 0x42c370
lod0_offset = 0xa9b16a
mesh_skeleton_offset = 0x3ff308
if "enemy_m_fadeout_01_publish.binfbx" in file_path:
table_offset = 0x1F9C9
lod0_offset = 0x3034C9
if "ilmo_koskela_bikergang_publish_physx.binfbx" in file_path:
table_offset = 0x2594e
lod0_offset = 0x68d545
if "ilmo_koskela_cultist_publish_physx.binfbx" in file_path:
table_offset = 0x3ba099
lod0_offset = 0xafa914
mesh_skeleton_offset = 0x3911a0
if "ilmo_koskela_deerfest_publish_physx.binfbx" in file_path:
table_offset = 0x25801
lod0_offset = 0x9e9c96
if "jaakko_koskela_bikergang_publish_physx.binfbx" in file_path:
table_offset = 0x2f92b
lod0_offset = 0xd1080f
if "jaakko_koskela_cultist_publish_physx.binfbx" in file_path:
table_offset = 0x454c4e
lod0_offset = 0x10076b9
mesh_skeleton_offset = 0x42b998
if "kiran_estevez_default_publish_physx.binfbx" in file_path:
table_offset = 0x2ab4c
lod0_offset = 0x97e866
if "kiran_estevez_healed_publish.binfbx" in file_path:
table_offset = 0x254d7
lod0_offset = 0x970eaf
if "kiran_estevez_wounded_jacket_publish_physx.binfbx" in file_path:
table_offset = 0x2ab44
lod0_offset = 0x4E94AA
if "kiran_estevez_wounded_publish.binfbx" in file_path:
table_offset = 0x2549f
lod0_offset = 0x94af5a
if "mandy_may_nursinghome_publish_physx.binfbx" in file_path:
table_offset = 0x26357
lod0_offset = 0x9f24d5
if "nightingale_guardian_publish.binfbx" in file_path:
table_offset = 0x20494
lod0_offset = 0x101707D
if "norman_deerfest_publish.binfbx" in file_path:
table_offset = 0x2069d
lod0_offset = 0x8aa9d7
if "norman_default_publish.binfbx" in file_path:
table_offset = 0x239d4
lod0_offset = 0x76fee3
if "odin_default_publish_physx.binfbx" in file_path:
table_offset = 0x24487
lod0_offset = 0x87b739
if "odin_rocker_publish_physx.binfbx" in file_path:
table_offset = 0x296d2
lod0_offset = 0x8c35c5
if "pat_maine_default_publish.binfbx" in file_path:
table_offset = 0x23c97
lod0_offset = 0x828564
if "rose_marigold_default_publish_physx.binfbx" in file_path:
table_offset = 0x32a33
lod0_offset = 0xd96181
if "rose_marigold_nursinghome_publish_physx.binfbx" in file_path:
table_offset = 0x2d0ff
lod0_offset = 0xd1b5ed
if "saga_alt_publish_physx.binfbx" in file_path:
table_offset = 0x40ed2
lod0_offset = 0x19990ED
if "saga_default_publish_physx.binfbx" in file_path:
table_offset = 0x3e394
lod0_offset = 0x16E23CC
if "saga_no_jacket_publish_physx.binfbx" in file_path:
table_offset = 0x34e35
lod0_offset = 0x18f6e73
if "saga_raincoat_publish_physx.binfbx" in file_path:
table_offset = 0x40d66
lod0_offset = 0x158ac0c
if "steven_lin_default_publish.binfbx" in file_path:
table_offset = 0x24d73
lod0_offset = 0xb58c24
if "tammy_booker_cultist_publish_physx.binfbx" in file_path:
table_offset = 0x25d85
lod0_offset = 0x50feb6
if "tammy_booker_default_publish_physx.binfbx" in file_path:
table_offset = 0x3ebedd
lod0_offset = 0x8758c7
mesh_skeleton_offset = 0x3c6190
if "tim_breaker_darkplace_publish_physx.binfbx" in file_path:
table_offset = 0x286c3
lod0_offset = 0xd766e0
if "tim_breaker_sheriff_publish_physx.binfbx" in file_path:
table_offset = 0x2851d
lod0_offset = 0xb25aab
if "tor_default_publish_physx.binfbx" in file_path:
table_offset = 0x2d8be
lod0_offset = 0xe73bad
if "vladimir_blum_cultist_publish_physx.binfbx" in file_path:
table_offset = 0x35b527
lod0_offset = 0x83b7ea
mesh_skeleton_offset = 0x3330b8
if "vladimir_blum_default_publish_physx.binfbx" in file_path:
table_offset = 0x243a2
lod0_offset = 0xaaed1d
# Auxiliary functions\
def read_char(file):
return struct.unpack('<B', file.read(1))[0]
def read_long(file):
return struct.unpack('<I', file.read(4))[0]
def read_short(file):
return struct.unpack('<h', file.read(2))[0]
def read_float(file):
return struct.unpack('<f', file.read(4))[0]
def read_float_array(count, file):
return [float(read_float(file)) for _ in range(count)]
def read_string(file):
result = ""
while True:
byte = file.read(1)
if not byte or byte == b'\x00':
break
result += byte.decode('utf-8') # Assuming the string is encoded in UTF-8
return result
def read_sofl(file):
length = read_long(file) - 1
string = file.read(length).decode('utf-8')
return string, length
# Mesh info structure
class MeshInfo:
def __init__(self, lodId, vertCount, faceCount, byteForFace, face_offset, name, bonesPerVertex, bbox, vertex_size):
self.lodId = lodId
self.vertCount = vertCount
self.faceCount = faceCount
self.byteForFace = byteForFace
self.face_offset = face_offset
self.name = name
self.bonesPerVertex = bonesPerVertex
self.bbox = bbox
self.vertex_size = vertex_size
class SkelInfo:
def __init__(self, weightcount, bonecount, bonenames, weightnames,bone_dict):
self.weightcount = weightcount
self.bonecount = bonecount
self.bonenames = bonenames
self.weightnames = weightnames
self.bone_dict = bone_dict
# Read mesh data for mesh info structure
def read_mesh_data(file, table_offset):
mesh_infos = []
file.seek(table_offset)
mesh_id = read_long(file)
dir, full = os.path.split(file_path)
name, ext = os.path.splitext(full)
bbox = dict()
for i in range(mesh_id):
cur_offset = file.tell()
# print(cur_offset)
lodId = read_long(file)
vertCount = read_long(file)
faceCount = read_long(file)
file.seek(8, 1)
byteForFace = read_long(file)
face_offset = read_long(file)
bonesPerVertex = read_long(file)
if bonesPerVertex == 8:
block_offset = 127
vertex_size = 32
elif bonesPerVertex == 4:
block_offset = 123
vertex_size = 16
elif bonesPerVertex == 1:
block_offset = 119
vertex_size = 12
elif bonesPerVertex == 0:
block_offset = 115
vertex_size = 8
# print(f"Vertex Size: {vertex_size}")
bbox[i] = read_float_array(10, file)
for n in range(10):
bbox[i][n] *= 5
# Go to the next block
if block_offset == 127:
file.seek(cur_offset + block_offset, 0)
countBlock = read_long(file)
file.seek(countBlock * 36, 1)
else:
file.seek(cur_offset + block_offset, 0)
mesh_infos.append(MeshInfo(lodId, vertCount, faceCount, byteForFace, face_offset, name, bonesPerVertex, bbox, vertex_size))
return mesh_infos
# Function for outputting mesh info structure (console)
def print_mesh_infos(mesh_infos):
for i, mesh_info in enumerate(mesh_infos, start=1): # Starts counting at 1
print(f"Mesh ID {i}: LOD ID: {mesh_info.lodId}, Vertex Count: {mesh_info.vertCount}, Face Count: {mesh_info.faceCount}, Bytes per Face: {mesh_info.byteForFace}, Bones per Vertex: {mesh_info.bonesPerVertex}, Face Offset: {mesh_info.face_offset}")
def read_skel_data(skel, mesh):
skel_infos = []
mesh.seek(mesh_skeleton_offset)
skel.seek(0x30, 0)
weightcount = read_long(mesh)
bonecount = read_long(skel)
bonenames = []
weightnames = []
bone_dict = dict()
skel.seek(0x50, 0)
offset = (bonecount * 32) + (bonecount * 2) + 80
while offset % 16 != 0:
offset += 1
offset += bonecount * 4
while offset % 16 != 0:
offset += 1
offset += 16 + (bonecount * 4)
while offset % 16 != 0:
offset += 1
offset += 16 + (bonecount * 8)
skel.seek(offset, 0)
for i in range(bonecount):
bonename = read_string(skel)
bonenames.append(bonename)
bone_dict[i] = bonename
# print("bone_dict:", bone_dict)
for i in range(weightcount):
cur_offset = mesh.tell()
# print("mesh name offset:", cur_offset)
weightname, wlength = read_sofl(mesh)
# print("weight name:", weightname, "current offset:", cur_offset, "weight number", i)
mesh.seek(cur_offset + (73 + wlength), 0)
# print("cur_offset (weights):", cur_offset + (73 + wlength))
weightnames.append(weightname)
skel_infos.append(SkelInfo(weightcount, bonecount, bonenames, weightnames, bone_dict))
return skel_infos
def print_skel_infos(skel_infos):
for i, skel_info in enumerate(skel_infos, start=1):
print(f"Weight Count: {skel_info.weightcount}, Bone Count: {skel_info.bonecount}")
def read_mesh_skel(skel, mesh, skel_info, name, objArray):
boneArray = []
parents = []
dir, full = os.path.split(file_path)
name, ext = os.path.splitext(full)
bpy.ops.object.add(
type='ARMATURE',
enter_editmode=True,)
arm_obj = bpy.context.object
bpy.data.collections[name].objects.link(arm_obj)
bpy.data.scenes['Scene'].collection.objects.unlink(arm_obj)
bpy.context.view_layer.objects.active = arm_obj
arm = arm_obj.data
skel.seek(0x50 + (skel_info.bonecount * 32), 0)
for i in range(0, skel_info.bonecount):
pid = read_short(skel)
parents.append(pid)
skel.seek(0x50, 0)
for i in range(0, skel_info.bonecount):
bpy.ops.object.mode_set(mode='EDIT')
# print("edit mode on")
cur_offset = skel.tell()
# print(f"cur skel offset: {cur_offset}")
bnVal = read_float_array(8, skel)
quat = Quaternion(Vector((bnVal[3], bnVal[0], bnVal[1], bnVal[2])))
# print(quat)
# print(skel_info.bone_dict)
bonename = skel_info.bone_dict.get(i, "key not found")
# print(f"{bonename} quat: {quat}")
# print(f"{bonename} transform: {transform}")
transform = [bnVal[4] * mltMsh, bnVal[5] * mltMsh, bnVal[6] * mltMsh]
tfm = Matrix.LocRotScale(transform, quat, None)
# print(f"{bonename} tfm matrix: {tfm}")
bone = arm.edit_bones.new(bonename)
bone.use_relative_parent = True
bone.head = Vector(transform)
bone.matrix = tfm
bone.length = 0.1
# bone.use_connect = True
if parents[i] >= 0:
bone.parent = arm.edit_bones[skel_info.bone_dict[parents[i]]]
bone.head += bone.parent.head
bone.tail = bone.head + Vector((.0, .0, .115))
if parents[i] == -1:
bone.head = Vector([.0, .0, .01]) #prevent zero length
if bone.parent:
bone.matrix = bone.parent.matrix @ tfm
else:
bone.matrix = tfm
# bpy.ops.object.mode_set(mode='POSE')
## print("pose mode on")
#
# pbone = arm_obj.pose.bones[bonename]
#
#
# pbone.matrix_basis.identity()
#
#
# if pbone.parent:
# pbone.matrix = pbone.parent.matrix @ tfm
# else:
# pbone.matrix = tfm
#
# bpy.ops.pose.armature_apply()
bpy.ops.object.mode_set(mode='OBJECT')
arm_obj.rotation_euler[0] = math.radians(90)
arm_obj.scale = (-1.0, 1.0, 1.0)
for i in range(len(objArray)):
mod = objArray[i].modifiers.new('Armature', 'ARMATURE')
mod.object = arm_obj
mod.use_bone_envelopes = False
mod.use_vertex_groups = True
if bpy.app.version[1] > 71:
context.view_layer.objects.active = objArray[i]
bpy.ops.object.vertex_group_sort(sort_type='BONE_HIERARCHY')
objArray[i].parent = arm_obj
# arm_obj.location.z += 3.12317
# Read UV data for the entire mesh with the corresponding LOD value
def read_mesh_uvs(file, mesh_info, uv_offset):
UV_array = []
file.seek(uv_offset)
uvCount = mesh_info.vertCount
# Assumption: UVs present for every vertex and consist of two 'short' values
for _ in range(uvCount):
u = struct.unpack('<H', file.read(2))[0] / 65535.0 * 16 # scaling factor [0,1]
v = struct.unpack('<H', file.read(2))[0] / 65535.0 * 16
UV_array.append((u,-v +1)) # Invert V for Blender
file.read(4) # Skipping bytes (padding)
return UV_array
smax = 1 / 65535
# Read vertex data for the entire mesh with the corresponding LOD value
def read_mesh_vertices(file, mesh_info, vertex_offset, minv, maxv, meshnum):
vertArray = []
lva = []
# Navigate to the start of the vertex data
file.seek(vertex_offset)
vertCount = mesh_info.vertCount
xMin = 9000000
yMin = 9000000
zMin = 9000000
xMax = 0
yMax = 0
zMax = 0
for _ in range(vertCount):
cur_offset = file.tell()
lx = read_short(file) * smax
ly = read_short(file) * smax
lz = read_short(file) * smax
lva.append((lx, ly, lz))
file.seek(cur_offset + mesh_info.vertex_size, 0)
# print(f"min vertex: {minv}")
# print(f"max vertex: {maxv}")
for vi in range(minv, maxv):
if xMin > lva[vi][0]:
xMin = lva[vi][0]
if xMax < lva[vi][0]:
xMax = lva[vi][0]
if yMin > lva[vi][1]:
yMin = lva[vi][1]
if yMax < lva[vi][1]:
yMax = lva[vi][1]
if zMin > lva[vi][2]:
zMin = lva[vi][2]
if zMax < lva[vi][2]:
zMax = lva[vi][2]
# minv -= 1
# print(f"bbox: {mesh_info.bbox[meshnum]}")
# print(f"x min: {xMin}, y min: {yMin}, z min: {zMin}")
# print(f"x max: {xMax}, y max: {yMax}, z max: {zMax}")
xsca = (mesh_info.bbox[meshnum][7] - mesh_info.bbox[meshnum][4]) / (xMax - xMin)
ysca = (mesh_info.bbox[meshnum][8] - mesh_info.bbox[meshnum][5]) / (yMax - yMin)
zsca = (mesh_info.bbox[meshnum][9] - mesh_info.bbox[meshnum][6]) / (zMax - zMin)
# avert your eyes, shitty handling for when one of the max or mins is 0 because it fucks up the submesh's scale.... :(
if xMax == 0 or xMin == 0:
xsca = ysca
if yMax == 0 or yMin == 0:
xsca = zsca
ysca = zsca
if zMax == 0 or zMin == 0:
print(f"Couldn't scale submesh {meshnum + 1} properly.")
elif yMax == 0 or yMin == 0:
ysca = xsca
if zMax == 0 or zMin == 0:
zsca = xsca
elif zMax == 0 or zMin == 0:
zsca = xsca
print(f"xsca: {xsca}")
print(f"ysca: {ysca}")
print(f"zsca: {zsca}")
for i in range(vertCount):
vx = ((lva[i][0] - xMin) * xsca + mesh_info.bbox[meshnum][4])
vy = ((lva[i][1] - yMin) * ysca + mesh_info.bbox[meshnum][5])
vz = ((lva[i][2] - zMin) * zsca + mesh_info.bbox[meshnum][6])
vertArray.append((vx, vy, vz))
return vertArray
def read_mesh_weights(file, mesh_info, skel_info, vertex_offset, vertcount):
file.seek(vertex_offset, 0)
print(f"vertex size in weights func: {mesh_info.vertex_size}")
bdrev = dict((v, k) for k, v in skel_info.bone_dict.items())
vrts = []
for i in range(vertcount):
vrt = []
cur_offset = file.tell()
if mesh_info.lodId == 0:
vbids = []
vweights = []
file.seek(cur_offset + 8, 0)
for _ in range(mesh_info.bonesPerVertex):
if mesh_info.vertex_size == 32:
lbid = read_short(file)
elif mesh_info.vertex_size == 16 or mesh_info.vertex_size == 12:
lbid = read_char(file)
vbids.append(lbid)
file.seek(cur_offset + (3 * mesh_info.bonesPerVertex), 0)
for _ in range(mesh_info.bonesPerVertex):
lweight = read_char(file)
vweights.append(lweight)
for bdat in range(mesh_info.bonesPerVertex):
lbid = vbids[bdat]
if lbid != 0:
weightname = skel_info.weightnames[lbid]
nbid = bdrev[weightname]
vrt.append(nbid)
vrt.append(vweights[bdat] / 255.0)
else:
vrt.append(0)
vrt.append(vweights[bdat] / 255.0)
vrts.append(vrt)
file.seek(cur_offset + mesh_info.vertex_size, 0)
print(f"number of weight verts: {len(vrts)}")
return vrts
# Read face data for each Submesh
def read_mesh_faces(file, mesh_info, face_offset):
faceArray = []
minv = 9000000
maxv = 0
# Go to the start point of the face data for this Submesh
file.seek(face_offset)
# Reading the face data (indices)
for _ in range(mesh_info.faceCount):
f1 = f2 = f3 = 0
if mesh_info.byteForFace == 4:
# The indices must be correct and within the bounds of the vertex array
f1, f2, f3 = struct.unpack('<III', file.read(12))
elif mesh_info.byteForFace == 2:
# The indices must be correct and within the bounds of the vertex array
f1, f2, f3 = struct.unpack('<HHH', file.read(6))
faceArray.append((f1, f2, f3))
if f1 < minv:
minv = f1 + 1
if f2 < minv:
minv = f2 + 1
if f3 < minv:
minv = f3 + 1
if f1 > maxv:
maxv = f1 + 1
if f2 > maxv:
maxv = f2 + 1
if f3 > maxv:
maxv = f3 + 1
return faceArray, minv, maxv
# Create mesh in Blender
def create_mesh_in_blender(vertArray, faceArray, UV_array, mesh_name, name, objArray):
# Create new mesh and new object
mesh_data = bpy.data.meshes.new(mesh_name)
mesh_obj = bpy.data.objects.new(mesh_name, mesh_data)
# Link scene and position mesh object
bpy.data.collections[name].objects.link(mesh_obj)
bpy.context.view_layer.objects.active = mesh_obj
mesh_obj.select_set(True)
if not import_rig:
bpy.context.active_object.rotation_euler[0] = math.radians(90)
# Assign mesh data
mesh_data.from_pydata(vertArray, [], faceArray)
mesh_data.update() # Mesh update to apply the changes
# Assign UV data
if UV_array:
# Check if UV map already exists, if not, create a new one
if "UVMap" not in mesh_data.uv_layers:
mesh_data.uv_layers.new(name='UVMap')
uv_layer = mesh_data.uv_layers['UVMap'].data
# Delete old UV map if there is one
if "UVMap" in mesh_data.uv_layers:
mesh_data.uv_layers.remove(mesh_data.uv_layers["UVMap"])
# Create new UV map
uv_layer = mesh_data.uv_layers.new(name="UVMap")
# Assignment of the UV coordinates to the new UV map
for poly in mesh_data.polygons: # Go through each polygon
poly.use_smooth = True
for loop_index, loop_vert in zip(poly.loop_indices, poly.vertices): # Go through each loop and vertex of the polygon
# Access the UV loop to set the UV coordinates
loop_uv = uv_layer.data[loop_index]
# Assign UV values (assumption: UV_array contains pairs of UV values)
loop_uv.uv = UV_array[loop_vert] # Assigning the UV coordinates according to the vertices
# Clean up mesh
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.delete_loose()
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.object.editmode_toggle()
mesh_data.update() # Mesh update to apply the changes
objArray.append(mesh_obj)
return objArray
# Constants
UV_SIZE = 8 # A UV pair consists of two floats
#FACE_SIZE = 3 # A face consists of three indices and each index is an integer
HEADER_SIZE = 4 # The first 4 bytes from lod0_offset must be skipped
# Main function
def main():
print("binfbx path (main):", file_path)
objArray = []
msh = -1
verts = 0
with open(file_path, "rb") as file:
mesh_infos = read_mesh_data(file, table_offset)
print_mesh_infos(mesh_infos)
for mesh_info in mesh_infos:
collection_name = mesh_info.name
# check if the collection already exists
if collection_name not in bpy.data.collections:
bpy.ops.collection.create(name=collection_name)
bpy.context.scene.collection.children.link(bpy.data.collections[collection_name])
if import_rig:
with open(skel_path, "rb") as skel:
print("reading skel...")
skel_infos = read_skel_data(skel, file)
print_skel_infos(skel_infos)
for i, mesh_info in enumerate(mesh_infos, start=1):
if mesh_info.lodId == target_lod:
msh += 1
vrtwgt = None
# for n in range(i)
uv_offset = lod0_offset + HEADER_SIZE
vertex_offset = (uv_offset + (mesh_info.vertCount * UV_SIZE) - HEADER_SIZE)
print(f"Vertex Size: {mesh_info.vertex_size}")
submeshvert_offset = vertex_offset + (verts * mesh_info.vertex_size)
print(f"Vertex Offset in DEC for Submesh {i} (LOD{mesh_info.lodId}): {submeshvert_offset}")
face_offset = vertex_offset + (mesh_info.vertCount * mesh_info.vertex_size) + (mesh_info.face_offset * mesh_info.byteForFace)
print(f"Face Offset in DEC for Mesh ID {i} (LOD{mesh_info.lodId}): {face_offset}")
UV_array = read_mesh_uvs(file, mesh_info, uv_offset)
faceArray, minv, maxv = read_mesh_faces(file, mesh_info, face_offset)
vertArray = read_mesh_vertices(file, mesh_info, vertex_offset, minv, maxv, i - 1)
if len(faceArray) != mesh_info.faceCount:
print(f"Warning: Number of faces loaded ({len(faceArray)}) does not match expected number ({mesh_info.faceCount}) agree!")
mesh_name = f"{mesh_info.name}_LOD{mesh_info.lodId}__ID_{i}"
objArray = create_mesh_in_blender(vertArray, faceArray, UV_array, mesh_name, mesh_info.name, objArray)
print(f"submesh number {msh}")
vertcount = len(objArray[msh].data.vertices)
verts += vertcount
print(f"number of verts for submesh {msh}: {vertcount}")
obj = objArray[msh]
if import_rig:
with open(skel_path, "rb") as skel:
# assigning weight groups
for _, skel_info in enumerate(skel_infos, start=1):
vrtwgt = read_mesh_weights(file, mesh_info, skel_info, submeshvert_offset, vertcount)
for vrt in range(vertcount):
wgt = vrtwgt
boneid = wgt[vrt][0]
bonename = skel_info.bone_dict[boneid]
grp = None
if bonename in obj.vertex_groups.keys():
grp = obj.vertex_groups[bonename]
if grp == None:
grp = obj.vertex_groups.new(name=bonename)
wgt = vrtwgt
# print(f"boneid: {vrtwgt[vrt][0]}")
# print(f"current bone: {n}")
grp.add([vrt],
wgt[vrt][1],
'ADD')
if smooth:
bpy.ops.object.mode_set(mode='WEIGHT_PAINT')
bpy.ops.active_object = obj
bpy.ops.object.vertex_group_smooth(group_select_mode='ALL', factor=0.5, repeat=2, expand=0.0)
if mesh_info.vertex_size == 32:
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.remove_doubles(use_unselected=True, use_sharp_edge_from_normals=True)
bpy.ops.object.mode_set(mode='OBJECT')
if i+1 < len(mesh_infos) and mesh_info.lodId != mesh_infos[i+1].lodId:
print("Move to next LOD or additional processing")