forked from helluvamesh/GYAZ-Export-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_op.py
1954 lines (1473 loc) · 91.3 KB
/
main_op.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 GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any laTter version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
##########################################################################################################
##########################################################################################################
import bpy, os, bmesh
from bpy.types import AddonPreferences, UIList, Scene, Object, Mesh, Menu
from mathutils import Vector, Matrix
import numpy as np
from math import radians
from pathlib import Path
from bpy.props import *
def popup (lines, icon, title):
def draw(self, context):
for line in lines:
self.layout.label(text=line)
bpy.context.window_manager.popup_menu(draw, title=title, icon=icon)
def list_to_visual_list (list):
return ", ".join(list)
def report (self, item, error_or_info):
self.report({error_or_info}, item)
def make_active_only (obj):
bpy.ops.object.mode_set (mode='OBJECT')
bpy.ops.object.select_all (action='DESELECT')
obj.select_set (True)
bpy.context.view_layer.objects.active = obj
def make_active (obj):
bpy.context.view_layer.objects.active = obj
# safe name
def sn (line):
for c in '/\:*?"<>|.,= '+"'":
line = line.replace (c, '_')
return (line)
def detect_mirrored_uvs (bm, uv_index):
uv_layer = bm.loops.layers.uv[uv_index]
mirrored_face_count = 0
for face in bm.faces:
uvs = [tuple(loop[uv_layer].uv) for loop in face.loops]
x_coords, y_coords = zip (*uvs)
result = 0.5 * np.array (np.dot(x_coords, np.roll(y_coords, 1)) - np.dot(y_coords, np.roll(x_coords, 1)))
if result > 0:
mirrored_face_count += 1
break
if mirrored_face_count > 0:
return True
else:
return False
untransformed_matrix = Matrix (([1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]))
def get_active_action (obj):
if obj.animation_data is not None:
return obj.animation_data.action
def clear_transformation (object):
for c in object.constraints:
c.mute = True
object.location = [0, 0, 0]
object.scale = [1, 1, 1]
object.rotation_quaternion = [1, 0, 0, 0]
object.rotation_euler = [0, 0, 0]
object.rotation_axis_angle = [0, 0, 1, 0]
def clear_transformation_matrix (object):
for c in object.constraints:
c.mute = True
object.matrix_world = untransformed_matrix
def _gather_images(node_tree, images):
for node in node_tree.nodes:
if node.type == 'TEX_IMAGE' or node.type == 'TEX_ENVIRONMENT':
if node is not None:
images.add(node.image)
elif node.type == 'GROUP':
_gather_images(node.node_tree, images)
# get set of texture images in node tree
def gather_images_from_nodes (node_tree):
images = set()
_gather_images(node_tree, images)
return set (images)
prefs = bpy.context.preferences.addons[__package__].preferences
# main ops
class Op_GYAZ_Export_Export (bpy.types.Operator):
bl_idname = "object.gyaz_export_export"
bl_label = "GYAZ Export: Export"
bl_description = "Export. STATIC MESHES: select one or multiple meshes, SKELETAL MESHES: select one armature, ANIMATIONS: select one armature, RIGID_ANIMATIONS: select one or multiple meshes"
asset_type_override: EnumProperty (name='Asset Type',
items=(
('DO_NOT_OVERRIDE', 'DO NOT OVERRIDE', ""),
('STATIC_MESHES', 'STATIC MESHES', ""),
('RIGID_ANIMATIONS', 'RIGID ANIMATIONS', ""),
('SKELETAL_MESHES', 'SKELETAL MESHES', ""),
('ANIMATIONS', 'SKELETAL ANIMATIONS', "")
),
default='DO_NOT_OVERRIDE', options={'SKIP_SAVE'})
# operator function
def execute (self, context):
start_mode = bpy.context.mode[:]
bpy.ops.object.mode_set (mode='OBJECT')
scene = bpy.context.scene
space = bpy.context.space_data
owner = scene.gyaz_export
scene_objects = scene.objects
ori_ao = bpy.context.active_object
ori_ao_name = bpy.context.active_object.name
mesh_children = [child for child in ori_ao.children if child.type == 'MESH' and child.gyaz_export.export]
asset_type = owner.skeletal_asset_type if ori_ao.type == 'ARMATURE' else owner.rigid_asset_type
# gather all objects from active collection
if asset_type == 'STATIC_MESHES' or asset_type == 'RIGID_ANIMATIONS':
if asset_type == 'STATIC_MESHES':
gather_from_collection = owner.static_mesh_gather_from_collection
gather_nested = owner.static_mesh_gather_nested
elif asset_type == 'RIGID_ANIMATIONS':
gather_from_collection = owner.rigid_anim_gather_from_collection
gather_nested = owner.rigid_anim_gather_nested
if not gather_from_collection:
ori_sel_objs = [obj for obj in bpy.context.selected_objects if obj.gyaz_export.export]
else:
name = ori_ao.name
collections = bpy.data.collections
x = [col for col in collections if name in col.objects]
if x:
active_collection = x[0]
objects_ = {obj for obj in active_collection.objects if obj.gyaz_export.export}
if gather_nested:
def gather(collection):
objects_.update(set(obj for obj in collection.objects if obj.gyaz_export.export))
for col in collection.children:
gather(col)
for col in active_collection.children:
gather(col)
ori_sel_objs = list(objects_)
else:
ori_sel_objs = [obj for obj in bpy.context.selected_objects if obj.gyaz_export.export]
else:
ori_sel_objs = [obj for obj in bpy.context.selected_objects if obj.gyaz_export.export]
if asset_type == 'STATIC_MESHES' or asset_type == 'RIGID_ANIMATIONS':
meshes_to_export = ori_sel_objs
elif asset_type == 'SKELETAL_MESHES' or asset_type == 'ANIMATIONS':
meshes_to_export = mesh_children
if self.asset_type_override != 'DO_NOT_OVERRIDE':
asset_type = self.asset_type_override
# LODs
export_lods = owner.export_lods if asset_type == 'STATIC_MESHES' else False
lod_info = {}
lods = []
if export_lods:
scene_objs_info = {str.lower(obj.name).replace(' ', '').replace('.', '').replace('_', ''): obj.name for obj in scene_objects}
# gather lods
for obj in meshes_to_export:
obj_lods = []
name = str.lower(obj.name).replace(' ', '').replace('.', '').replace('_', '')
for n in range (1, 10):
suffix = 'lod' + str(n)
lod_name_candidate = name + suffix
if lod_name_candidate in scene_objs_info.keys ():
lod_obj_name = scene_objs_info[lod_name_candidate]
lod_obj = scene_objects.get (lod_obj_name)
if lod_obj is not None:
if lod_obj.type == 'MESH':
obj_lods.append (lod_obj)
lods += obj_lods
lod_info[obj.name] = obj_lods
meshes_to_export += lods
lods_set = set (lods)
ori_sel_objs = list ( set (ori_sel_objs) - lods_set )
# COLLISION (mesh)
export_collision = owner.export_collision
collision_info = {}
collision_objs_ori = set ()
if asset_type == 'STATIC_MESHES' and export_collision:
prefixes = ['UBX', 'USP', 'UCP', 'UCX']
for obj in meshes_to_export:
name = obj.name
obj_cols = set ()
for prefix in prefixes:
col = scene_objects.get (prefix+'_'+name)
if col is not None:
obj_cols.add ((col, prefix+'_'+name+'_00'))
collision_objs_ori.add (scene_objects.get (col.name))
for n in range (1, 100):
suffix = '.00'+str(n) if n < 10 else '.0'+str(n)
col = scene_objects.get (prefix+'_'+name+suffix)
collision_objs_ori.add (col)
if col is not None:
suffix = '_0'+str(n) if n < 10 else '_'+str(n)
obj_cols.add ((col, prefix+'_'+name+suffix))
collision_objs_ori.add (scene_objects.get (col.name))
if len (obj_cols) > 0:
collision_info[name] = obj_cols
ori_sel_objs = list ( set (ori_sel_objs) - collision_objs_ori )
meshes_to_export = list ( set (meshes_to_export) - collision_objs_ori )
root_folder = owner.export_folder
if asset_type == 'STATIC_MESHES':
pack_objects = owner.static_mesh_pack_objects
pack_name = owner.static_mesh_pack_name
elif asset_type == 'SKELETAL_MESHES' or asset_type == 'ANIMATIONS':
pack_objects = owner.skeletal_mesh_pack_objects
pack_name = ori_ao_name
elif asset_type == 'RIGID_ANIMATIONS':
pack_objects = owner.rigid_anim_pack_objects
pack_name = owner.rigid_anim_pack_name
if asset_type == 'STATIC_MESHES':
export_vert_colors = owner.static_mesh_vcolors
elif asset_type == 'SKELETAL_MESHES':
export_vert_colors = owner.skeletal_mesh_vcolors
elif asset_type == 'RIGID_ANIMATIONS':
export_vert_colors = owner.rigid_anim_vcolors
else:
export_vert_colors = False
if asset_type == 'SKELETAL_MESHES' or asset_type == 'ANIMATIONS':
export_shape_keys = owner.skeletal_shapes
elif asset_type == 'RIGID_ANIMATIONS':
export_shape_keys = owner.rigid_anim_shapes
else:
export_shape_keys = False
action_export_mode = owner.action_export_mode
rigid_anim_cubes = True if owner.rigid_anim_cubes and not pack_objects else False
# root bone name
root_bone_name = owner.root_bone_name
def main (asset_type, image_info, image_set, ori_ao, ori_ao_name, ori_sel_objs, mesh_children, meshes_to_export, root_folder, pack_objects, action_export_mode, pack_name, lod_info, lods, export_collision, collision_info, export_sockets):
###############################################################
#SAVE .BLEND FILE BEFORE DOING ANYTHING
###############################################################
file_is_saved = False
blend_data = bpy.context.blend_data
blend_path = blend_data.filepath
if blend_data.is_saved:
bpy.ops.wm.save_as_mainfile (filepath=blend_path)
file_is_saved = True
else:
report (self, 'File has never been saved.', 'WARNING')
file_is_saved = False
###############################################################
def set_bone_parent (name, parent_name):
ebones = bpy.context.active_object.data.edit_bones
ebone = ebones.get (name)
if ebone is not None:
if parent_name == None:
ebone.parent = None
else:
ebone.parent = ebones.get (parent_name)
######################################################
# MAIN VARIABLES
######################################################
scene_objects = bpy.context.scene.objects
owner = scene.gyaz_export
# make sure all objects are selectable
values = [False] * len (scene_objects)
scene_objects.foreach_set ('hide_select', values)
# make list of bones to keep
if asset_type == 'SKELETAL_MESHES' or asset_type == 'ANIMATIONS':
if not scene.gyaz_export.export_all_bones:
export_bones = scene.gyaz_export.export_bones
else:
export_bones = ori_ao.data.bones
export_bone_list = [item.name for item in export_bones]
extra_bones = scene.gyaz_export.extra_bones
extra_bone_list = [item.name for item in extra_bones]
bone_list = export_bone_list + extra_bone_list
if root_bone_name in bone_list:
bone_list.remove (root_bone_name)
if root_bone_name in export_bone_list:
export_bone_list.remove (root_bone_name)
if root_bone_name in extra_bone_list:
extra_bone_list.remove (root_bone_name)
# define clear transforms
if asset_type == 'STATIC_MESHES':
clear_transforms = scene.gyaz_export.static_mesh_clear_transforms
elif asset_type == 'SKELETAL_MESHES' or asset_type == 'ANIMATIONS':
clear_transforms = scene.gyaz_export.skeletal_clear_transforms
# get root mode
if asset_type == 'SKELETAL_MESHES' or asset_type == 'ANIMATIONS':
root_mode = scene.gyaz_export.root_mode
# make all collectios visible
for collection in bpy.context.scene.collection.children:
# collection.exclude doesn't work
try:
#collection.exclude = False
collection.hide_viewport = False
collection.hide_select = False
for obj in collection.objects:
obj.hide_viewport, obj.hide_select = False, False
obj.hide_set (False)
except:
''
length = len (scene.gyaz_export.extra_bones)
constraint_extra_bones = scene.gyaz_export.constraint_extra_bones if length > 0 else False
rename_vert_groups_to_extra_bones = scene.gyaz_export.rename_vert_groups_to_extra_bones if length > 0 else False
# file format
exporter = scene.gyaz_export.exporter
if exporter == 'FBX':
format = '.fbx'
#######################################################
# EXPORT FOLDER
#######################################################
export_folder_mode = scene.gyaz_export.export_folder_mode
relative_folder_name = scene.gyaz_export.relative_folder_name
export_folder = scene.gyaz_export.export_folder
#export folder
if export_folder_mode == 'RELATIVE_FOLDER':
relative_folder_path = "//" + relative_folder_name
#make sure it is an absolute path
root_folder = os.path.abspath ( bpy.path.abspath (relative_folder_path) )
#create relative folder
os.makedirs(root_folder, exist_ok=True)
elif export_folder_mode == 'PATH':
#make sure it is an absolute path
root_folder = os.path.abspath ( bpy.path.abspath (export_folder) )
############################################################
# GATHER COLLISION & SOCKETS
############################################################
collision_objects = []
sockets = []
if asset_type == 'STATIC_MESHES':
if export_collision:
# geather collision (mesh objects)
collision_info_keys = collision_info.keys ()
for obj in ori_sel_objs:
obj_name = obj.name
if obj_name in collision_info_keys:
cols = collision_info [obj_name]
for col, new_name in cols:
# rename collision object
col.name = new_name
col.name = new_name
collision_objects.append (col)
# parent collision to obj
collision_info_keys = collision_info.keys ()
for obj in ori_sel_objs:
obj_name = obj.name
if obj_name in collision_info_keys:
cols = collision_info [obj_name]
for col, new_name in cols:
collision = scene.objects.get (new_name)
if collision is not None:
collision.parent = obj
collision.matrix_parent_inverse = obj.matrix_world.inverted ()
# gather and rescale sockets (single bone armature objects)
export_sockets = owner.export_sockets
if export_sockets:
for obj in ori_sel_objs:
for child in obj.children:
if child.type == 'ARMATURE' and child.name.startswith ('SOCKET_'):
child.scale = (1, 1, 1)
child.location *= 100
sockets.append (child)
# clear lod transform
if export_lods:
lod_info_keys = set (lod_info.keys ())
for obj in ori_sel_objs:
obj_name = obj.name
if obj_name in lod_info_keys:
lods = lod_info[obj_name]
for lod in lods:
if clear_transforms:
clear_transformation(lod)
#######################################################
# REPLACE SKELETAL MESHES WITH CUBES
# don't want to have high poly meshes in every animation file
#######################################################
if asset_type == 'ANIMATIONS' or asset_type == 'RIGID_ANIMATIONS' and rigid_anim_cubes:
bpy.ops.object.mode_set (mode='OBJECT')
objs = mesh_children if asset_type == 'ANIMATIONS' else ori_sel_objs
for obj in objs:
if obj.type == 'MESH':
# replace all mesh data with a cube (keeping the original mesh object)
mesh = obj.data
bm = bmesh.new ()
bm.from_mesh (mesh)
bm.clear ()
bmesh.ops.create_cube(bm, size=0.1, calc_uvs=True)
bm.to_mesh (mesh)
bm.free ()
# you have to make the shapekeys modify the mesh otherwise the fbx exporter won't export it
if obj.data.shape_keys is not None:
for key_index, key_block in enumerate(obj.data.shape_keys.key_blocks):
if key_index != 0:
for i in range (0, 8):
key_block.data[i].co += Vector ((0, 0, 1))
# remove materials
slots = obj.material_slots
for slot in slots:
slot.material = None
#######################################################
# REMOVE ANIMATION AND CENTER OBJECTS
#######################################################
# remove actions from all objects that need to be exported
bpy.ops.object.mode_set (mode='OBJECT')
if asset_type == 'STATIC_MESHES':
if clear_transforms:
for obj in ori_sel_objs:
obj.animation_data_clear ()
clear_transformation (obj)
if asset_type == 'SKELETAL_MESHES':
# remove action
if hasattr (ori_ao, "animation_data"):
if ori_ao.get ("animation_data") == True:
ori_ao.animation_data.action = None
for obj in ori_sel_objs:
obj.animation_data_clear ()
clear_transformation_matrix (ori_ao)
if clear_transforms:
for obj in ori_ao.children:
clear_transformation_matrix (obj)
# remove bone constarints
make_active_only (ori_ao)
bpy.ops.object.mode_set (mode='POSE')
for pbone in ori_ao.pose.bones:
cs = pbone.constraints
for c in cs:
cs.remove (c)
# reset bone transforms
for pbone in ori_ao.pose.bones:
pbone.location = [0, 0, 0]
pbone.scale = [1, 1, 1]
pbone.rotation_quaternion = [1, 0, 0, 0]
pbone.rotation_euler = [0, 0, 0]
pbone.rotation_axis_angle = [0, 0, 1, 0]
elif asset_type == 'ANIMATIONS':
if root_mode == 'BONE':
clear_transformation_matrix (ori_ao)
for obj in mesh_children:
if clear_transforms:
clear_transformation_matrix (obj)
if action_export_mode != 'ACTIVE':
obj.animation_data_clear ()
#######################################################
# BUILD FINAL RIG
#######################################################
if asset_type == 'SKELETAL_MESHES' or asset_type == 'ANIMATIONS':
make_active_only (ori_ao)
bpy.ops.object.mode_set (mode='EDIT')
ebones = ori_ao.data.edit_bones
bones = ori_ao.data.bones
# extra bone info
extra_bone_info = []
extra_bones = scene.gyaz_export.extra_bones
for item in extra_bones:
ebone = ebones.get (item.source)
if ebone is not None:
info = {'name': item.name, 'head': ebone.head[:], 'tail': ebone.tail[:], 'roll': ebone.roll, 'parent': item.parent}
extra_bone_info.append (info)
bpy.ops.object.mode_set (mode='OBJECT')
# duplicate armature
final_rig_data = ori_ao.data.copy ()
# create new armature object
final_rig = bpy.data.objects.new (name=root_bone_name, object_data=final_rig_data)
scene.collection.objects.link (final_rig)
make_active_only (final_rig)
# renaming just once may not be enough
final_rig.name = root_bone_name
final_rig.name = root_bone_name
# remove drivers
if hasattr (final_rig_data, "animation_data") == True:
if final_rig_data.animation_data is not None:
for driver in final_rig_data.animation_data.drivers:
final_rig_data.driver_remove (driver.data_path)
# delete bones
bpy.ops.object.mode_set (mode='EDIT')
all_bones = set (bone.name for bone in final_rig_data.bones)
bones_to_remove = all_bones - set (export_bone_list)
bones_to_remove.add (root_bone_name)
for name in bones_to_remove:
ebones = final_rig_data.edit_bones
ebone = ebones.get (name)
if ebone is not None:
ebones.remove (ebone)
# create extra bones
for item in extra_bone_info:
ebone = final_rig.data.edit_bones.new (name=item['name'])
ebone.head = item['head']
ebone.tail = item['tail']
ebone.roll = item['roll']
for item in extra_bone_info:
set_bone_parent (item['name'], item['parent'] )
# delete constraints
bpy.ops.object.mode_set (mode='POSE')
for pbone in final_rig.pose.bones:
for c in pbone.constraints:
pbone.constraints.remove (c)
# make all bones visible
for n in range (0, 32):
final_rig_data.layers[n] = True
for bone in final_rig_data.bones:
bone.hide = False
# make sure bones export with correct scale
# bpy.ops.object.mode_set (mode='OBJECT')
# final_rig.scale = (100, 100, 100)
# bpy.ops.object.transform_apply (location=False, rotation=False, scale=True, properties=False)
# final_rig.delta_scale = (0.01, 0.01, 0.01)
# bind meshes to the final rig
# for child in mesh_children:
# child.delta_scale[0] *= 100
# child.delta_scale[1] *= 100
# child.delta_scale[2] *= 100
# for child in mesh_children:
# child.parent = final_rig
# child.matrix_parent_inverse = final_rig.matrix_world.inverted ()
# child.parent_type = 'ARMATURE'
# constraint final rig to the original armature
make_active_only (final_rig)
bpy.ops.object.mode_set (mode='POSE')
# def constraint_bones (source_bone, target_bone, source_obj, target_obj):
# pbones = target_obj.pose.bones
# pbone = pbones[target_bone]
# c = pbone.constraints.new (type='COPY_LOCATION')
# c.target = source_obj
# c.subtarget = source_bone
# c = pbone.constraints.new (type='COPY_ROTATION')
# c.target = source_obj
# c.subtarget = source_bone
# c = pbone.constraints.new (type='TRANSFORM')
# c.target = source_obj
# c.subtarget = source_bone
# c.use_motion_extrapolate = True
# c.map_from = 'SCALE'
# c.map_to = 'SCALE'
# c.map_to_x_from = 'X'
# c.map_to_y_from = 'Y'
# c.map_to_z_from = 'Z'
# c.from_min_x_scale = -1
# c.from_max_x_scale = 1
# c.from_min_y_scale = -1
# c.from_max_y_scale = 1
# c.from_min_z_scale = -1
# c.from_max_z_scale = 1
# c.to_min_x_scale = -0.01
# c.to_max_x_scale = 0.01
# c.to_min_y_scale = -0.01
# c.to_max_y_scale = 0.01
# c.to_min_z_scale = -0.01
# c.to_max_z_scale = 0.01
# constraint 'export bones'
# for name in export_bone_list:
# constraint_bones (name, name, ori_ao, final_rig)
# constraint 'extra bones'
# if constraint_extra_bones:
# for item in scene.gyaz_export.extra_bones:
# new_name = item.name
# source_name = item.source
# parent_name = item.parent
# constraint_bones (source_name, new_name, ori_ao, final_rig)
# constraint root
# if root_mode == 'BONE':
# if ori_ao.data.bones.get (root_bone_name) is not None:
# subtarget = root_bone_name
# else:
# subtarget = ''
# c = final_rig.constraints.new (type='COPY_LOCATION')
# c.target = ori_ao
# c.subtarget = subtarget
# c = final_rig.constraints.new (type='COPY_ROTATION')
# c.target = ori_ao
# c.subtarget = subtarget
# c = final_rig.constraints.new (type='TRANSFORM')
# c.target = ori_ao
# c.subtarget = subtarget
# c.use_motion_extrapolate = True
# c.map_from = 'SCALE'
# c.map_to = 'SCALE'
# c.map_to_x_from = 'X'
# c.map_to_y_from = 'Y'
# c.map_to_z_from = 'Z'
# c.from_min_x_scale = -1
# c.from_max_x_scale = 1
# c.from_min_y_scale = -1
# c.from_max_y_scale = 1
# c.from_min_z_scale = -1
# c.from_max_z_scale = 1
# c.to_min_x_scale = -0.01
# c.to_max_x_scale = 0.01
# c.to_min_y_scale = -0.01
# c.to_max_y_scale = 0.01
# c.to_min_z_scale = -0.01
# c.to_max_z_scale = 0.01
# rename vert groups to match extra bone names
if rename_vert_groups_to_extra_bones:
for mesh in mesh_children:
vgroups = mesh.vertex_groups
for item in scene.gyaz_export.extra_bones:
vgroup = vgroups.get (item.source)
if vgroup is not None:
vgroup.name = item.name
# make sure armature modifier points to the final rig
for ob in mesh_children:
for m in ob.modifiers:
if m.type == 'ARMATURE':
m.object = final_rig
bpy.ops.object.mode_set (mode='OBJECT')
#######################################################
# LIMIT BONE INFLUENCES BY VERTEX
#######################################################
if asset_type == 'SKELETAL_MESHES':
bpy.ops.object.mode_set (mode='OBJECT')
limit_prop = scene.gyaz_export.skeletal_mesh_limit_bone_influences
for child in mesh_children:
if len (child.vertex_groups) > 0:
make_active_only (child)
ctx = bpy.context.copy ()
ctx['object'] = child
bpy.ops.object.mode_set (mode='WEIGHT_PAINT')
child.data.use_paint_mask_vertex = True
bpy.ops.paint.vert_select_all (action='SELECT')
if limit_prop != 'unlimited':
limit = int (limit_prop)
bpy.ops.object.vertex_group_limit_total (ctx, group_select_mode='ALL', limit=limit)
# clean vertex weights with 0 influence
bpy.ops.object.vertex_group_clean (ctx, group_select_mode='ALL', limit=0, keep_single=False)
bpy.ops.object.mode_set (mode='OBJECT')
bpy.ops.object.select_all (action='DESELECT')
###########################################################
# REMOVE VERT COLORS, SHAPE KEYS, UVMAPS AND MERGE MATERIALS
###########################################################
# render meshes
for obj in meshes_to_export:
if obj.type == 'MESH':
mesh = obj.data
# vert colors
if not export_vert_colors:
vcolors = mesh.vertex_colors
for vc in vcolors:
vcolors.remove (vc)
else:
vcolors = mesh.vertex_colors
vcolors_to_remove = []
for index, vc in enumerate (vcolors):
if not mesh.gyaz_export.vert_color_export[index]:
vcolors_to_remove.append (vc)
for vc in reversed (vcolors_to_remove):
vcolors.remove (vc)
# shape keys
if not export_shape_keys:
if mesh.shape_keys is not None:
for key in mesh.shape_keys.key_blocks:
obj.shape_key_remove (key)
# uv maps
uvmaps = mesh.uv_layers
uvmaps_to_remove = []
for index, uvmap in enumerate (uvmaps):
if not mesh.gyaz_export.uv_export[index]:
uvmaps_to_remove.append (uvmap)
for uvmap in reversed (uvmaps_to_remove):
uvmaps.remove (uvmap)
# merge materials
if obj.data.gyaz_export.merge_materials:
mats = obj.data.materials
for mat in mats:
if len (mats) > 1:
mats.pop (index=0)
atlas_name = obj.data.gyaz_export.atlas_name
atlas_material = bpy.data.materials.get (atlas_name)
if atlas_material == None:
atlas_material = bpy.data.materials.new (name=atlas_name)
obj.material_slots[0].material = atlas_material
# collision
for obj in collision_objects:
if obj.type == 'MESH':
mesh = obj.data
vcolors = mesh.vertex_colors
for vc in vcolors:
vcolors.remove (vc)
if mesh.shape_keys is not None:
for key in mesh.shape_keys.key_blocks:
obj.shape_key_remove (key)
#######################################################
# EXPORT OPERATOR PROPS
#######################################################
# FBX EXPORTER SETTINGS:
# MAIN
use_selection = True
use_active_collection = False
global_scale = 1
apply_unit_scale = False
apply_scale_options = 'FBX_SCALE_NONE'
axis_forward = '-Z'
axis_up = 'Y'
object_types = {'EMPTY', 'CAMERA', 'LIGHT', 'ARMATURE', 'MESH', 'OTHER'}
bake_space_transform = False
use_custom_props = False
# 'STRIP' is the only mode textures are not referenced in the fbx file (only referenced not copied - this is undesirable behavior)
path_mode = 'STRIP'
batch_mode = 'OFF'
embed_textures = False
# GEOMETRIES
use_mesh_modifiers = True
use_mesh_modifiers_render = True
mesh_smooth_type = owner.mesh_smoothing
use_mesh_edges = False
use_tspace = True
# ARMATURES
use_armature_deform_only = False
add_leaf_bones = owner.add_end_bones
primary_bone_axis = owner.primary_bone_axis
secondary_bone_axis = owner.secondary_bone_axis
armature_nodetype = 'NULL'
# ANIMATION
bake_anim = False
bake_anim_use_all_bones = False
bake_anim_use_nla_strips = False
bake_anim_use_all_actions = False
bake_anim_force_startend_keying = False
bake_anim_step = 1
bake_anim_simplify_factor = 0
# asset prefixes
use_prefixes = scene.gyaz_export.use_prefixes
if use_prefixes:
static_mesh_prefix = sn(prefs.static_mesh_prefix)
skeletal_mesh_prefix = sn(prefs.skeletal_mesh_prefix)
material_prefix = sn(prefs.material_prefix)
texture_prefix = sn(prefs.texture_prefix)
animation_prefix = sn(prefs.animation_prefix)
# if prefix starts with an underscore, use a suffix instead
if static_mesh_prefix.startswith('_'):
static_mesh_suffix = static_mesh_prefix
static_mesh_prefix = ''
else:
static_mesh_suffix = ''
if skeletal_mesh_prefix.startswith('_'):
skeletal_mesh_suffix = skeletal_mesh_prefix
skeletal_mesh_prefix = ''
else:
skeletal_mesh_suffix = ''
if material_prefix.startswith('_'):
material_suffix = material_prefix
material_prefix = ''
else:
material_suffix = ''
if texture_prefix.startswith('_'):
texture_suffix = texture_prefix
texture_prefix = ''
else:
texture_suffix = ''
if animation_prefix.startswith('_'):
animation_suffix = animation_prefix
animation_prefix = ''
else:
animation_suffix = ''
else:
static_mesh_prefix = ''
skeletal_mesh_prefix = ''
material_prefix = ''
texture_prefix = ''
animation_prefix = ''
static_mesh_suffix = ''
skeletal_mesh_suffix = ''
material_suffix = ''
texture_suffix= ''
animation_suffix = ''
# folder names
textures_folder = sn(prefs.texture_folder_name) + '/'
anims_folder = sn(prefs.anim_folder_name) + '/'
meshes_folder = sn(prefs.mesh_folder_name) + '/'
###########################################################
# TEXTURE FUNCTIONS
###########################################################
# remove dot plus three numbers from the end of image names
def remove_dot_plus_three_numbers (name):
def endswith_numbers (string):
try:
int(string)
return True
except:
return False
if endswith_numbers ( name[-3:] ) == True and name[-4] == '.':
return name[:-4]
else:
return name
# texture export function