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 #####
20 "name": "Bone Selection Sets",
21 "author": "Inês Almeida, Sybren A. Stüvel, Antony Riakiotakis, Dan Eicher",
23 "blender": (2, 80, 0),
24 "location": "Properties > Object Data (Armature) > Selection Sets",
25 "description": "List of Bone sets for easy selection while animating",
27 "wiki_url": "https://wiki.blender.org/index.php/Extensions:2.6/Py/"
28 "Scripts/Animation/SelectionSets",
29 "category": "Animation",
33 from bpy
.types
import (
40 from bpy
.props
import (
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_specials(Menu
):
66 bl_label
= "Selection Sets Specials"
68 def draw(self
, context
):
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'
84 def poll(cls
, context
):
85 return (context
.object and
86 context
.object.type == 'ARMATURE' and
89 def draw(self
, context
):
95 row
.enabled
= (context
.mode
== 'POSE')
98 rows
= 4 if len(arm
.selection_sets
) > 0 else 1
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
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_specials", icon
='DOWNARROW_HLT', text
="")
112 # move up/down arrows
113 if len(arm
.selection_sets
) > 0:
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'
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
):
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
):
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'
151 def poll(cls
, context
):
152 return POSE_OT_selection_set_select
.poll(context
)
154 def draw(self
, context
):
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."""
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."""
176 def poll(cls
, context
):
177 if not super().poll(context
):
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
):
191 arm
.selection_sets
.clear()
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
):
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
)
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",
224 ('UP', "Up", "", -1),
225 ('DOWN', "Down", "", 1),
232 def poll(cls
, context
):
233 if not super().poll(context
):
236 return len(arm
.selection_sets
) > 1
238 def execute(self
, context
):
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
):
247 arm
.selection_sets
.move(active_idx
, new_idx
)
248 arm
.active_selection_set
= new_idx
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
):
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
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
):
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
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
):
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")
303 bpy
.ops
.pose
.selection_set_assign('EXEC_DEFAULT')
307 def execute(self
, context
):
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
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
):
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
)
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',
348 description
='Which Selection Set to select; -1 uses the active Selection Set',
352 def execute(self
, context
):
355 if self
.selection_set_index
== -1:
356 idx
= arm
.active_selection_set
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
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
):
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
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')
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')
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
):
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')
423 # Select the pasted Selection Set.
424 context
.object.active_selection_set
= len(context
.object.selection_sets
) - 1
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."""
443 active_idx
= arm
.active_selection_set
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."""
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.
474 >>> uniqify('hey', ['there'])
476 >>> uniqify('hey', ['hey.001', 'hey.005'])
478 >>> uniqify('hey', ['hey', 'hey.001', 'hey.005'])
480 >>> uniqify('hey', ['hey', 'hey.005', 'hey.001'])
482 >>> uniqify('hey', ['hey', 'hey.005', 'hey.001', 'hey.left'])
484 >>> uniqify('hey', ['hey', 'hey.001', 'hey.002'])
487 It also works with a dict_keys object:
488 >>> uniqify('hey', {'hey': 1, 'hey.005': 1, 'hey.001': 1}.keys())
492 if name
not in other_names
:
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
502 # Find the first unused number.
508 return "{}.{:03d}".format(name
, min_index
)
511 # Registry ####################################################################
514 POSE_MT_selection_set_create
,
515 POSE_MT_selection_sets_specials
,
516 POSE_MT_selection_sets_select
,
517 POSE_PT_selection_sets
,
518 POSE_UL_selection_set
,
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.
542 bpy
.utils
.register_class(cls
)
545 bpy
.types
.Object
.selection_sets
= CollectionProperty(
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",
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
)
569 bpy
.utils
.unregister_class(cls
)
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__":