forked from helluvamesh/GYAZ-Export-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ops.py
619 lines (448 loc) · 23.3 KB
/
ops.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
# ##### 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, subprocess, bmesh
from bpy.types import Operator
from bpy.props import *
from mathutils import Vector
def report (self, item, error_or_info):
self.report({error_or_info}, item)
class Op_GYAZ_Export_SelectFileInWindowsFileExplorer (bpy.types.Operator):
bl_idname = "object.gyaz_export_select_file_in_explorer"
bl_label = "GYAZ Export: Select File in Explorer"
bl_description = "Select File in file explorer"
path: StringProperty (default='', options={'SKIP_SAVE'})
# operator function
def execute(self, context):
path = os.path.abspath ( bpy.path.abspath (self.path) )
subprocess.Popen(r'explorer /select,'+path)
return {'FINISHED'}
class Op_GYAZ_Export_OpenFolderInWindowsFileExplorer (bpy.types.Operator):
bl_idname = "object.gyaz_export_open_folder_in_explorer"
bl_label = "GYAZ Export: Open Folder in Explorer"
bl_description = "Open folder in file explorer"
path: StringProperty (default='', options={'SKIP_SAVE'})
# operator function
def execute(self, context):
path = os.path.abspath ( bpy.path.abspath (self.path) )
subprocess.Popen ('explorer "'+path+'"')
return {'FINISHED'}
class Op_GYAZ_Export_SavePreset (bpy.types.Operator):
bl_idname = "object.gyaz_export_save_preset"
bl_label = "GYAZ Export: Save Preset"
bl_description = "Save preset"
ui_name: StringProperty (name = 'name', default = '')
# popup with properties
def invoke(self, context, event):
wm = bpy.context.window_manager
return wm.invoke_props_dialog(self)
# operator function
def execute(self, context):
preset_name = self.ui_name
scene = bpy.context.scene
preset_name_no_whitespace = preset_name.replace (" ", "")
if preset_name_no_whitespace == '-' or preset_name_no_whitespace == '':
report (self, "Invalid name.", 'WARNING')
else:
# check for existing preset with the same name
prefs = bpy.context.preferences.addons[__package__].preferences
presets = prefs.bone_presets
preset_names = [preset.preset_name for preset in presets]
if preset_name in preset_names:
preset = presets[preset_name.index (preset_name)]
else:
# add preset
preset = presets.add ()
scene_extra_bones = getattr (scene.gyaz_export, "extra_bones")
scene_export_bones = getattr (scene.gyaz_export, "export_bones")
scene_active_preset = getattr (scene.gyaz_export, "active_preset")
preset_extra_bones = getattr (preset, "extra_bones")
preset_export_bones = getattr (preset, "export_bones")
preset_extra_bones.clear ()
preset_export_bones.clear ()
for scene_item in scene_extra_bones:
preset_item = preset_extra_bones.add ()
preset_item.name = scene_item.name
preset_item.source = scene_item.source
preset_item.parent = scene_item.parent
for scene_item in scene_export_bones:
preset_item = preset_export_bones.add ()
preset_item.name = scene_item.name
setattr (preset, 'preset_name', preset_name)
setattr (preset, 'root_mode', scene.gyaz_export.root_mode)
setattr (preset, 'root_bone_name', scene.gyaz_export.root_bone_name)
setattr (preset, 'export_all_bones', scene.gyaz_export.export_all_bones)
setattr (preset, 'constraint_extra_bones', scene.gyaz_export.constraint_extra_bones)
setattr (preset, 'rename_vert_groups_to_extra_bones', scene.gyaz_export.rename_vert_groups_to_extra_bones)
scene.gyaz_export.active_preset = preset.preset_name
# save user preferences
bpy.context.area.type = 'PREFERENCES'
bpy.ops.wm.save_userpref()
bpy.context.area.type = 'PROPERTIES'
return {'FINISHED'}
class Op_GYAZ_Export_RemovePreset (bpy.types.Operator):
bl_idname = "object.gyaz_export_remove_preset"
bl_label = "GYAZ Export: Remove Preset"
bl_description = "Remove preset"
# operator function
def execute(self, context):
scene = bpy.context.scene
prefs = bpy.context.preferences.addons[__package__].preferences
scene_active_preset = getattr (scene.gyaz_export, "active_preset")
presets = prefs.bone_presets
preset_len = len (presets)
if preset_len > 0:
preset_names = [preset.preset_name for preset in presets]
if scene_active_preset in preset_names:
index = preset_names.index (scene_active_preset)
presets.remove (index)
if preset_len > 1:
preset_names = [preset.preset_name for preset in presets]
scene.gyaz_export.active_preset = preset_names[0]
else:
scene.gyaz_export.property_unset ("extra_bones")
scene.gyaz_export.property_unset ("export_bones")
scene.gyaz_export.property_unset ("export_all_bones")
scene.gyaz_export.property_unset ("root_mode")
scene.gyaz_export.property_unset ("root_bone_name")
scene.gyaz_export.property_unset ("constraint_extra_bones")
scene.gyaz_export.property_unset ("rename_vert_groups_to_extra_bones")
# save user preferences
bpy.context.area.type = 'PREFERENCES'
bpy.ops.wm.save_userpref()
bpy.context.area.type = 'PROPERTIES'
return {'FINISHED'}
class Op_GYAZ_Export_Functions (bpy.types.Operator):
bl_idname = "object.gyaz_export_functions"
bl_label = "GYAZ Export: Functions"
bl_description = ""
ui_mode: EnumProperty(
name = 'mode',
items = (
('ADD_TO_EXTRA_BONES', '', ''),
('REMOVE_ALL_FROM_EXTRA_BONES', '', ''),
('ADD_TO_EXPORT_BONES', '', ''),
('REMOVE_ALL_FROM_EXPORT_BONES', '', ''),
('ADD_TO_EXPORT_ACTIONS', '', ''),
('REMOVE_ALL_FROM_EXPORT_ACTIONS', '', '')
),
default = "ADD_TO_EXTRA_BONES")
# operator function
def execute(self, context):
mode = self.ui_mode
scene = bpy.context.scene
scene_extra_bones = getattr (scene.gyaz_export, "extra_bones")
scene_export_bones = getattr (scene.gyaz_export, "export_bones")
if mode == 'ADD_TO_EXTRA_BONES':
item = scene_extra_bones.add ()
item.name = ''
item.merge_to = ''
scene.gyaz_export.extra_bones_active_index = len (scene_extra_bones) - 1
elif mode == 'REMOVE_ALL_FROM_EXTRA_BONES':
scene_extra_bones.clear ()
scene.gyaz_export.extra_bones_active_index = -1
elif mode == 'ADD_TO_EXPORT_BONES':
item = scene_export_bones.add ()
item.name = ''
item.merge_to = ''
elif mode == 'REMOVE_ALL_FROM_EXPORT_BONES':
scene_export_bones.clear ()
elif mode == 'ADD_TO_EXPORT_ACTIONS':
item = scene.gyaz_export.actions.add ()
item.name = ''
item.merge_to = ''
elif mode == 'REMOVE_ALL_FROM_EXPORT_ACTIONS':
scene.gyaz_export.actions.clear ()
return {'FINISHED'}
class Op_GYAZ_Export_ReadSelectedPoseBones (bpy.types.Operator):
bl_idname = "object.gyaz_export_read_selected_pose_bones"
bl_label = "GYAZ Export: Read Selected Pose Bones"
bl_description = "Read selected pose bones"
mode: EnumProperty (items=(('EXPORT_BONES', 'Export Bones', ''), ('EXTRA_BONES', 'Extra Bones', '')), default='EXPORT_BONES')
def execute(self, context):
scene = bpy.context.scene
rig = bpy.context.active_object
mode = self.mode
if mode == 'EXPORT_BONES':
collection = getattr (scene.gyaz_export, "export_bones")
prop = 'name'
elif mode == 'EXTRA_BONES':
collection = getattr (scene.gyaz_export, "extra_bones")
prop = 'source'
if bpy.context.mode == 'POSE':
selected_visible_bones = []
bones = rig.data.bones
selected_visible_bones = [pbone.name for pbone in bpy.context.selected_pose_bones]
names_already_in_collection = set ( getattr (item, prop) for item in collection )
for name in selected_visible_bones:
if name not in names_already_in_collection:
item = collection.add ()
setattr (item, prop, name)
return {'FINISHED'}
class Op_GYAZ_Export_MarkAllSelectedForExport (bpy.types.Operator):
bl_idname = "object.gyaz_export_mark_all_selected_for_export"
bl_label = "GYAZ Export: Mark All Selected For Export"
bl_description = "All selected objects for export"
# operator function
def execute(self, context):
armature = bpy.context.active_object.type == 'ARMATURE'
scene = bpy.context.scene
selected_objects = bpy.context.selected_objects
active_object = bpy.context.active_object
if armature:
for child in active_object.children:
child.gyaz_export.export = True
else:
for obj in selected_objects:
obj.gyaz_export.export = True
return {'FINISHED'}
class Op_GYAZ_Export_MarkAllSelectedNotForExport (bpy.types.Operator):
bl_idname = "object.gyaz_export_mark_all_selected_not_for_export"
bl_label = "GYAZ Export: Mark All Selected Not For Export"
bl_description = "All selected objects not for export"
# operator function
def execute(self, context):
armature = bpy.context.active_object.type == 'ARMATURE'
scene = bpy.context.scene
selected_objects = bpy.context.selected_objects
active_object = bpy.context.active_object
if not armature:
for obj in selected_objects:
obj.gyaz_export.export = False
elif armature:
for child in active_object.children:
child.gyaz_export.export = False
return {'FINISHED'}
class Op_GYAZ_Export_MarkAllForExport (bpy.types.Operator):
bl_idname = "object.gyaz_export_mark_all_for_export"
bl_label = "GYAZ Export: Mark All Objects For Export"
bl_description = "All scene objects for export"
# operator function
def execute(self, context):
scene = bpy.context.scene
for obj in scene.objects:
obj.gyaz_export.export = True
return {'FINISHED'}
class Op_GYAZ_Export_MarkAllNotForExport (bpy.types.Operator):
bl_idname = "object.gyaz_export_mark_all_not_for_export"
bl_label = "GYAZ Export: Mark All Not For Export"
bl_description = "All scene objects not for export"
# operator function
def execute(self, context):
scene = bpy.context.scene
for obj in scene.objects:
obj.gyaz_export.export = False
return {'FINISHED'}
class Op_GYAZ_Export_RemoveItemFromExtraBones (bpy.types.Operator):
bl_idname = "object.gyaz_export_remove_item_from_extra_bones"
bl_label = "GYAZ Export: Remove Item From Extra Bones"
bl_description = "Remove item"
# operator function
def execute(self, context):
scene = bpy.context.scene
index = scene.gyaz_export.extra_bones_active_index
scene.gyaz_export.extra_bones.remove (index)
if len (scene.gyaz_export.extra_bones) < index + 1:
scene.gyaz_export.extra_bones_active_index -= 1
return {'FINISHED'}
class Op_GYAZ_Export_RemoveItemFromExportBones (bpy.types.Operator):
bl_idname = "object.gyaz_export_remove_item_from_export_bones"
bl_label = "GYAZ Export: Remove Item From Export Bones"
bl_description = "Remove item"
ui_index: IntProperty (name='', default=0)
# operator function
def execute(self, context):
index = self.ui_index
scene = bpy.context.scene
scene.gyaz_export.export_bones.remove (index)
return {'FINISHED'}
class Op_GYAZ_Export_RemoveItemFromActions (bpy.types.Operator):
bl_idname = "object.gyaz_export_remove_item_from_actions"
bl_label = "GYAZ Export: Remove Item From Action Names"
bl_description = "Remove item"
ui_index: IntProperty (name='', default=0)
# operator function
def execute(self, context):
index = self.ui_index
scene = bpy.context.scene
scene.gyaz_export.actions.remove (index)
return {'FINISHED'}
class Op_GYAZ_Export_SetSourceAsActiveBone (bpy.types.Operator):
bl_idname = "object.gyaz_export_set_source_as_active_bone"
bl_label = "GYAZ Export: Set Source As Active Bone"
bl_description = "Set active bone"
ui_index: IntProperty (name='', default=0)
# operator function
def execute(self, context):
index = self.ui_index
scene = bpy.context.scene
if bpy.context.mode == 'POSE':
scene.gyaz_export.extra_bones[index].source = bpy.context.active_bone.name
return {'FINISHED'}
class Op_GYAZ_Export_SetParentAsActiveBone (bpy.types.Operator):
bl_idname = "object.gyaz_export_set_parent_as_active_bone"
bl_label = "GYAZ Export: Set Parent As Active Bone"
bl_description = "Set active bone"
ui_index: IntProperty (name='', default=0)
# operator function
def execute(self, context):
index = self.ui_index
scene = bpy.context.scene
if bpy.context.mode == 'POSE':
scene.gyaz_export.extra_bones[index].parent = bpy.context.active_bone.name
return {'FINISHED'}
class Op_GYAZ_Export_SetNameAsActiveBone (bpy.types.Operator):
bl_idname = "object.gyaz_export_set_name_as_active_bone"
bl_label = "GYAZ Export: Set Name As Active Bone"
bl_description = "Set active bone"
ui_index: IntProperty (name='', default=0)
# operator function
def execute(self, context):
index = self.ui_index
scene = bpy.context.scene
scene_export_bones = getattr (scene.gyaz_export, "export_bones")
if bpy.context.mode == 'POSE':
bone = bpy.context.active_bone
print (bone)
if bone == None:
report (self, 'No active bone.', 'WARNING')
else:
name = bone.name
names_already_in_collection = []
for item in scene_export_bones:
names_already_in_collection.append (item.name)
# set name
if name not in names_already_in_collection:
scene_export_bones[index].name = name
else:
report (self, 'Bone is already listed.', 'INFO')
return {'FINISHED'}
class Op_GYAZ_Export_MoveExtraBoneItem (bpy.types.Operator):
bl_idname = "object.gyaz_export_move_extra_bone_item"
bl_label = "GYAZ Export: Move Up/Down Extra Bone Item"
bl_description = "Move up/down"
mode: EnumProperty (items=(('UP', 'UP', ''), ('DOWN', 'DOWN', '')), default='UP')
index: IntProperty (name='', default=0)
# operator function
def execute(self, context):
mode = self.mode
scene = bpy.context.scene
index = scene.gyaz_export.extra_bones_active_index
rig = bpy.context.active_object
if mode == 'UP':
if index >= 1:
target_index = index-1
else:
target_index = 0
scene.gyaz_export.extra_bones.move (index, target_index)
if len (scene.gyaz_export.extra_bones) > 0:
scene.gyaz_export.extra_bones_active_index -= 1
elif mode == 'DOWN':
scene_extra_bones = getattr (scene.gyaz_export, "extra_bones")
length = len (scene_extra_bones.items ())
last_index = length-1
if index < last_index:
target_index = index+1
else:
target_index = last_index
scene_extra_bones.move (index, target_index)
if len (scene.gyaz_export.extra_bones) > scene.gyaz_export.extra_bones_active_index + 1:
scene.gyaz_export.extra_bones_active_index += 1
return {'FINISHED'}
class Op_GYAZ_Export_SetActiveActionToExport (bpy.types.Operator):
bl_idname = "object.gyaz_export_set_active_action_to_export"
bl_label = "GYAZ Export: Set Active Action to Export"
bl_description = "Set active action"
ui_index: IntProperty (name='', default=0)
# operator function
def execute(self, context):
index = self.ui_index
scene = bpy.context.scene
# get active action's name
obj = bpy.context.active_object
action = get_active_action (obj)
action_name = action.name if action is not None else ''
# make list of already listed actions
already_listed_actions = []
for item in scene.gyaz_export.actions:
already_listed_actions.append (item.name)
if action == None:
report (self, 'No action found.', 'INFO')
elif action_name in already_listed_actions:
report (self, 'Action is already listed.', 'INFO')
else:
scene.gyaz_export.actions[index].name = action_name
return {'FINISHED'}
class Op_GYAZ_Export_SetFilterType (bpy.types.Operator):
bl_idname = "object.gyaz_export_set_filter_type"
bl_label = "GYAZ Export: Set Filter Type"
bl_description = "Filter Type"
# operator function
def execute(self, context):
owner = bpy.context.scene.gyaz_export
value = owner.filter_type
if value == 'START':
owner.filter_type = 'IN'
if value == 'IN':
owner.filter_type = 'END'
if value == 'END':
owner.filter_type = 'START'
return {'FINISHED'}
def register():
bpy.utils.register_class (Op_GYAZ_Export_MarkAllSelectedForExport)
bpy.utils.register_class (Op_GYAZ_Export_MarkAllSelectedNotForExport)
bpy.utils.register_class (Op_GYAZ_Export_MarkAllForExport)
bpy.utils.register_class (Op_GYAZ_Export_MarkAllNotForExport)
bpy.utils.register_class (Op_GYAZ_Export_SelectFileInWindowsFileExplorer)
bpy.utils.register_class (Op_GYAZ_Export_OpenFolderInWindowsFileExplorer)
bpy.utils.register_class (Op_GYAZ_Export_SavePreset)
bpy.utils.register_class (Op_GYAZ_Export_RemovePreset)
bpy.utils.register_class (Op_GYAZ_Export_Functions)
bpy.utils.register_class (Op_GYAZ_Export_ReadSelectedPoseBones)
bpy.utils.register_class (Op_GYAZ_Export_RemoveItemFromExtraBones)
bpy.utils.register_class (Op_GYAZ_Export_RemoveItemFromExportBones)
bpy.utils.register_class (Op_GYAZ_Export_SetSourceAsActiveBone)
bpy.utils.register_class (Op_GYAZ_Export_SetParentAsActiveBone)
bpy.utils.register_class (Op_GYAZ_Export_SetNameAsActiveBone)
bpy.utils.register_class (Op_GYAZ_Export_MoveExtraBoneItem)
bpy.utils.register_class (Op_GYAZ_Export_RemoveItemFromActions)
bpy.utils.register_class (Op_GYAZ_Export_SetActiveActionToExport)
bpy.utils.register_class (Op_GYAZ_Export_SetFilterType)
def unregister():
bpy.utils.unregister_class (Op_GYAZ_Export_MarkAllSelectedForExport)
bpy.utils.unregister_class (Op_GYAZ_Export_MarkAllSelectedNotForExport)
bpy.utils.unregister_class (Op_GYAZ_Export_MarkAllForExport)
bpy.utils.unregister_class (Op_GYAZ_Export_MarkAllNotForExport)
bpy.utils.unregister_class (Op_GYAZ_Export_SelectFileInWindowsFileExplorer)
bpy.utils.unregister_class (Op_GYAZ_Export_OpenFolderInWindowsFileExplorer)
bpy.utils.unregister_class (Op_GYAZ_Export_SavePreset)
bpy.utils.unregister_class (Op_GYAZ_Export_RemovePreset)
bpy.utils.unregister_class (Op_GYAZ_Export_Functions)
bpy.utils.unregister_class (Op_GYAZ_Export_ReadSelectedPoseBones)
bpy.utils.unregister_class (Op_GYAZ_Export_RemoveItemFromExtraBones)
bpy.utils.unregister_class (Op_GYAZ_Export_RemoveItemFromExportBones)
bpy.utils.unregister_class (Op_GYAZ_Export_SetSourceAsActiveBone)
bpy.utils.unregister_class (Op_GYAZ_Export_SetParentAsActiveBone)
bpy.utils.unregister_class (Op_GYAZ_Export_SetNameAsActiveBone)
bpy.utils.unregister_class (Op_GYAZ_Export_MoveExtraBoneItem)
bpy.utils.unregister_class (Op_GYAZ_Export_RemoveItemFromActions)
bpy.utils.unregister_class (Op_GYAZ_Export_SetActiveActionToExport)
bpy.utils.unregister_class (Op_GYAZ_Export_SetFilterType)
if __name__ == "__main__":
register()