-
Notifications
You must be signed in to change notification settings - Fork 1
/
io_scene_m3d.py
1696 lines (1575 loc) · 72 KB
/
io_scene_m3d.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
# ##### BEGIN MIT LICENSE BLOCK #####
#
# blender/io_scene_m3d.py
#
# Copyright (C) 2019 - 2022 bzt (bztsrc@gitlab)
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use, copy,
# modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
# @brief Blender 2.80 Model 3D Exporter (and one day Importer too)
# https://gitlab.com/bztsrc/model3d
#
# ##### END MIT LICENSE BLOCK #####
# <pep8-80 compliant>
bl_info = {
"name": "Model 3D (.m3d) format",
"author": "bzt",
"version": (0, 0, 1),
"blender": (2, 80, 0),
"location": "File > Import-Export",
"description": "Export M3D",
"wiki_url": "https://gitlab.com/bztsrc/model3d/blob/master/docs/m3d_format.md",
"category": "Import-Export"}
# -----------------------------------------------------------------------------
# Import libraries
import bmesh
import os
from operator import itemgetter
from struct import pack, unpack
from mathutils import Matrix
from bpy_extras import io_utils, node_shader_utils
from bpy_extras.wm_utils.progress_report import (
ProgressReport,
ProgressReportSubstep,
)
# -----------------------------------------------------------------------------
# Blender material property and M3D property type assignments
# See https://gitlab.com/bztsrc/model3d/blob/master/docs/m3d_format.md section Materials)
mat_property_map = {
#type format PrincipledBSDF property ASCII variant
0: ["color", "base_color", "Kd"],
1: ["gscale", "metallic", "Ka"],
2: ["gscale", "specular", "Ks"],
3: ["//float","specular_tint", "Ns"],
4: ["//color","emissive", "Ke"], # not in BSDF?
5: ["gscale", "transmission", "Tf"],
6: ["float", "normalmap_strength", "Km"],
7: ["float", "alpha", "d"],
8: ["//byte", "illumination", "il"], # not in PBR at all
64: ["float", "roughness", "Pr"],
65: ["float", "metallic", "Pm"],
66: ["//float","sheen", "Ps"], # not in BSDF?
67: ["float", "ior", "Ni"],
128: ["map", "base_color_texture", "map_Kd"],
130: ["//map", "specular_texture", "map_Ks"], # should work, but it does not
133: ["map", "transmission_texture", "map_Tf"],
134: ["map", "normalmap_texture", "map_Km"],
135: ["map", "alpha_texture", "map_D"],
192: ["map", "roughness_texture", "map_Pr"],
193: ["map", "metallic_texture", "map_Pm"],
195: ["map", "ior_texture", "map_Ni"],
}
# -----------------------------------------------------------------------------
# Load and parse a Model 3D file (unlike the exporter, this is WIP)
def read_m3d(context,
filepath,
report,
global_matrix=None,
):
# read in an index
def getidx(data, fmt):
idx = -1
if fmt == 0:
if len(data) > 0:
idx = unpack("<B", data[0:1])[0]
if idx > 253:
idx = idx - 256
data = data[1:]
elif fmt == 1:
if len(data) > 1:
idx = unpack("<H", data[0:2])[0]
if idx > 65533:
idx = idx - 65536
data = data[2:]
elif fmt == 2:
if len(data) > 3:
idx = unpack("<I", data[0:4])[0]
if idx > 4294967293:
idx = idx - 4294967296
data = data[4:]
return data, idx
# read in a coordinate
def getcrd(data, fmt):
crd = 0
if fmt == 0:
crd = round(float(unpack("<b", data[0:1])[0]) / 127.0, 4)
data = data[1:]
elif fmt == 1:
crd = round(float(unpack("<h", data[0:2])[0]) / 32767.0, 4)
data = data[2:]
elif fmt == 2:
crd = unpack("<f", data[0:4])[0]
data = data[4:]
elif fmt == 3:
crd = unpack("<d", data[0:8])[0]
data = data[8:]
if crd == -0.0:
crd = 0.0
return data, crd
with ProgressReport(context.window_manager) as progress:
############ !!!!!!!!! Work In Progress !!!!!!!!!! ###############
if global_matrix is None:
global_matrix = axis_conversion(from_forward='Z', from_up='Y').to_4x4()
# read in file
f = open(filepath, 'rb')
data = f.read()
f.close()
if len(data) > 2 and data[0:2] == b'\x1F\x8B':
import gzip
data = gzip.decompress(data)
if len(data) < 8 or (data[0:4] != b'3DMO' and data[0:7] != b'3dmodel'):
report({"ERROR"}, filepath + " is not a valid Model 3D file!")
return {'FINISHED'}
# load into Blender independent lists
cmap = []
tmap = []
vrts = []
faces = []
shapes = []
labels = []
materials = []
bones = []
skins = []
if data[0:7] == b'3dmodel':
# Model 3D ASCII variant
data = str(data)
if data[0:2] == "b'":
data = data[2:len(data)-3] # cut off "b'" and "'"
# parse model header
head = {
'name':'', 'license':'', 'author':'', 'description':'',
'scale':1.0
}
else:
# Model 3D binary variant
# skip over preview image chunk
if data[8:12] == b'PRVW':
length = unpack("<I", data[4:8])[0]
data = data[length:]
if data[8:12] != b'HEAD':
import zlib
data = zlib.decompress(data[8:])
else:
data = data[8:]
if data[0:4] != b'HEAD':
report({"ERROR"}, filepath + " is not a valid Model 3D file!")
return {'FINISHED'}
# parse model header and string table
head = {
'name':'', 'license':'', 'author':'', 'description':'',
'scale':unpack("<f", data[8:12])[0],
'vc_s':(data[12] >> 0) & 3, 'vi_s':(data[12] >> 2) & 3, 'si_s':(data[12] >> 4) & 3,
'ci_s':(data[12] >> 6) & 3, 'ti_s':(data[13] >> 2) & 3, 'bi_s':(data[13] >> 2) & 3,
'nb_s':(data[13] >> 4) & 3, 'sk_s':(data[13] >> 6) & 3, 'fc_s':(data[14] >> 0) & 3,
'hi_s':(data[14] >> 2) & 3, 'fi_s':(data[14] >> 4) & 3,
}
length = unpack("<I", data[4:8])[0]
chunk = data[16:length]
data = data[length:]
s = chunk.split(b'\000')
head['name'] = str(s[0], 'utf-8')
head['license'] = str(s[1], 'utf-8')
head['author'] = str(s[2], 'utf-8')
head['description'] = str(s[3], 'utf-8')
strs = {0:''}
i = len(s[0]) + len(s[1]) + len(s[2]) + len(s[3]) + 4
for t in s[4:]:
strs[i] = str(t, 'utf-8')
i = i + len(t) + 1
# decode chunks
while len(data) > 4 and data[0:4] != b'OMD3':
magic = data[0:4]
length = unpack("<I", data[4:8])[0]
if length < 8 or length > 16 * 1024 * 1024:
report({"ERROR"}, str(magic,'utf-8') + " bad chunk in Model 3D file!")
break
chunk = data[8:length]
# color map
if magic == b'CMAP':
while len(chunk) > 0:
cmap.append([
unpack("<B", chunk[0:1])[0] / 255.0,
unpack("<B", chunk[1:2])[0] / 255.0,
unpack("<B", chunk[2:3])[0] / 255.0,
unpack("<B", chunk[3:4])[0] / 255.0])
chunk = chunk[4:]
# texture map
elif magic == b'TMAP':
while len(chunk) > 0:
u = v = 0.0
# don't use getcrd, because this is scaled to 255
if head['vc_s'] == 0:
u = float(unpack("<B", chunk[0:1])[0]) / 255.0
v = float(unpack("<B", chunk[1:2])[0]) / 255.0
chunk = chunk[2:]
elif head['vc_s'] == 1:
u = float(unpack("<H", chunk[0:2])[0]) / 65535.0
v = float(unpack("<H", chunk[2:4])[0]) / 65535.0
chunk = chunk[4:]
elif head['vc_s'] == 2:
u, v = unpack("<ff", chunk[0:8])
chunk = chunk[8:]
elif head['vc_s'] == 3:
u, v = unpack("<dd", chunk[0:16])
chunk = chunk[16:]
tmap.append([round(u, 4), round(v, 4)])
# vertex list (for vertex and normals)
elif magic == b'VRTS':
while len(chunk) > 0:
chunk, x = getcrd(chunk, head['vc_s'])
chunk, y = getcrd(chunk, head['vc_s'])
chunk, z = getcrd(chunk, head['vc_s'])
chunk, w = getcrd(chunk, head['vc_s'])
chunk, c = getidx(chunk, head['ci_s'])
chunk, s = getidx(chunk, head['sk_s'])
if ci_s < 4 and c != -1:
c = cmap[c]
elif ci_s == 4:
c = [c[0:1] / 255.0, c[1:2] / 255.0, c[2:3] / 255.0, c[3:4] / 255.0]
vrts.append([x, y, z, w, c, s])
# material chunk
elif magic == b'MTRL':
chunk, n = getidx(chunk, head['si_s'])
props = {}
while len(chunk) > 0:
typ = unpack("<B", chunk[0:1])[0]
chunk = chunk[1:]
try:
t = mat_property_map[typ];
except:
print("unknown property ", typ)
break;
if typ == 0:
chunk, c = getidx(chunk, head['ci_s'])
props["base_color"] = cmap[c]
props["alpha"] = cmap[c][3]
elif typ >= 128:
chunk, s = getidx(chunk, head['si_s'])
if t[1][0:2] != "//":
props[t[1]] = strs[s] + ".png"
elif t[0] == "gscale":
chunk, c = getidx(chunk, head['ci_s'])
props[t[1]] = round((cmap[c][0] + cmap[c][1] + cmap[c][2]) / 3, 4)
elif t[0] == "color":
chunk, c = getidx(chunk, head['ci_s'])
if ci_s < 4 and c != -1:
c = cmap[c]
elif ci_s == 4:
c = [c[0:1] / 255.0, c[1:2] / 255.0, c[2:3] / 255.0, c[3:4] / 255.0]
props[t[1]] = c
elif t[0] == "float":
chunk, c = getcrd(chunk, 2)
props[t[1]] = c
elif t[0] == "byte":
props[t[1]] = unpack("<B", chunk[0:1])[0]
chunk = chunk[1:]
elif t[0] == "int":
props[t[1]] = unpack("<I", chunk[0:4])[0]
chunk = chunk[4:]
elif t[0] == "//map":
chunk = chunk[head['si_s']:]
elif t[0] == "//color" or t[0] == "//gscale":
chunk = chunk[head['ci_s']:]
elif t[0] == "//float" or t[0] == "//int":
chunk = chunk[4:]
elif t[0] == "//byte":
chunk = chunk[1:]
else:
break;
materials.append([strs[n], props])
# mesh (only triangles supported for now)
elif magic == b'MESH':
m = -1
while len(chunk) > 0:
typ = unpack("<B", chunk[0:1])[0]
chunk = chunk[1:]
if typ == 0:
chunk, s = getidx(chunk, head['si_s'])
m = -1
if s != 0:
for i, mat in enumerate(materials):
if strs[s] == mat[0]:
m = i
break
elif (typ >> 4) == 3:
v = [-1, -1, -1]
t = [-1, -1, -1]
n = [-1, -1, -1]
for i in range(0, 3):
chunk, v[i] = getidx(chunk, head['vi_s'])
if typ & 1:
chunk, t[i] = getidx(chunk, head['ti_s'])
if typ & 2:
chunk, n[i] = getidx(chunk, head['vi_s'])
faces.append({'m':m, 'v':v, 't':t, 'n':n})
else:
print("Only triangles supported")
break
# shapes
elif magic == b'SHPE':
chunk, s = getidx(chunk, head['si_s'])
print("shape ", strs[s])
# annotation labels
elif magic == b'LBLS':
chunk, n = getidx(chunk, head['si_s'])
chunk, l = getidx(chunk, head['si_s'])
chunk, c = getidx(chunk, head['ci_s'])
if ci_s < 4 and c != -1:
c = cmap[c]
elif ci_s == 4:
c = [c[0:1] / 255.0, c[1:2] / 255.0, c[2:3] / 255.0, c[3:4] / 255.0]
print("Labels layer:", strs[n], ", lang:", strs[l], ", color ", c)
# armature bones and vertex groups
elif magic == b'BONE':
chunk, b = getidx(chunk, head['bi_s'])
chunk, s = getidx(chunk, head['sk_s'])
print("Skeleton ", b, "bone(s),", s, "skin record(s) (unique bone/weight combos)")
# animation and timeline marker
elif magic == b'ACTN':
chunk, s = getidx(chunk, head['si_s'])
chunk, f = getidx(chunk, 1)
chunk, l = getidx(chunk, 2)
print("Action ", strs[s], ", durationmsec", l, ", numframes ", f)
# inlined asset
elif magic == b'ASET':
chunk, s = getidx(chunk, head['si_s'])
print("Inlined asset ", strs[s], "(", len(chunk), " bytes)")
else:
print("Unknown chunk '%s' skipping..." % (str(magic,'utf-8')))
data = data[length:]
del strs
del cmap
# ----------------- Start of Blender Specific Stuff ---------------------
print("\nhead ", head)
print("\ntmap ", tmap) # texture map, array of [u, v]
print("\nvrts ", vrts) # vertex list, array of [x, y, z, w, [r,g,b,a], skinid]
print("\nmaterials", materials) # array of [name, array of [principledBSDFpropname, value]]
print("\nfaces ", faces) # triangles, array of [m:materialidx, v[3]:vertexidx, t[3]:tmapidx, n[3]:normalvertexidx]
print("\nshapes ", shapes)
print("\nlabels ", labels)
print("\nbones ", bones)
print("\nskins ", skins)
# ----------------- End of Blender Specific Stuff ---------------------
report({"ERROR"}, "Model 3D importer not fully implemented yet.")
return {'FINISHED'}
# -----------------------------------------------------------------------------
# Construct and save a Model 3D file
def write_m3d(context,
filepath,
report,
*,
use_name='', # model's name
use_license='MIT', # model's license
use_author='', # model's author
use_comment='', # model's comment
use_scale=1.0, # model-space 1.0 in SI meters
use_selection=True, # export selected items only
use_mesh_modifiers=True, # apply mesh modifiers
use_normals=False, # save normal vectors too
use_uvs=True, # save texture map UV coordinates
use_colors=True, # save per vertex colors
use_shapes=False, # save shape commands
use_materials=True, # save materials
use_skeleton=True, # save bind-pose armature
use_animation=True, # save skeletal animations
use_markers=False, # use timeline markers for animations
use_fps=25, # frame per second
use_quality='-1', # -1: auto, 0: 8 bit, 1: 16 bit, 2: 32 bit, 3: 64 bit
use_inline=False, # inline textures
use_gridcompress=True, # use lossy grid compression
use_strmcompress=True, # use lossless stream compression
use_ascii=False, # save ASCII variant
use_relbones=True, # (debug only) use parent relative bone positions
global_matrix=None, # default orientation
check_existing=True,
):
# convert string to name identifier
def safestr(name, morelines=0):
if name is None:
return ''
elif morelines == 3:
return name.replace('\r', '').strip()
elif morelines == 2:
return name.replace('\r', '').replace('\n', ' ').strip()
elif morelines == 1:
return name.replace('\r', '').replace('\n', '\r\n').strip()
else:
return name.replace(' ', '_').replace('/', '_').replace('\\', '_').replace('\r', '').replace('\n', ' ').strip()
# set is unique, but has no index, list has index, but not unique...
# this is utterly and painfully slow, hence the dict wrapper below
def uniquelist(l, e):
try:
i = l.index(e)
except ValueError:
i = len(l)
l.append(e)
return i
# use hash table and then convert dict to list instead
# this uses considerably more memory, but we have no choice:
# using uniquelist takes several minutes with 50000 triangles...
def uniquedict(l, e):
h = hash(str(e))
try:
return l[h][0]
except KeyError:
i = len(l)
l[h] = [i, e]
return i
def dict2list(l):
r = []
for i, v in l.items():
r.insert(v[0], v[1])
return r
# get index size (we use -1 and -2 as special indices)
def idxsize(cnt):
if cnt == 0:
return 3
elif cnt < 254:
return 0
elif cnt < 65534:
return 1
return 2
# write out an index
def addidx(fmt, idx):
# we rely on the fact that in C -1 is a full binary 1 which
# gives the maximum unsigned value regardless to size, but
# pack stops us from taking advantage of that
if fmt == 0:
if idx < 0:
idx = 256 + idx
return pack("<B", idx)
elif fmt == 1:
if idx < 0:
idx = 65536 + idx
return pack("<H", idx)
elif fmt == 2:
if idx < 0:
idx = 4294967296 + idx
return pack("<I", idx)
return b''
# eliminate minus zero
def vert(x,y,z,w,c,s):
if x == -0.0:
x = 0.0
if y == -0.0:
y = 0.0
if z == -0.0:
z = 0.0
if w == -0.0:
w = 0.0
return [x,y,z,w,c,s]
# normalize matrix, decompose and recompose to eliminate errors
def matnorm(a):
p, q, s = a.decompose()
q.normalize()
return Matrix.Translation(p) @ q.to_matrix().to_4x4()
# get texture
def gettexture(fn, use_inline):
data = b''
if fn[0:2] == "//":
fn = fn[2:]
imgpath = repr(os.path.basename(fn))[1:-1]
imgpath = os.path.splitext(imgpath)[0]
if imgpath != "" and use_inline:
try:
data = open(os.path.join(os.path.dirname(filepath), fn), 'rb').read()
except:
try:
data = open(os.path.join(os.path.dirname(filepath), os.path.basename(fn)), 'rb').read()
except:
try:
data = open(os.path.join(os.path.dirname(filepath), imgpath + ".png"), 'rb').read()
except:
try:
data = open(imgpath + ".png", 'rb').read()
except:
try:
data = open(fn, 'rb').read()
except:
data = b''
if len(data) < 8 or data[0:4] != b'\x89PNG':
report({"ERROR"}, "Texture file '" + fn + "' not found or not a valid PNG. Cannot be inlined.")
data = b''
return [ imgpath, data ]
# recursively walk skeleton and construct string representation
def bonestr(strs, bones, parent, level):
ret = ""
for i,b in enumerate(bones):
if b[0] == parent:
ret += "/"*level + str(b[2]) + " " + str(b[3]) + " " + strs[b[1]] + "\r\n"
ret += bonestr(strs, bones, i, level+1)
return ret
with ProgressReport(context.window_manager) as progress:
if global_matrix is None:
global_matrix = axis_conversion(from_forward='-Y', from_up='Z',to_forward='Z', to_up='Y').to_4x4()
if use_animation:
use_skeleton = True
if use_fps < 1 or use_fps > 120:
use_fps = 25
# Get Blender objects to export
depsgraph = context.evaluated_depsgraph_get()
scene = context.scene
if use_selection:
objects = context.selected_objects
else:
objects = context.scene.objects
# if use_quality is set to auto, then count the number of triangles to decide
use_quality = int(use_quality)
if use_quality < 0 or use_quality > 3:
n = 0
for i, ob_main in enumerate(objects):
if ob_main.parent and ob_main.parent.instance_type in {'VERTS', 'FACES'}:
continue
try:
me = ob_main.original.to_mesh()
n += len(me.polygons)
except:
continue
if n < 1024:
use_quality = 0
else:
use_quality = 1
# we must use floating point without grid compression
if use_gridcompress == False and use_quality < 2:
use_quality = 2
# get the number of significant digits depending on quality
if use_quality == 3:
digits = 15
if use_quality == 2:
digits = 7
else:
digits = 4
# Build global lists with unique elements
# we use a dict wrapper to speed up things
cmap = {} # color map entries
strs = {} # string table with unique strings
verts = {} # unique list of vertices
tmaps = {} # texture map UV coordinates
faces = [] # triangles list
shapes = [] # shapes list
labels = [] # annotation labels
materials = [] # translated material name and properties
bones = {} # bind-pose skeleton
skins = {} # array of bone id / weight combinations per vertex
actions = [] # animations
inlined = {} # inlined textures
extras = [] # extra chunks (engine specific)
progress.enter_substeps(2 + use_materials + use_skeleton + use_animation)
# ----------------- Start of Blender Specific Stuff ---------------------
refmats = {} # unique list of referenced Blender material objects
nb_m = 0 # maximum number of bone weights per vertex
fi_m = 0 # frame index maximum
# set rest armature (bind-pose skeleton)
# if we don't do this, we'll get strange bones and distorted mesh
oldaction = None
oldframe = context.scene.frame_current
oldpose = {}
for i,ob_main in enumerate(objects):
if ob_main.type == "ARMATURE":
oldpose[i] = ob_main.data.pose_position
ob_main.data.pose_position = "REST"
ob_main.data.update_tag()
if oldaction == None and ob_main.animation_data and ob_main.animation_data.action:
oldaction = ob_main.animation_data.action
context.scene.frame_set(0)
### Armature ###
if use_skeleton:
# this must be done before the mesh so that skin can refer to bones
progress.step("Exporting Armature")
idx = 0
for i,ob_main in enumerate(objects):
if ob_main.type != "ARMATURE":
continue
for b in ob_main.data.bones:
m = matnorm(global_matrix @ ob_main.matrix_world @ b.matrix_local)
a = -1
if b.parent:
# is there a better way to get the parent's
# index in the armature's bone collection?
for j,p in enumerate(ob_main.data.bones):
if p == b.parent:
a = j
break
if use_relbones == True:
p = matnorm(global_matrix @ ob_main.matrix_world @ b.parent.matrix_local)
m = p.inverted() @ m
# For the top level bones, we need model-space p,q
# for the children, parent relative p,q
p = m.to_translation() # position
q = m.to_quaternion() # orientation
q.normalize()
n = safestr(b.name)
try:
ni = strs[hash(str(n))][0]
name = "'" + b.name + "'"
if b.name != n:
name += " (" + n + ")"
report({"ERROR"}, "Bone name " + name + " not unique.")
use_skeleton = False
use_animation = False
bones = {}
break
except:
pass
bones[b.name] = [idx, [a, uniquedict(strs, n),
uniquedict(verts, vert(
round(p[0], digits),
round(p[1], digits),
round(p[2], digits), 1.0, 0, -1)),
uniquedict(verts, vert(
round(q.x, digits),
round(q.y, digits),
round(q.z, digits),
round(q.w, digits), 0, -2))]]
idx = idx + 1
if len(bones) < 1 and use_animation:
report({"ERROR"}, "Skipping skeletal animation in lack of armature.")
use_animation = False
### Mesh data ###
progress.step("Exporting Mesh")
progress.enter_substeps(len(objects))
for i, ob_main in enumerate(objects):
# this mess was taken from io_scene_obj. The point is, at the end we have
# something that has faces with triangles and model-space coordinate vertices
if ob_main.parent and ob_main.parent.instance_type in {'VERTS', 'FACES'}:
continue
obs = [(ob_main, ob_main.matrix_world)]
if ob_main.is_instancer:
obs += [(dup.instance_object.original, dup.matrix_world.copy())
for dup in depsgraph.object_instances
if dup.parent and dup.parent.original == ob_main]
for ob, ob_mat in obs:
# get a copy of the mesh object
try:
o = ob.evaluated_get(depsgraph) if use_mesh_modifiers else ob.original
me = o.to_mesh()
except:
me = None
if me is None or len(me.polygons) < 1:
continue
if use_name is None or use_name == '':
use_name = ob.name
# triangulate if we must
r = False
for poly in me.polygons:
if len(poly.loop_indices) != 3:
r = True
break
if r == True:
#print("Need to triangulate mesh '" + me.name + "'")
bm = bmesh.new()
bm.from_mesh(me)
bmesh.ops.triangulate(bm, faces=bm.faces[:])
bm.to_mesh(me)
bm.free()
# transform vertices to model-space
me.transform(global_matrix @ ob_mat)
if ob_mat.determinant() < 0.0:
me.flip_normals()
if use_normals:
me.calc_normals_split()
if use_skeleton and len(ob.vertex_groups) > 0:
vg = ob.vertex_groups
else:
vg = []
if use_skeleton == True and use_animation == True:
report({"ERROR"}, "Mesh '" + me.name + "' in object '" + ob.name + "' has no vertex groups, no skeletal animation possible!")
if use_uvs and len(me.uv_layers) > 0:
uv_layer = me.uv_layers.active.data[:]
else:
uv_layer = []
if use_colors and len(me.vertex_colors) > 0:
# bug in Blender 3.6, vertex_colors.active_index might be out of bounds...
if me.vertex_colors.active_index >= 0 and me.vertex_colors.active_index < len(me.vertex_colors) and len(me.vertex_colors[me.vertex_colors.active_index].data) > 0:
vcol = me.vertex_colors[me.vertex_colors.active_index].data
else:
report({"ERROR"}, "Vertex color in mesh '" + me.name + "' in object '" + ob.name + "' has invalid out-of-bounds index (vertex_colors.active_index is " + str(me.vertex_colors.active_index) + ", largest can be " + str(len(me.vertex_colors) - 1) + ").")
# try to fallback to the first vertex_colors index
if len(me.vertex_colors[0].data) > 0:
vcol = me.vertex_colors[0].data
else:
report({"ERROR"}, "Vertex color in mesh '" + me.name + "' in object '" + ob.name + "' unable to fallback to vertex_colors[0], no data.")
vcol = []
else:
vcol = []
matnames = []
if use_materials:
for m in me.materials[:]:
if m and m.name:
matnames.append(uniquedict(strs, safestr(m.name)))
else:
matnames.append(-1)
# Ahhh finally we can get the vertices and faces
badref = {}
for pi,poly in enumerate(me.polygons):
face = [ -1, [-1,-1,-1], [-1,-1,-1], [-1,-1,-1], -1 ]
if len(matnames) > 0:
if poly.material_index < len(matnames):
i = poly.material_index
else:
i = 0
# workaround to report each bad material index only once
try:
dummy = badref[poly.material_index]
except:
badref[poly.material_index] = 1
report({"ERROR"}, "Polygon face in mesh '" + me.name + "' referencing a non-existent material (index " + str(poly.material_index) + ", largest can be " + str(len(matnames) - 1) + ").")
if i >= 0:
face[0] = matnames[i]
uniquedict(refmats, me.materials[i])
for i, li in enumerate(poly.loop_indices):
if len(vcol) > 0:
c = uniquedict(cmap, [vcol[li].color[0], vcol[li].color[1], vcol[li].color[2], vcol[li].color[3]])
else:
c = 0
v = me.vertices[poly.vertices[i]]
if use_skeleton and len(vg) > 0 and len(v.groups) > 0:
wf = 0.0
for g in v.groups:
wf += g.weight
if wf > 0.0:
skin = []
w = wi = wm = 0
for g in v.groups:
try:
s = round(g.weight / wf * 255.0)
if s > wm:
wm = s
si = len(skin)
if s < 1:
s = 1
if s > 255:
s = 255
skin.append([bones[vg[g.group].name][0], s])
w = w + s
except:
report({"ERROR"}, "Vertex group name '" + vg[g.group].name + "' does not match any bone.")
use_skeleton = False
vg = []
s = -1
break
try:
if w != 255:
skin[si][1] += 255 - w
except:
pass
s = uniquedict(skins, skin)
if len(skin) > nb_m:
nb_m = len(skin)
else:
s = -1
else:
s = -1
face[1][i] = uniquedict(verts, vert(
round(v.co.x, digits),
round(v.co.y, digits),
round(v.co.z, digits), 1.0, c, s))
if use_normals:
try:
no = v.normal.copy()
except:
no = poly.loops[i].normal.copy()
no.normalize()
face[3][i] = uniquedict(verts, vert(
round(no.x, digits),
round(no.y, digits),
round(no.z, digits), 1.0, 0, -1))
del no
if use_uvs and len(uv_layer) > 0:
face[2][i] = uniquedict(tmaps, list(uv_layer[li].uv[:]))
faces.append(face)
del me
progress.step()
progress.leave_substeps()
### Materials ###
if use_materials:
progress.step("Exporting Materials")
progress.enter_substeps(len(refmats))
matopa = {}
matopa[-1] = -1
for i,v in refmats.items():
mi = v[0]
mat = v[1]
if mat is not None:
props = {}
d = 1.0
if mat.node_tree:
# at least try to get the diffuse texture from other material types,
# because not all wrapped in PrincipledBSDF properly
for n in mat.node_tree.nodes:
if n.type == 'TEX_IMAGE' and n.image and n.image.filepath and n.image.filepath != "" and n.image.filepath != "//":
imgpath, data = gettexture(n.image.filepath, use_inline)
if imgpath != "":
s = uniquedict(strs, imgpath)
if use_inline and len(data) > 8:
uniquedict(inlined, [s, data])
props[128] = [128, s]
break
# otherwise properly parse material if blender can convert it into PrincipledBSDF
mat_wrap = node_shader_utils.PrincipledBSDFWrapper(mat)
if mat_wrap:
for key, mat_wrap_key in mat_property_map.items():
if key == 0:
# Kd
if mat_wrap.alpha != 0.0 and mat_wrap.alpha != 1.0:
d = mat_wrap.alpha
elif mat_wrap.base_color and len(mat_wrap.base_color) > 3:
d = mat_wrap.base_color[3]
else:
d = 0.0
if d != 0.0:
props[0] = [0, uniquedict(cmap, [mat_wrap.base_color[0], mat_wrap.base_color[1], mat_wrap.base_color[2], d])]
elif key == 8:
# il
il = 0
if mat_wrap.specular == 0:
il = 1
elif mat_wrap.metallic != 0.0:
if d != 1.0:
il = 6
else:
il = 3
elif d != 1.0:
il = 9
else:
il = 2
if il != 0:
props[8] = [8, il]
elif mat_wrap_key[0][0:2] == "//":
continue
try:
val = getattr(mat_wrap, mat_wrap_key[1], None)
except:
continue
if val is None:
continue
if key >= 128:
# according to the doc, texture material attributes should always have val.image
# but sometimes they don't... And sometimes filename is "//" for whatever reason...
if val.image is None or val.image.filepath is None or val.image.filepath == "" or val.image.filepath == "//":
continue
imgpath, data = gettexture(val.image.filepath, use_inline)
if imgpath == "":
continue
s = uniquedict(strs, imgpath)
props[key] = [key, s]
if use_inline and len(data) > 8:
uniquedict(inlined, [s, data])
elif mat_wrap_key[0] == "gscale" and val != 0.0:
props[key] = [key, uniquedict(cmap, [val, val, val, 1.0])]
elif mat_wrap_key[0] == "color" and len(val) == 3:
props[key] = [key, uniquedict(cmap, [val[0], val[1], val[2], 1.0])]
elif mat_wrap_key[0] == "color" and len(val) == 4:
props[key] = [key, uniquedict(cmap, val)]
elif mat_wrap_key[0] == "float" and val != 0.0:
props[key] = [key, val]
elif (mat_wrap_key[0] == "byte" or mat_wrap_key[0] == "int") and val != 0:
props[key] = [key, val]
else:
report({"ERROR"}, "Material '" + mat.name + "' does not use PrincipledBSDF surface, not parsing.")
# append material if it has at least one property
if len(props) > 0:
ni = uniquedict(strs, safestr(mat.name))
matopa[ni] = 255 - int(255.0 * d)
materials.append([ni, props])
progress.step()
progress.leave_substeps()
# sort faces by opacity and material index
for i,v in enumerate(faces):
try:
faces[i][4] = matopa[faces[i][0]]
except:
faces[i][4] = 255
faces.sort(key=itemgetter(4,0))
else:
# sort faces by material index only
faces.sort(key=itemgetter(0))
### Actions ###
if use_animation:
progress.step("Exporting Animations")
if use_skeleton and len(bones) > 0:
mpf = 1000.0/use_fps # msec per frame
acts = []
nf = 0 # number of total frames
# collect actions from timeline markers, otherwise use actions
if use_markers == True:
if len(scene.timeline_markers) > 0:
tlm = sorted(scene.timeline_markers, key=lambda tl: tl.frame)
for i,t in enumerate(tlm):
if i + 1 >= len(tlm):
et = scene.frame_end
else:
et = tlm[i+1].frame - 1
if et > t.frame:
acts.append([safestr(t.name), -1, t.frame, et])
nf = nf + et - t.frame
del tlm
else:
for i,a in enumerate(bpy.data.actions):
# pre 3.0 blender used frame_range for the animation's length
# but post 3.0 blender uses that for a manually selected range
st = et = 0
try:
st = int(a.curve_frame_range[0])
et = int(a.curve_frame_range[1])
except: