Repository navigation
Expand file tree
/
Copy pathfbx_utils.py
More file actions
1931 lines (1601 loc) · 86.2 KB
/
Copy pathfbx_utils.py
File metadata and controls
1931 lines (1601 loc) · 86.2 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
# SPDX-FileCopyrightText: 2013 Campbell Barton
# SPDX-FileCopyrightText: 2014 Bastien Montagne
#
# SPDX-License-Identifier: GPL-2.0-or-later
import math
import time
from collections import namedtuple
from collections.abc import Iterable
from itertools import zip_longest, chain
from dataclasses import dataclass, field
from typing import Callable
import numpy as np
import bpy
import bpy_extras
from bpy.types import Object, Bone, PoseBone, DepsgraphObjectInstance
from mathutils import Vector, Matrix
from . import encode_bin, data_types
# "Constants"
FBX_VERSION = 7400
# 1004 adds use of "OtherFlags"->"TCDefinition" to control the FBX_KTIME opt-in in FBX version 7700.
FBX_HEADER_VERSION = 1003
FBX_SCENEINFO_VERSION = 100
FBX_TEMPLATES_VERSION = 100
FBX_MODELS_VERSION = 232
FBX_GEOMETRY_VERSION = 124
# Revert back normals to 101 (simple 3D values) for now, 102 (4D + weights) seems not well supported by most apps
# currently, apart from some AD products.
FBX_GEOMETRY_NORMAL_VERSION = 101
FBX_GEOMETRY_BINORMAL_VERSION = 101
FBX_GEOMETRY_TANGENT_VERSION = 101
FBX_GEOMETRY_SMOOTHING_VERSION = 102
FBX_GEOMETRY_CREASE_VERSION = 101
FBX_GEOMETRY_VCOLOR_VERSION = 101
FBX_GEOMETRY_UV_VERSION = 101
FBX_GEOMETRY_MATERIAL_VERSION = 101
FBX_GEOMETRY_LAYER_VERSION = 100
FBX_GEOMETRY_SHAPE_VERSION = 100
FBX_DEFORMER_SHAPE_VERSION = 100
FBX_DEFORMER_SHAPECHANNEL_VERSION = 100
FBX_POSE_BIND_VERSION = 100
FBX_DEFORMER_SKIN_VERSION = 101
FBX_DEFORMER_CLUSTER_VERSION = 100
FBX_MATERIAL_VERSION = 102
FBX_TEXTURE_VERSION = 202
FBX_ANIM_KEY_VERSION = 4008
FBX_NAME_CLASS_SEP = b"\x00\x01"
FBX_ANIM_PROPSGROUP_NAME = "d"
FBX_KTIME_V7 = 46186158000 # This is the number of "ktimes" in one second (yep, precision over the nanosecond...)
# FBX 2019.5 (FBX version 7700) changed the number of "ktimes" per second, however, the new value is opt-in until FBX
# version 8000 where it will probably become opt-out.
FBX_KTIME_V8 = 141120000
# To explicitly use the V7 value in FBX versions 7700-7XXX: fbx_root->"FBXHeaderExtension"->"OtherFlags"->"TCDefinition"
# is set to 127.
# To opt in to the V8 value in FBX version 7700-7XXX: "TCDefinition" is set to 0.
FBX_TIMECODE_DEFINITION_TO_KTIME_PER_SECOND = {
0: FBX_KTIME_V8,
127: FBX_KTIME_V7,
}
# The "ktimes" per second for Blender exported FBX is constant because the exported `FBX_VERSION` is constant.
FBX_KTIME = FBX_KTIME_V8 if FBX_VERSION >= 8000 else FBX_KTIME_V7
MAT_CONVERT_LIGHT = Matrix.Rotation(math.pi / 2.0, 4, 'X') # Blender is -Z, FBX is -Y.
MAT_CONVERT_CAMERA = Matrix.Rotation(math.pi / 2.0, 4, 'Y') # Blender is -Z, FBX is +X.
# XXX I can't get this working :(
# MAT_CONVERT_BONE = Matrix.Rotation(math.pi / 2.0, 4, 'Z') # Blender is +Y, FBX is -X.
MAT_CONVERT_BONE = Matrix()
BLENDER_OTHER_OBJECT_TYPES = {'CURVE', 'SURFACE', 'FONT', 'META'}
BLENDER_OBJECT_TYPES_MESHLIKE = {'MESH'} | BLENDER_OTHER_OBJECT_TYPES
SHAPE_KEY_SLIDER_HARD_MIN = bpy.types.ShapeKey.bl_rna.properties["slider_min"].hard_min
SHAPE_KEY_SLIDER_HARD_MAX = bpy.types.ShapeKey.bl_rna.properties["slider_max"].hard_max
# Lamps.
FBX_LIGHT_TYPES = {
'POINT': 0, # Point.
'SUN': 1, # Directional.
'SPOT': 2, # Spot.
'HEMI': 1, # Directional.
'AREA': 3, # Area.
}
FBX_LIGHT_DECAY_TYPES = {
'CONSTANT': 0, # None.
'INVERSE_LINEAR': 1, # Linear.
'INVERSE_SQUARE': 2, # Quadratic.
'INVERSE_COEFFICIENTS': 2, # Quadratic...
'CUSTOM_CURVE': 2, # Quadratic.
'LINEAR_QUADRATIC_WEIGHTED': 2, # Quadratic.
}
RIGHT_HAND_AXES = {
# Up, Forward -> FBX values (tuples of (axis, sign), Up, Front, Coord).
('X', '-Y'): ((0, 1), (1, 1), (2, 1)),
('X', 'Y'): ((0, 1), (1, -1), (2, -1)),
('X', '-Z'): ((0, 1), (2, 1), (1, -1)),
('X', 'Z'): ((0, 1), (2, -1), (1, 1)),
('-X', '-Y'): ((0, -1), (1, 1), (2, -1)),
('-X', 'Y'): ((0, -1), (1, -1), (2, 1)),
('-X', '-Z'): ((0, -1), (2, 1), (1, 1)),
('-X', 'Z'): ((0, -1), (2, -1), (1, -1)),
('Y', '-X'): ((1, 1), (0, 1), (2, -1)),
('Y', 'X'): ((1, 1), (0, -1), (2, 1)),
('Y', '-Z'): ((1, 1), (2, 1), (0, 1)),
('Y', 'Z'): ((1, 1), (2, -1), (0, -1)),
('-Y', '-X'): ((1, -1), (0, 1), (2, 1)),
('-Y', 'X'): ((1, -1), (0, -1), (2, -1)),
('-Y', '-Z'): ((1, -1), (2, 1), (0, -1)),
('-Y', 'Z'): ((1, -1), (2, -1), (0, 1)),
('Z', '-X'): ((2, 1), (0, 1), (1, 1)),
('Z', 'X'): ((2, 1), (0, -1), (1, -1)),
('Z', '-Y'): ((2, 1), (1, 1), (0, -1)),
('Z', 'Y'): ((2, 1), (1, -1), (0, 1)), # Blender system!
('-Z', '-X'): ((2, -1), (0, 1), (1, -1)),
('-Z', 'X'): ((2, -1), (0, -1), (1, 1)),
('-Z', '-Y'): ((2, -1), (1, 1), (0, 1)),
('-Z', 'Y'): ((2, -1), (1, -1), (0, -1)),
}
# NOTE: Not fully in enum value order, since when exporting the first entry matching the framerate value is used
# (e.g. better have NTSC fullframe than NTSC drop frame for 29.97 framerate).
FBX_FRAMERATES = (
# (-1.0, 0), # Default framerate.
(-1.0, 14), # Custom framerate.
(120.0, 1),
(100.0, 2),
(60.0, 3),
(50.0, 4),
(48.0, 5),
(30.0, 6), # BW NTSC, full frame.
(30.0, 7), # Drop frame.
(30.0 / 1.001, 9), # Color NTSC, full frame.
(30.0 / 1.001, 8), # Color NTSC, drop frame.
(25.0, 10),
(24.0, 11),
# (1.0, 12), # 1000 milli/s (use for date time?).
(24.0 / 1.001, 13),
(96.0, 15),
(72.0, 16),
(60.0 / 1.001, 17),
(120.0 / 1.001, 18),
)
# ##### Misc utilities #####
# Enable performance reports (measuring time used to perform various steps of importing or exporting).
DO_PERFMON = False
if DO_PERFMON:
class PerfMon():
def __init__(self):
self.level = -1
self.ref_time = []
def level_up(self, message=""):
self.level += 1
self.ref_time.append(None)
if message:
print("\t" * self.level, message, sep="")
def level_down(self, message=""):
if not self.ref_time:
if message:
print(message)
return
ref_time = self.ref_time[self.level]
print("\t" * self.level,
"\tDone (%f sec)\n" % ((time.process_time() - ref_time) if ref_time is not None else 0.0),
sep="")
if message:
print("\t" * self.level, message, sep="")
del self.ref_time[self.level]
self.level -= 1
def step(self, message=""):
ref_time = self.ref_time[self.level]
curr_time = time.process_time()
if ref_time is not None:
print("\t" * self.level, "\tDone (%f sec)\n" % (curr_time - ref_time), sep="")
self.ref_time[self.level] = curr_time
print("\t" * self.level, message, sep="")
else:
class PerfMon():
def __init__(self):
pass
def level_up(self, message=""):
pass
def level_down(self, message=""):
pass
def step(self, message=""):
pass
# Scale/unit mess. FBX can store the 'reference' unit of a file in its UnitScaleFactor property
# (1.0 meaning centimeter, afaik). We use that to reflect user's default unit as set in Blender with scale_length.
# However, we always get values in BU (i.e. meters), so we have to reverse-apply that scale in global matrix...
# Note that when no default unit is available, we assume 'meters' (and hence scale by 100).
def units_blender_to_fbx_factor(scene):
return 100.0 if (scene.unit_settings.system == 'NONE') else (100.0 * scene.unit_settings.scale_length)
# Note: this could be in a utility (math.units e.g.)...
UNITS = {
"meter": 1.0, # Ref unit!
"kilometer": 0.001,
"millimeter": 1000.0,
"foot": 1.0 / 0.3048,
"inch": 1.0 / 0.0254,
"turn": 1.0, # Ref unit!
"degree": 360.0,
"radian": math.pi * 2.0,
"second": 1.0, # Ref unit!
"ktime": FBX_KTIME, # For export use only because the imported "ktimes" per second may vary.
}
def units_convertor(u_from, u_to):
"""Return a convertor between specified units."""
conv = UNITS[u_to] / UNITS[u_from]
return lambda v: v * conv
def units_convertor_iter(u_from, u_to):
"""Return an iterable convertor between specified units."""
conv = units_convertor(u_from, u_to)
def convertor(it):
for v in it:
yield(conv(v))
return convertor
def matrix4_to_array(mat):
"""Concatenate matrix's columns into a single, flat tuple"""
# blender matrix is row major, fbx is col major so transpose on write
return tuple(f for v in mat.transposed() for f in v)
def array_to_matrix4(arr):
"""Convert a single 16-len tuple into a valid 4D Blender matrix"""
# Blender matrix is row major, fbx is col major so transpose on read
return Matrix(tuple(zip(*[iter(arr)] * 4))).transposed()
def parray_as_ndarray(arr):
"""Convert an array.array into an np.ndarray that shares the same memory"""
return np.frombuffer(arr, dtype=arr.typecode)
def similar_values(v1, v2, e=1e-6):
"""Return True if v1 and v2 are nearly the same."""
if v1 == v2:
return True
return ((abs(v1 - v2) / max(abs(v1), abs(v2))) <= e)
def similar_values_iter(v1, v2, e=1e-6):
"""Return True if iterables v1 and v2 are nearly the same."""
if v1 == v2:
return True
for v1, v2 in zip(v1, v2):
if (v1 != v2) and ((abs(v1 - v2) / max(abs(v1), abs(v2))) > e):
return False
return True
def shape_difference_exclude_similar(sv_cos, ref_cos, e=1e-6):
"""Return a tuple of:
the difference between the vertex cos in sv_cos and ref_cos, excluding any that are nearly the same,
and the indices of the vertices that are not nearly the same"""
assert(sv_cos.size == ref_cos.size)
# Create views of 1 co per row of the arrays, only making copies if needed.
sv_cos = sv_cos.reshape(-1, 3)
ref_cos = ref_cos.reshape(-1, 3)
# Quick check for equality
if np.array_equal(sv_cos, ref_cos):
# There's no difference between the two arrays.
empty_cos = np.empty((0, 3), dtype=sv_cos.dtype)
empty_indices = np.empty(0, dtype=np.int32)
return empty_cos, empty_indices
# Note that unlike math.isclose(a,b), np.isclose(a,b) is not symmetrical and the second argument 'b', is
# considered to be the reference value.
# Note that atol=0 will mean that if only one co component being compared is zero, they won't be considered close.
similar_mask = np.isclose(sv_cos, ref_cos, atol=0, rtol=e)
# A co is only similar if every component in it is similar.
co_similar_mask = np.all(similar_mask, axis=1)
# Get the indices of cos that are not similar.
not_similar_verts_idx = np.flatnonzero(~co_similar_mask)
# Subtracting first over the entire arrays and then indexing seems faster than indexing both arrays first and then
# subtracting, until less than about 3% of the cos are being indexed.
difference_cos = (sv_cos - ref_cos)[not_similar_verts_idx]
return difference_cos, not_similar_verts_idx
def _mat4_vec3_array_multiply(mat4, vec3_array, dtype=None, return_4d=False):
"""Multiply a 4d matrix by each 3d vector in an array and return as an array of either 3d or 4d vectors.
A view of the input array is returned if return_4d=False, the dtype matches the input array and either the matrix is
None or, ignoring the last row, is a 3x3 identity matrix with no translation:
┌1, 0, 0, 0┐
│0, 1, 0, 0│
└0, 0, 1, 0┘
When dtype=None, it defaults to the dtype of the input array."""
return_dtype = dtype if dtype is not None else vec3_array.dtype
vec3_array = vec3_array.reshape(-1, 3)
# Multiplying a 4d mathutils.Matrix by a 3d mathutils.Vector implicitly extends the Vector to 4d during the
# calculation by appending 1.0 to the Vector and then the 4d result is truncated back to 3d.
# Numpy does not do an implicit extension to 4d, so it would have to be done explicitly by extending the entire
# vec3_array to 4d.
# However, since the w component of the vectors is always 1.0, the last column can be excluded from the
# multiplication and then added to every multiplied vector afterwards, which avoids having to make a 4d copy of
# vec3_array beforehand.
# For a single column vector:
# ┌a, b, c, d┐ ┌x┐ ┌ax+by+cz+d┐
# │e, f, g, h│ @ │y│ = │ex+fy+gz+h│
# │i, j, k, l│ │z│ │ix+jy+kz+l│
# └m, n, o, p┘ └1┘ └mx+ny+oz+p┘
# ┌a, b, c┐ ┌x┐ ┌d┐ ┌ax+by+cz┐ ┌d┐ ┌ax+by+cz+d┐
# │e, f, g│ @ │y│ + │h│ = │ex+fy+gz│ + │h│ = │ex+fy+gz+h│
# │i, j, k│ └z┘ │l│ │ix+jy+kz│ │l│ │ix+jy+kz+l│
# └m, n, o┘ └p┘ └mx+ny+oz┘ └p┘ └mx+ny+oz+p┘
# column_vector_multiplication in mathutils_Vector.c uses double precision math for Matrix @ Vector by casting the
# matrix's values to double precision and then casts back to single precision when returning the result, so at least
# double precision math is always be used to match standard Blender behaviour.
math_precision = np.result_type(np.double, vec3_array)
to_multiply = None
to_add = None
w_to_set = 1.0
if mat4 is not None:
mat_np = np.array(mat4, dtype=math_precision)
# Identity matrix is compared against to check if any matrix multiplication is required.
identity = np.identity(4, dtype=math_precision)
if not return_4d:
# If returning 3d, the entire last row of the matrix can be ignored because it only affects the w component.
mat_np = mat_np[:3]
identity = identity[:3]
# Split mat_np into the columns to multiply and the column to add afterwards.
# First 3 columns
multiply_columns = mat_np[:, :3]
multiply_identity = identity[:, :3]
# Last column only
add_column = mat_np.T[3]
# Analyze the split parts of the matrix to figure out if there is anything to multiply and anything to add.
if not np.array_equal(multiply_columns, multiply_identity):
to_multiply = multiply_columns
if return_4d and to_multiply is None:
# When there's nothing to multiply, the w component of add_column can be set directly into the array because
# mx+ny+oz+p becomes 0x+0y+0z+p where p is add_column[3].
w_to_set = add_column[3]
# Replace add_column with a view of only the translation.
add_column = add_column[:3]
if add_column.any():
to_add = add_column
if to_multiply is None:
# If there's anything to add, ensure it's added using the precision being used for math.
array_dtype = math_precision if to_add is not None else return_dtype
if return_4d:
multiplied_vectors = np.empty((len(vec3_array), 4), dtype=array_dtype)
multiplied_vectors[:, :3] = vec3_array
multiplied_vectors[:, 3] = w_to_set
else:
# If there's anything to add, ensure a copy is made so that the input vec3_array isn't modified.
multiplied_vectors = vec3_array.astype(array_dtype, copy=to_add is not None)
else:
# Matrix multiplication has the signature (n,k) @ (k,m) -> (n,m).
# Where v is the number of vectors in vec3_array and d is the number of vector dimensions to return:
# to_multiply has shape (d,3), vec3_array has shape (v,3) and the result should have shape (v,d).
# Either vec3_array or to_multiply must be transposed:
# Can transpose vec3_array and then transpose the result:
# (v,3).T -> (3,v); (d,3) @ (3,v) -> (d,v); (d,v).T -> (v,d)
# Or transpose to_multiply and swap the order of multiplication:
# (d,3).T -> (3,d); (v,3) @ (3,d) -> (v,d)
# There's no, or negligible, performance difference between the two options, however, the result of the latter
# will be C contiguous in memory, making it faster to convert to flattened bytes with .tobytes().
multiplied_vectors = vec3_array @ to_multiply.T
if to_add is not None:
for axis, to_add_to_axis in zip(multiplied_vectors.T, to_add):
if to_add_to_axis != 0:
axis += to_add_to_axis
# Cast to the desired return type before returning.
return multiplied_vectors.astype(return_dtype, copy=False)
def vcos_transformed(raw_cos, m=None, dtype=None):
return _mat4_vec3_array_multiply(m, raw_cos, dtype)
def nors_transformed(raw_nors, m=None, dtype=None):
# Great, now normals are also expected 4D!
# XXX Back to 3D normals for now!
# return _mat4_vec3_array_multiply(m, raw_nors, dtype, return_4d=True)
return _mat4_vec3_array_multiply(m, raw_nors, dtype)
def astype_view_signedness(arr, new_dtype):
"""Unsafely views arr as new_dtype if the itemsize and byteorder of arr matches but the signedness does not.
Safely views arr as new_dtype if both arr and new_dtype have the same itemsize, byteorder and signedness, but could
have a different character code, e.g. 'i' and 'l'. np.ndarray.astype with copy=False does not normally create this
view, but Blender can be picky about the character code used, so this function will create the view.
Otherwise, calls np.ndarray.astype with copy=False.
The benefit of copy=False is that if the array can be safely viewed as the new type, then a view is made, instead of
a copy with the new type.
Unsigned types can't be viewed safely as signed or vice-versa, meaning that a copy would always be made by
.astype(..., copy=False).
This is intended for viewing uintc data (a common Blender C type with variable itemsize, though usually 4 bytes, so
uint32) as int32 (a common FBX type), when the itemsizes match."""
arr_dtype = arr.dtype
if not isinstance(new_dtype, np.dtype):
# new_dtype could be a type instance or a string, but it needs to be a dtype to compare its itemsize, byteorder
# and kind.
new_dtype = np.dtype(new_dtype)
# For simplicity, only dtypes of the same itemsize and byteorder, but opposite signedness, are handled. Everything
# else is left to .astype.
arr_kind = arr_dtype.kind
new_kind = new_dtype.kind
# Signed and unsigned int are opposite in terms of signedness. Other types don't have signedness.
integer_kinds = {'i', 'u'}
if (
arr_kind in integer_kinds and new_kind in integer_kinds
and arr_dtype.itemsize == new_dtype.itemsize
and arr_dtype.byteorder == new_dtype.byteorder
):
# arr and new_dtype have signedness and matching itemsize and byteorder, so return a view of the new type.
return arr.view(new_dtype)
else:
return arr.astype(new_dtype, copy=False)
def fast_first_axis_flat(ar):
"""Get a flat view (or a copy if a view is not possible) of the input array whereby each element is a single element
of a dtype that is fast to sort, sorts according to individual bytes and contains the data for an entire row (and
any further dimensions) of the input array.
Since the dtype of the view could sort in a different order to the dtype of the input array, this isn't typically
useful for actual sorting, but it is useful for sorting-based uniqueness, such as np.unique."""
# If there are no rows, each element will be viewed as the new dtype.
elements_per_row = math.prod(ar.shape[1:])
row_itemsize = ar.itemsize * elements_per_row
# Get a dtype with itemsize that equals row_itemsize.
# Integer types sort the fastest, but are only available for specific itemsizes.
uint_dtypes_by_itemsize = {1: np.uint8, 2: np.uint16, 4: np.uint32, 8: np.uint64}
# Signed/unsigned makes no noticeable speed difference, but using unsigned will result in ordering according to
# individual bytes like the other, non-integer types.
if row_itemsize in uint_dtypes_by_itemsize:
entire_row_dtype = uint_dtypes_by_itemsize[row_itemsize]
else:
# When using kind='stable' sorting, numpy only uses radix sort with integer types, but it's still
# significantly faster to sort by a single item per row instead of multiple row elements or multiple structured
# type fields.
# Construct a flexible size dtype with matching itemsize.
# Should always be 4 because each character in a unicode string is UCS4.
str_itemsize = np.dtype((np.str_, 1)).itemsize
if row_itemsize % str_itemsize == 0:
# Unicode strings seem to be slightly faster to sort than bytes.
entire_row_dtype = np.dtype((np.str_, row_itemsize // str_itemsize))
else:
# Bytes seem to be slightly faster to sort than raw bytes (np.void).
entire_row_dtype = np.dtype((np.bytes_, row_itemsize))
# View each element along the first axis as a single element.
# View (or copy if a view is not possible) as flat
ar = ar.reshape(-1)
# To view as a dtype of different size, the last axis (entire array in NumPy 1.22 and earlier) must be C-contiguous.
if row_itemsize != ar.itemsize and not ar.flags.c_contiguous:
ar = np.ascontiguousarray(ar)
return ar.view(entire_row_dtype)
def fast_first_axis_unique(ar, return_unique=True, return_index=False, return_inverse=False, return_counts=False):
"""np.unique with axis=0 but optimised for when the input array has multiple elements per row, and the returned
unique array doesn't need to be sorted.
Arrays with more than one element per row are more costly to sort in np.unique due to being compared one
row-element at a time, like comparing tuples.
By viewing each entire row as a single non-structured element, much faster sorting can be achieved. Since the values
are viewed as a different type to their original, this means that the returned array of unique values may not be
sorted according to their original type.
The array of unique values can be excluded from the returned tuple by specifying return_unique=False.
Float type caveats:
All elements of -0.0 in the input array will be replaced with 0.0 to ensure that both values are collapsed into one.
NaN values can have lots of different byte representations (e.g. signalling/quiet and custom payloads). Only the
duplicates of each unique byte representation will be collapsed into one."""
# At least something should always be returned.
assert(return_unique or return_index or return_inverse or return_counts)
# Only signed integer, unsigned integer and floating-point kinds of data are allowed. Other kinds of data have not
# been tested.
assert(ar.dtype.kind in "iuf")
# Floating-point types have different byte representations for -0.0 and 0.0. Collapse them together by replacing all
# -0.0 in the input array with 0.0.
if ar.dtype.kind == 'f':
ar[ar == -0.0] = 0.0
# It's a bit annoying that the unique array is always calculated even when it might not be needed, but it is
# generally insignificant compared to the cost of sorting.
result = np.unique(fast_first_axis_flat(ar), return_index=return_index,
return_inverse=return_inverse, return_counts=return_counts)
if return_unique:
unique = result[0] if isinstance(result, tuple) else result
# View in the original dtype.
unique = unique.view(ar.dtype)
# Return the same number of elements per row and any extra dimensions per row as the input array.
unique.shape = (-1, *ar.shape[1:])
if isinstance(result, tuple):
return (unique,) + result[1:]
else:
return unique
else:
# Remove the first element, the unique array.
result = result[1:]
if len(result) == 1:
# Unpack single element tuples.
return result[0]
else:
return result
def ensure_object_not_in_edit_mode(context, obj):
"""Objects in Edit mode usually cannot be exported because much of the API used when exporting is not available for
Objects in Edit mode.
Exiting the currently active Object (and any other Objects opened in multi-editing) from Edit mode is simple and
should be done with `bpy.ops.mesh.mode_set(mode='OBJECT')` instead of using this function.
This function is for the rare case where an Object is in Edit mode, but the current context mode is not Edit mode.
This can occur from a state where the current context mode is Edit mode, but then the active Object of the current
View Layer is changed to a different Object that is not in Edit mode. This changes the current context mode, but
leaves the other Object(s) in Edit mode.
"""
if obj.mode != 'EDIT':
return True
# Get the active View Layer.
view_layer = context.view_layer
# A View Layer belongs to a scene.
scene = view_layer.id_data
# Get the current active Object of this View Layer, so we can restore it once done.
orig_active = view_layer.objects.active
# Check if obj is in the View Layer. If obj is not in the View Layer, it cannot be set as the active Object.
# We don't use `obj.name in view_layer.objects` because an Object from a Library could have the same name.
is_in_view_layer = any(o == obj for o in view_layer.objects)
do_unlink_from_scene_collection = False
try:
if not is_in_view_layer:
# There might not be any enabled collections in the View Layer, so link obj into the Scene Collection
# instead, which is always available to all View Layers of that Scene.
scene.collection.objects.link(obj)
do_unlink_from_scene_collection = True
view_layer.objects.active = obj
# Now we're finally ready to attempt to change obj's mode.
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='OBJECT')
if obj.mode == 'EDIT':
# The Object could not be set out of EDIT mode and therefore cannot be exported.
return False
finally:
# Always restore the original active Object and unlink obj from the Scene Collection if it had to be linked.
view_layer.objects.active = orig_active
if do_unlink_from_scene_collection:
scene.collection.objects.unlink(obj)
return True
def expand_shape_key_range(shape_key, value_to_fit):
"""Attempt to expand the slider_min/slider_max of a shape key to fit `value_to_fit` within the slider range,
expanding slightly beyond `value_to_fit` if possible, so that the new slider_min/slider_max is not the same as
`value_to_fit`. Blender has a hard minimum and maximum for slider values, so it may not be possible to fit the value
within the slider range.
If `value_to_fit` is already within the slider range, no changes are made.
First tries setting slider_min/slider_max to double `value_to_fit`, otherwise, expands the range in the direction of
`value_to_fit` by double the distance to `value_to_fit`.
The new slider_min/slider_max is rounded down/up to the nearest whole number for a more visually pleasing result.
Returns whether it was possible to expand the slider range to fit `value_to_fit`."""
if value_to_fit < (slider_min := shape_key.slider_min):
if value_to_fit < 0.0:
# For the most common case, set slider_min to double value_to_fit.
target_slider_min = value_to_fit * 2.0
else:
# Doubling value_to_fit would make it larger, so instead decrease slider_min by double the distance between
# slider_min and value_to_fit.
target_slider_min = slider_min - (slider_min - value_to_fit) * 2.0
# Set slider_min to the first whole number less than or equal to target_slider_min.
shape_key.slider_min = math.floor(target_slider_min)
return value_to_fit >= SHAPE_KEY_SLIDER_HARD_MIN
elif value_to_fit > (slider_max := shape_key.slider_max):
if value_to_fit > 0.0:
# For the most common case, set slider_max to double value_to_fit.
target_slider_max = value_to_fit * 2.0
else:
# Doubling value_to_fit would make it smaller, so instead increase slider_max by double the distance between
# slider_max and value_to_fit.
target_slider_max = slider_max + (value_to_fit - slider_max) * 2.0
# Set slider_max to the first whole number greater than or equal to target_slider_max.
shape_key.slider_max = math.ceil(target_slider_max)
return value_to_fit <= SHAPE_KEY_SLIDER_HARD_MAX
else:
# Value is already within the range.
return True
# ##### Attribute utils. #####
AttributeDataTypeInfo = namedtuple("AttributeDataTypeInfo", ["dtype", "foreach_attribute", "item_size"])
_attribute_data_type_info_lookup = {
'FLOAT': AttributeDataTypeInfo(np.single, "value", 1),
'INT': AttributeDataTypeInfo(np.intc, "value", 1),
'FLOAT_VECTOR': AttributeDataTypeInfo(np.single, "vector", 3),
'FLOAT_COLOR': AttributeDataTypeInfo(np.single, "color", 4), # color_srgb is an alternative
'BYTE_COLOR': AttributeDataTypeInfo(np.single, "color", 4), # color_srgb is an alternative
'STRING': AttributeDataTypeInfo(None, "value", 1), # Not usable with foreach_get/set
'BOOLEAN': AttributeDataTypeInfo(bool, "value", 1),
'FLOAT2': AttributeDataTypeInfo(np.single, "vector", 2),
'INT8': AttributeDataTypeInfo(np.intc, "value", 1),
'INT32_2D': AttributeDataTypeInfo(np.intc, "value", 2),
}
def attribute_get(attributes, name, data_type, domain):
"""Get an attribute by its name, data_type and domain.
Returns None if no attribute with this name, data_type and domain exists."""
attr = attributes.get(name)
if not attr:
return None
if attr.data_type == data_type and attr.domain == domain:
return attr
# It shouldn't normally happen, but it's possible there are multiple attributes with the same name, but different
# data_types or domains.
for attr in attributes:
if attr.name == name and attr.data_type == data_type and attr.domain == domain:
return attr
return None
def attribute_foreach_set(attribute, array_or_list, foreach_attribute=None):
"""Set every value of an attribute with foreach_set."""
if foreach_attribute is None:
foreach_attribute = _attribute_data_type_info_lookup[attribute.data_type].foreach_attribute
attribute.data.foreach_set(foreach_attribute, array_or_list)
def attribute_to_ndarray(attribute, foreach_attribute=None):
"""Create a NumPy ndarray from an attribute."""
data = attribute.data
data_type_info = _attribute_data_type_info_lookup[attribute.data_type]
ndarray = np.empty(len(data) * data_type_info.item_size, dtype=data_type_info.dtype)
if foreach_attribute is None:
foreach_attribute = data_type_info.foreach_attribute
data.foreach_get(foreach_attribute, ndarray)
return ndarray
@dataclass
class AttributeDescription:
"""Helper class to reduce duplicate code for handling built-in Blender attributes."""
name: str
# Valid identifiers can be found in bpy.types.Attribute.bl_rna.properties["data_type"].enum_items
data_type: str
# Valid identifiers can be found in bpy.types.Attribute.bl_rna.properties["domain"].enum_items
domain: str
# Some attributes are required to exist if certain conditions are met. If a required attribute does not exist when
# attempting to get it, an AssertionError is raised.
is_required_check: Callable[[bpy.types.AttributeGroup], bool] = None
# NumPy dtype that matches the internal C data of this attribute.
dtype: np.dtype = field(init=False)
# The default attribute name to use with foreach_get and foreach_set.
foreach_attribute: str = field(init=False)
# The number of elements per value of the attribute when flattened into a 1-dimensional list/array.
item_size: int = field(init=False)
def __post_init__(self):
data_type_info = _attribute_data_type_info_lookup[self.data_type]
self.dtype = data_type_info.dtype
self.foreach_attribute = data_type_info.foreach_attribute
self.item_size = data_type_info.item_size
def is_required(self, attributes):
"""Check if the attribute is required to exist in the provided attributes."""
is_required_check = self.is_required_check
return is_required_check and is_required_check(attributes)
def get(self, attributes):
"""Get the attribute.
If the attribute is required, but does not exist, an AssertionError is raised, otherwise None is returned."""
attr = attribute_get(attributes, self.name, self.data_type, self.domain)
if not attr and self.is_required(attributes):
raise AssertionError("Required attribute '%s' with type '%s' and domain '%s' not found in %r"
% (self.name, self.data_type, self.domain, attributes))
return attr
def ensure(self, attributes):
"""Get the attribute, creating it if it does not exist.
Raises a RuntimeError if the attribute could not be created, which should only happen when attempting to create
an attribute with a reserved name, but with the wrong data_type or domain. See usage of
BuiltinCustomDataLayerProvider in Blender source for most reserved names.
There is no guarantee that the returned attribute has the desired name because the name could already be in use
by another attribute with a different data_type and/or domain."""
attr = self.get(attributes)
if attr:
return attr
attr = attributes.new(self.name, self.data_type, self.domain)
if not attr:
raise RuntimeError("Could not create attribute '%s' with type '%s' and domain '%s' in %r"
% (self.name, self.data_type, self.domain, attributes))
return attr
def foreach_set(self, attributes, array_or_list, foreach_attribute=None):
"""Get the attribute, creating it if it does not exist, and then set every value in the attribute."""
attribute_foreach_set(self.ensure(attributes), array_or_list, foreach_attribute)
def get_ndarray(self, attributes, foreach_attribute=None):
"""Get the attribute and if it exists, return a NumPy ndarray containing its data, otherwise return None."""
attr = self.get(attributes)
return attribute_to_ndarray(attr, foreach_attribute) if attr else None
def to_ndarray(self, attributes, foreach_attribute=None):
"""Get the attribute and if it exists, return a NumPy ndarray containing its data, otherwise return a
zero-length ndarray."""
ndarray = self.get_ndarray(attributes, foreach_attribute)
return ndarray if ndarray is not None else np.empty(0, dtype=self.dtype)
# Built-in Blender attributes
# Only attributes used by the importer/exporter are included here.
# See usage of BuiltinCustomDataLayerProvider in Blender source to find most built-in attributes.
MESH_ATTRIBUTE_MATERIAL_INDEX = AttributeDescription("material_index", 'INT', 'FACE')
MESH_ATTRIBUTE_POSITION = AttributeDescription("position", 'FLOAT_VECTOR', 'POINT',
is_required_check=lambda attributes: bool(attributes.id_data.vertices))
MESH_ATTRIBUTE_SHARP_EDGE = AttributeDescription("sharp_edge", 'BOOLEAN', 'EDGE')
MESH_ATTRIBUTE_EDGE_VERTS = AttributeDescription(".edge_verts", 'INT32_2D', 'EDGE',
is_required_check=lambda attributes: bool(attributes.id_data.edges))
MESH_ATTRIBUTE_CORNER_VERT = AttributeDescription(".corner_vert", 'INT', 'CORNER',
is_required_check=lambda attributes: bool(attributes.id_data.loops))
MESH_ATTRIBUTE_CORNER_EDGE = AttributeDescription(".corner_edge", 'INT', 'CORNER',
is_required_check=lambda attributes: bool(attributes.id_data.loops))
MESH_ATTRIBUTE_SHARP_FACE = AttributeDescription("sharp_face", 'BOOLEAN', 'FACE')
# ##### UIDs code. #####
# ID class (mere int).
class UUID(int):
pass
# UIDs storage.
_keys_to_uuids = {}
_uuids_to_keys = {}
def _key_to_uuid(uuids, key):
# TODO: Check this is robust enough for our needs!
# Note: We assume we have already checked the related key wasn't yet in _keys_to_uids!
# As int64 is signed in FBX, we keep uids below 2**63...
if isinstance(key, int) and 0 <= key < 2**63:
# We can use value directly as id!
uuid = key
else:
uuid = hash(key)
if uuid < 0:
uuid = -uuid
if uuid >= 2**63:
uuid //= 2
# Try to make our uid shorter!
if uuid > int(1e9):
t_uuid = uuid % int(1e9)
if t_uuid not in uuids:
uuid = t_uuid
# Make sure our uuid *is* unique.
if uuid in uuids:
inc = 1 if uuid < 2**62 else -1
while uuid in uuids:
uuid += inc
if 0 > uuid >= 2**63:
# Note that this is more that unlikely, but does not harm anyway...
raise ValueError("Unable to generate an UUID for key {}".format(key))
return UUID(uuid)
def get_fbx_uuid_from_key(key):
"""
Return an UUID for given key, which is assumed to be hashable.
"""
uuid = _keys_to_uuids.get(key, None)
if uuid is None:
uuid = _key_to_uuid(_uuids_to_keys, key)
_keys_to_uuids[key] = uuid
_uuids_to_keys[uuid] = key
return uuid
# XXX Not sure we'll actually need this one?
def get_key_from_fbx_uuid(uuid):
"""
Return the key which generated this uid.
"""
assert(uuid.__class__ == UUID)
return _uuids_to_keys.get(uuid, None)
# Blender-specific key generators
def get_bid_name(bid):
library = getattr(bid, "library", None)
if library is not None:
return "%s_L_%s" % (bid.name, library.name)
else:
return bid.name
def get_blenderID_key(bid):
if isinstance(bid, Iterable):
return "|".join("B" + e.rna_type.name + "#" + get_bid_name(e) for e in bid)
else:
return "B" + bid.rna_type.name + "#" + get_bid_name(bid)
def get_blenderID_name(bid):
if isinstance(bid, Iterable):
return "|".join(get_bid_name(e) for e in bid)
else:
return get_bid_name(bid)
def get_blender_empty_key(obj):
"""Return bone's keys (Model and NodeAttribute)."""
return "|".join((get_blenderID_key(obj), "Empty"))
def get_blender_mesh_shape_key(me):
"""Return main shape deformer's key."""
return "|".join((get_blenderID_key(me), "Shape"))
def get_blender_mesh_shape_channel_key(me, shape):
"""Return shape channel and geometry shape keys."""
return ("|".join((get_blenderID_key(me), "Shape", get_blenderID_key(shape))),
"|".join((get_blenderID_key(me), "Geometry", get_blenderID_key(shape))))
def get_blender_bone_key(armature, bone):
"""Return bone's keys (Model and NodeAttribute)."""
return "|".join((get_blenderID_key((armature, bone)), "Data"))
def get_blender_bindpose_key(obj, mesh):
"""Return object's bindpose key."""
return "|".join((get_blenderID_key(obj), get_blenderID_key(mesh), "BindPose"))
def get_blender_armature_skin_key(armature, mesh):
"""Return armature's skin key."""
return "|".join((get_blenderID_key(armature), get_blenderID_key(mesh), "DeformerSkin"))
def get_blender_bone_cluster_key(armature, mesh, bone):
"""Return bone's cluster key."""
return "|".join((get_blenderID_key(armature), get_blenderID_key(mesh),
get_blenderID_key(bone), "SubDeformerCluster"))
def get_blender_anim_id_base(scene, ref_id):
if ref_id is not None:
return get_blenderID_key(scene) + "|" + get_blenderID_key(ref_id)
else:
return get_blenderID_key(scene)
def get_blender_anim_stack_key(scene, ref_id):
"""Return single anim stack key."""
return get_blender_anim_id_base(scene, ref_id) + "|AnimStack"
def get_blender_anim_layer_key(scene, ref_id):
"""Return ID's anim layer key."""
return get_blender_anim_id_base(scene, ref_id) + "|AnimLayer"
def get_blender_anim_curve_node_key(scene, ref_id, obj_key, fbx_prop_name):
"""Return (stack/layer, ID, fbxprop) curve node key."""
return "|".join((get_blender_anim_id_base(scene, ref_id), obj_key, fbx_prop_name, "AnimCurveNode"))
def get_blender_anim_curve_key(scene, ref_id, obj_key, fbx_prop_name, fbx_prop_item_name):
"""Return (stack/layer, ID, fbxprop, item) curve key."""
return "|".join((get_blender_anim_id_base(scene, ref_id), obj_key, fbx_prop_name,
fbx_prop_item_name, "AnimCurve"))
def get_blender_nodetexture_key(ma, socket_names):
return "|".join((get_blenderID_key(ma), *socket_names))
# ##### Element generators. #####
# Note: elem may be None, in this case the element is not added to any parent.
def elem_empty(elem, name):
sub_elem = encode_bin.FBXElem(name)
if elem is not None:
elem.elems.append(sub_elem)
return sub_elem
def _elem_data_single(elem, name, value, func_name):
sub_elem = elem_empty(elem, name)
getattr(sub_elem, func_name)(value)
return sub_elem
def _elem_data_vec(elem, name, value, func_name):
sub_elem = elem_empty(elem, name)
func = getattr(sub_elem, func_name)
for v in value:
func(v)
return sub_elem
def elem_data_single_bool(elem, name, value):
return _elem_data_single(elem, name, value, "add_bool")
def elem_data_single_char(elem, name, value):
return _elem_data_single(elem, name, value, "add_char")
def elem_data_single_int8(elem, name, value):
return _elem_data_single(elem, name, value, "add_int8")
def elem_data_single_int16(elem, name, value):
return _elem_data_single(elem, name, value, "add_int16")
def elem_data_single_int32(elem, name, value):
return _elem_data_single(elem, name, value, "add_int32")