animation_animall: remove workaround for T68666
[blender-addons.git] / bone_selection_sets.py
blob0278bd23a371d2353518121deb3e10b83c9915ff
1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENCE BLOCK #####
19 bl_info = {
20 "name": "Bone Selection Sets",
21 "author": "Inês Almeida, Sybren A. Stüvel, Antony Riakiotakis, Dan Eicher",
22 "version": (2, 1, 1),
23 "blender": (2, 80, 0),
24 "location": "Properties > Object Data (Armature) > Selection Sets",
25 "description": "List of Bone sets for easy selection while animating",
26 "warning": "",
27 "wiki_url": "https://wiki.blender.org/index.php/Extensions:2.6/Py/"
28 "Scripts/Animation/SelectionSets",
29 "category": "Animation",
32 import bpy
33 from bpy.types import (
34 Operator,
35 Menu,
36 Panel,
37 UIList,
38 PropertyGroup,
40 from bpy.props import (
41 StringProperty,
42 IntProperty,
43 EnumProperty,
44 BoolProperty,
45 CollectionProperty,
49 # Data Structure ##############################################################
51 # Note: bones are stored by name, this means that if the bone is renamed,
52 # there can be problems. However, bone renaming is unlikely during animation.
53 class SelectionEntry(PropertyGroup):
54 name: StringProperty(name="Bone Name")
57 class SelectionSet(PropertyGroup):
58 name: StringProperty(name="Set Name")
59 bone_ids: CollectionProperty(type=SelectionEntry)
60 is_selected: BoolProperty(name="Is Selected")
63 # UI Panel w/ UIList ##########################################################
65 class POSE_MT_selection_sets_context_menu(Menu):
66 bl_label = "Selection Sets Specials"
68 def draw(self, context):
69 layout = self.layout
71 layout.operator("pose.selection_set_delete_all", icon='X')
72 layout.operator("pose.selection_set_remove_bones", icon='X')
73 layout.operator("pose.selection_set_copy", icon='COPYDOWN')
74 layout.operator("pose.selection_set_paste", icon='PASTEDOWN')
77 class POSE_PT_selection_sets(Panel):
78 bl_label = "Selection Sets"
79 bl_space_type = 'PROPERTIES'
80 bl_region_type = 'WINDOW'
81 bl_context = "data"
83 @classmethod
84 def poll(cls, context):
85 return (context.object and
86 context.object.type == 'ARMATURE' and
87 context.object.pose)
89 def draw(self, context):
90 layout = self.layout
92 arm = context.object
94 row = layout.row()
95 row.enabled = (context.mode == 'POSE')
97 # UI list
98 rows = 4 if len(arm.selection_sets) > 0 else 1
99 row.template_list(
100 "POSE_UL_selection_set", "", # type and unique id
101 arm, "selection_sets", # pointer to the CollectionProperty
102 arm, "active_selection_set", # pointer to the active identifier
103 rows=rows
106 # add/remove/specials UI list Menu
107 col = row.column(align=True)
108 col.operator("pose.selection_set_add", icon='ADD', text="")
109 col.operator("pose.selection_set_remove", icon='REMOVE', text="")
110 col.menu("POSE_MT_selection_sets_context_menu", icon='DOWNARROW_HLT', text="")
112 # move up/down arrows
113 if len(arm.selection_sets) > 0:
114 col.separator()
115 col.operator("pose.selection_set_move", icon='TRIA_UP', text="").direction = 'UP'
116 col.operator("pose.selection_set_move", icon='TRIA_DOWN', text="").direction = 'DOWN'
118 # buttons
119 row = layout.row()
121 sub = row.row(align=True)
122 sub.operator("pose.selection_set_assign", text="Assign")
123 sub.operator("pose.selection_set_unassign", text="Remove")
125 sub = row.row(align=True)
126 sub.operator("pose.selection_set_select", text="Select")
127 sub.operator("pose.selection_set_deselect", text="Deselect")
130 class POSE_UL_selection_set(UIList):
131 def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
132 sel_set = item
133 layout.prop(item, "name", text="", icon='GROUP_BONE', emboss=False)
134 if self.layout_type in ('DEFAULT', 'COMPACT'):
135 layout.prop(item, "is_selected", text="")
138 class POSE_MT_selection_set_create(Menu):
139 bl_label = "Choose Selection Set"
141 def draw(self, context):
142 layout = self.layout
143 layout.operator("pose.selection_set_add_and_assign",
144 text="New Selection Set")
147 class POSE_MT_selection_sets_select(Menu):
148 bl_label = 'Select Selection Set'
150 @classmethod
151 def poll(cls, context):
152 return POSE_OT_selection_set_select.poll(context)
154 def draw(self, context):
155 layout = self.layout
156 layout.operator_context = 'EXEC_DEFAULT'
157 for idx, sel_set in enumerate(context.object.selection_sets):
158 props = layout.operator(POSE_OT_selection_set_select.bl_idname, text=sel_set.name)
159 props.selection_set_index = idx
162 # Operators ###################################################################
164 class PluginOperator(Operator):
165 """Operator only available for objects of type armature in pose mode."""
166 @classmethod
167 def poll(cls, context):
168 return (context.object and
169 context.object.type == 'ARMATURE' and
170 context.mode == 'POSE')
173 class NeedSelSetPluginOperator(PluginOperator):
174 """Operator only available if the armature has a selected selection set."""
175 @classmethod
176 def poll(cls, context):
177 if not super().poll(context):
178 return False
179 arm = context.object
180 return 0 <= arm.active_selection_set < len(arm.selection_sets)
183 class POSE_OT_selection_set_delete_all(PluginOperator):
184 bl_idname = "pose.selection_set_delete_all"
185 bl_label = "Delete All Sets"
186 bl_description = "Deletes All Selection Sets"
187 bl_options = {'UNDO', 'REGISTER'}
189 def execute(self, context):
190 arm = context.object
191 arm.selection_sets.clear()
192 return {'FINISHED'}
195 class POSE_OT_selection_set_remove_bones(PluginOperator):
196 bl_idname = "pose.selection_set_remove_bones"
197 bl_label = "Remove Selected Bones from All Sets"
198 bl_description = "Removes the Selected Bones from All Sets"
199 bl_options = {'UNDO', 'REGISTER'}
201 def execute(self, context):
202 arm = context.object
204 # iterate only the selected bones in current pose that are not hidden
205 for bone in context.selected_pose_bones:
206 for selset in arm.selection_sets:
207 if bone.name in selset.bone_ids:
208 idx = selset.bone_ids.find(bone.name)
209 selset.bone_ids.remove(idx)
211 return {'FINISHED'}
214 class POSE_OT_selection_set_move(NeedSelSetPluginOperator):
215 bl_idname = "pose.selection_set_move"
216 bl_label = "Move Selection Set in List"
217 bl_description = "Move the active Selection Set up/down the list of sets"
218 bl_options = {'UNDO', 'REGISTER'}
220 direction: EnumProperty(
221 name="Move Direction",
222 description="Direction to move the active Selection Set: UP (default) or DOWN",
223 items=[
224 ('UP', "Up", "", -1),
225 ('DOWN', "Down", "", 1),
227 default='UP',
228 options={'HIDDEN'},
231 @classmethod
232 def poll(cls, context):
233 if not super().poll(context):
234 return False
235 arm = context.object
236 return len(arm.selection_sets) > 1
238 def execute(self, context):
239 arm = context.object
241 active_idx = arm.active_selection_set
242 new_idx = active_idx + (-1 if self.direction == 'UP' else 1)
244 if new_idx < 0 or new_idx >= len(arm.selection_sets):
245 return {'FINISHED'}
247 arm.selection_sets.move(active_idx, new_idx)
248 arm.active_selection_set = new_idx
250 return {'FINISHED'}
253 class POSE_OT_selection_set_add(PluginOperator):
254 bl_idname = "pose.selection_set_add"
255 bl_label = "Create Selection Set"
256 bl_description = "Creates a new empty Selection Set"
257 bl_options = {'UNDO', 'REGISTER'}
259 def execute(self, context):
260 arm = context.object
261 sel_sets = arm.selection_sets
262 new_sel_set = sel_sets.add()
263 new_sel_set.name = uniqify("SelectionSet", sel_sets.keys())
265 # select newly created set
266 arm.active_selection_set = len(sel_sets) - 1
268 return {'FINISHED'}
271 class POSE_OT_selection_set_remove(NeedSelSetPluginOperator):
272 bl_idname = "pose.selection_set_remove"
273 bl_label = "Delete Selection Set"
274 bl_description = "Delete a Selection Set"
275 bl_options = {'UNDO', 'REGISTER'}
277 def execute(self, context):
278 arm = context.object
280 arm.selection_sets.remove(arm.active_selection_set)
282 # change currently active selection set
283 numsets = len(arm.selection_sets)
284 if (arm.active_selection_set > (numsets - 1) and numsets > 0):
285 arm.active_selection_set = len(arm.selection_sets) - 1
287 return {'FINISHED'}
290 class POSE_OT_selection_set_assign(PluginOperator):
291 bl_idname = "pose.selection_set_assign"
292 bl_label = "Add Bones to Selection Set"
293 bl_description = "Add selected bones to Selection Set"
294 bl_options = {'UNDO', 'REGISTER'}
296 def invoke(self, context, event):
297 arm = context.object
299 if not (arm.active_selection_set < len(arm.selection_sets)):
300 bpy.ops.wm.call_menu("INVOKE_DEFAULT",
301 name="POSE_MT_selection_set_create")
302 else:
303 bpy.ops.pose.selection_set_assign('EXEC_DEFAULT')
305 return {'FINISHED'}
307 def execute(self, context):
308 arm = context.object
309 act_sel_set = arm.selection_sets[arm.active_selection_set]
311 # iterate only the selected bones in current pose that are not hidden
312 for bone in context.selected_pose_bones:
313 if bone.name not in act_sel_set.bone_ids:
314 bone_id = act_sel_set.bone_ids.add()
315 bone_id.name = bone.name
317 return {'FINISHED'}
320 class POSE_OT_selection_set_unassign(NeedSelSetPluginOperator):
321 bl_idname = "pose.selection_set_unassign"
322 bl_label = "Remove Bones from Selection Set"
323 bl_description = "Remove selected bones from Selection Set"
324 bl_options = {'UNDO', 'REGISTER'}
326 def execute(self, context):
327 arm = context.object
328 act_sel_set = arm.selection_sets[arm.active_selection_set]
330 # iterate only the selected bones in current pose that are not hidden
331 for bone in context.selected_pose_bones:
332 if bone.name in act_sel_set.bone_ids:
333 idx = act_sel_set.bone_ids.find(bone.name)
334 act_sel_set.bone_ids.remove(idx)
336 return {'FINISHED'}
339 class POSE_OT_selection_set_select(NeedSelSetPluginOperator):
340 bl_idname = "pose.selection_set_select"
341 bl_label = "Select Selection Set"
342 bl_description = "Add Selection Set bones to current selection"
343 bl_options = {'UNDO', 'REGISTER'}
345 selection_set_index: IntProperty(
346 name='Selection Set Index',
347 default=-1,
348 description='Which Selection Set to select; -1 uses the active Selection Set',
349 options={'HIDDEN'},
352 def execute(self, context):
353 arm = context.object
355 if self.selection_set_index == -1:
356 idx = arm.active_selection_set
357 else:
358 idx = self.selection_set_index
359 sel_set = arm.selection_sets[idx]
361 for bone in context.visible_pose_bones:
362 if bone.name in sel_set.bone_ids:
363 bone.bone.select = True
365 return {'FINISHED'}
368 class POSE_OT_selection_set_deselect(NeedSelSetPluginOperator):
369 bl_idname = "pose.selection_set_deselect"
370 bl_label = "Deselect Selection Set"
371 bl_description = "Remove Selection Set bones from current selection"
372 bl_options = {'UNDO', 'REGISTER'}
374 def execute(self, context):
375 arm = context.object
376 act_sel_set = arm.selection_sets[arm.active_selection_set]
378 for bone in context.selected_pose_bones:
379 if bone.name in act_sel_set.bone_ids:
380 bone.bone.select = False
382 return {'FINISHED'}
385 class POSE_OT_selection_set_add_and_assign(PluginOperator):
386 bl_idname = "pose.selection_set_add_and_assign"
387 bl_label = "Create and Add Bones to Selection Set"
388 bl_description = "Creates a new Selection Set with the currently selected bones"
389 bl_options = {'UNDO', 'REGISTER'}
391 def execute(self, context):
392 bpy.ops.pose.selection_set_add('EXEC_DEFAULT')
393 bpy.ops.pose.selection_set_assign('EXEC_DEFAULT')
394 return {'FINISHED'}
397 class POSE_OT_selection_set_copy(NeedSelSetPluginOperator):
398 bl_idname = "pose.selection_set_copy"
399 bl_label = "Copy Selection Set(s)"
400 bl_description = "Copies the selected Selection Set(s) to the clipboard"
401 bl_options = {'UNDO', 'REGISTER'}
403 def execute(self, context):
404 context.window_manager.clipboard = to_json(context)
405 self.report({'INFO'}, 'Copied Selection Set(s) to Clipboard')
406 return {'FINISHED'}
409 class POSE_OT_selection_set_paste(PluginOperator):
410 bl_idname = "pose.selection_set_paste"
411 bl_label = "Paste Selection Set(s)"
412 bl_description = "Adds new Selection Set(s) from the Clipboard"
413 bl_options = {'UNDO', 'REGISTER'}
415 def execute(self, context):
416 import json
418 try:
419 from_json(context, context.window_manager.clipboard)
420 except (json.JSONDecodeError, KeyError):
421 self.report({'ERROR'}, 'The clipboard does not contain a Selection Set')
422 else:
423 # Select the pasted Selection Set.
424 context.object.active_selection_set = len(context.object.selection_sets) - 1
426 return {'FINISHED'}
429 # Helper Functions ############################################################
431 def menu_func_select_selection_set(self, context):
432 self.layout.menu('POSE_MT_selection_sets_select', text="Bone Selection Set")
435 def to_json(context) -> str:
436 """Convert the selected Selection Sets of the current rig to JSON.
438 Selected Sets are the active_selection_set determined by the UIList
439 plus any with the is_selected checkbox on."""
440 import json
442 arm = context.object
443 active_idx = arm.active_selection_set
445 json_obj = {}
446 for idx, sel_set in enumerate(context.object.selection_sets):
447 if idx == active_idx or sel_set.is_selected:
448 bones = [bone_id.name for bone_id in sel_set.bone_ids]
449 json_obj[sel_set.name] = bones
451 return json.dumps(json_obj)
454 def from_json(context, as_json: str):
455 """Add the selection sets (one or more) from JSON to the current rig."""
456 import json
458 json_obj = json.loads(as_json)
459 arm_sel_sets = context.object.selection_sets
461 for name, bones in json_obj.items():
462 new_sel_set = arm_sel_sets.add()
463 new_sel_set.name = uniqify(name, arm_sel_sets.keys())
464 for bone_name in bones:
465 bone_id = new_sel_set.bone_ids.add()
466 bone_id.name = bone_name
469 def uniqify(name: str, other_names: list) -> str:
470 """Return a unique name with .xxx suffix if necessary.
472 Example usage:
474 >>> uniqify('hey', ['there'])
475 'hey'
476 >>> uniqify('hey', ['hey.001', 'hey.005'])
477 'hey'
478 >>> uniqify('hey', ['hey', 'hey.001', 'hey.005'])
479 'hey.002'
480 >>> uniqify('hey', ['hey', 'hey.005', 'hey.001'])
481 'hey.002'
482 >>> uniqify('hey', ['hey', 'hey.005', 'hey.001', 'hey.left'])
483 'hey.002'
484 >>> uniqify('hey', ['hey', 'hey.001', 'hey.002'])
485 'hey.003'
487 It also works with a dict_keys object:
488 >>> uniqify('hey', {'hey': 1, 'hey.005': 1, 'hey.001': 1}.keys())
489 'hey.002'
492 if name not in other_names:
493 return name
495 # Construct the list of numbers already in use.
496 offset = len(name) + 1
497 others = (n[offset:] for n in other_names
498 if n.startswith(name + '.'))
499 numbers = sorted(int(suffix) for suffix in others
500 if suffix.isdigit())
502 # Find the first unused number.
503 min_index = 1
504 for num in numbers:
505 if min_index < num:
506 break
507 min_index = num + 1
508 return "{}.{:03d}".format(name, min_index)
511 # Registry ####################################################################
513 classes = (
514 POSE_MT_selection_set_create,
515 POSE_MT_selection_sets_context_menu,
516 POSE_MT_selection_sets_select,
517 POSE_PT_selection_sets,
518 POSE_UL_selection_set,
519 SelectionEntry,
520 SelectionSet,
521 POSE_OT_selection_set_delete_all,
522 POSE_OT_selection_set_remove_bones,
523 POSE_OT_selection_set_move,
524 POSE_OT_selection_set_add,
525 POSE_OT_selection_set_remove,
526 POSE_OT_selection_set_assign,
527 POSE_OT_selection_set_unassign,
528 POSE_OT_selection_set_select,
529 POSE_OT_selection_set_deselect,
530 POSE_OT_selection_set_add_and_assign,
531 POSE_OT_selection_set_copy,
532 POSE_OT_selection_set_paste,
536 # Store keymaps here to access after registration.
537 addon_keymaps = []
540 def register():
541 for cls in classes:
542 bpy.utils.register_class(cls)
544 # Add properties.
545 bpy.types.Object.selection_sets = CollectionProperty(
546 type=SelectionSet,
547 name="Selection Sets",
548 description="List of groups of bones for easy selection"
550 bpy.types.Object.active_selection_set = IntProperty(
551 name="Active Selection Set",
552 description="Index of the currently active selection set",
553 default=0
556 # Add shortcuts to the keymap.
557 wm = bpy.context.window_manager
558 km = wm.keyconfigs.addon.keymaps.new(name='Pose')
559 kmi = km.keymap_items.new('wm.call_menu', 'W', 'PRESS', alt=True, shift=True)
560 kmi.properties.name = 'POSE_MT_selection_sets_select'
561 addon_keymaps.append((km, kmi))
563 # Add entries to menus.
564 bpy.types.VIEW3D_MT_select_pose.append(menu_func_select_selection_set)
567 def unregister():
568 for cls in classes:
569 bpy.utils.unregister_class(cls)
571 # Clear properties.
572 del bpy.types.Object.selection_sets
573 del bpy.types.Object.active_selection_set
575 # Clear shortcuts from the keymap.
576 for km, kmi in addon_keymaps:
577 km.keymap_items.remove(kmi)
578 addon_keymaps.clear()
580 # Clear entries from menus.
581 bpy.types.VIEW3D_MT_select_pose.remove(menu_func_select_selection_set)
585 if __name__ == "__main__":
586 import doctest
588 doctest.testmod()
589 register()