Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Re-assignable hotkey for toggling between Rest and Pose Mode for selected Armature.
bl_info = {
"name": "Toggle Armature Rest/Pose",
"description": "Re-assignable hotkey for toggling between Rest and Pose Mode for selected Armature.",
"author": "Ferdinand Joseph Fernandez",
"version": (1, 0),
"location": "Press Shift-Q while in 3d View, and in Pose Mode of an Armature",
"support": "TESTING",
"category": "Animation",
}
import bpy
def is_in_visible_layer(ob):
ob_layers = ob.layers
scene_layers = bpy.context.scene.layers
for layer_index in range(len(ob_layers)):
if ob_layers[layer_index] == True and scene_layers[layer_index] == True:
return True
return False
class RestPoseToggle(bpy.types.Operator):
"""Toggle Rest/Pose"""
bl_idname = "animation.rest_pose_toggle"
bl_label = "Toggle Rest/Pose"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
ob = context.object
if ob.type != 'ARMATURE':
# Selected wasn't an armature.
# Search whole scene for the first
# armature that isn't hidden via the outliner,
# and is in a layer that is currently visible.
for scene_ob in bpy.context.scene.objects:
if scene_ob.type != 'ARMATURE' or scene_ob.hide == True or is_in_visible_layer(scene_ob) == False:
continue
ob = scene_ob
break
if ob.type == 'ARMATURE':
armature = ob.data
if armature.pose_position == 'POSE':
armature.pose_position = 'REST'
else:
armature.pose_position = 'POSE'
return {'FINISHED'}
# keymaps are stored here so they can be accessed after registration
rest_pose_toggle_addon_keymaps = []
def register():
bpy.utils.register_class(RestPoseToggle)
# hotkey registration
wm = bpy.context.window_manager
kc = wm.keyconfigs.addon
if kc:
keymap_pose_rest_pose_toggle = kc.keymaps.new(name='Pose', space_type='EMPTY')
keymap_item_pose_rest_pose_toggle = keymap_pose_rest_pose_toggle.keymap_items.new("animation.rest_pose_toggle", 'Q', 'PRESS', shift=True)
keymap_object_rest_pose_toggle = kc.keymaps.new(name='Object Mode', space_type='EMPTY')
keymap_item_object_rest_pose_toggle = keymap_object_rest_pose_toggle.keymap_items.new("animation.rest_pose_toggle", 'Q', 'PRESS', shift=True)
# add to list so we can unregister afterwards
rest_pose_toggle_addon_keymaps.append((keymap_pose_rest_pose_toggle, keymap_item_pose_rest_pose_toggle))
rest_pose_toggle_addon_keymaps.append((keymap_object_rest_pose_toggle, keymap_item_object_rest_pose_toggle))
def unregister():
# unregister hotkey
wm = bpy.context.window_manager
kc = wm.keyconfigs.addon
if kc:
for km, kmi in rest_pose_toggle_addon_keymaps:
#print("Removing hotkey %s" % (kmi.idname))
km.keymap_items.remove(kmi)
# note: don't remove km, as it may contain hotkeys of other add-ons!
#kc.keymaps.remove(km)
# clear the list
#addon_keymaps.clear()
del rest_pose_toggle_addon_keymaps[:]
bpy.utils.unregister_class(RestPoseToggle)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment