Skip to content

Instantly share code, notes, and snippets.

@sjkillen
Created September 17, 2023 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjkillen/71c5224aae709bbc42745e56b2bcc716 to your computer and use it in GitHub Desktop.
Save sjkillen/71c5224aae709bbc42745e56b2bcc716 to your computer and use it in GitHub Desktop.
Toggle the IK on a armature in Blender Python
import bpy
from bpy.props import IntProperty, FloatProperty
from bpy.types import Armature, PoseBone, KinematicConstraint, Object
def ik_constraints(armature: Object):
assert type(armature.data) is Armature
return (constraint for pbone in armature.pose.bones
for constraint in pbone.constraints
if type(constraint is KinematicConstraint))
class ToggleIK(bpy.types.Operator):
"""Disable/Enable the IK of the selected skeleton"""
bl_idname = "object.toggle_ik_skeleton"
bl_label = "Toggle IK on Skeleton"
@classmethod
def poll(Cls, context):
return type(context.object.data) is Armature and context.mode == "POSE"
def invoke(self, context, event):
state = any(constraint.enabled for constraint in ik_constraints(context.object))
for constraint in ik_constraints(context.object):
constraint.enabled = not state
if state:
self.report({'INFO'}, "Disabled")
else:
self.report({'INFO'}, "Enabled")
return {'FINISHED'}
def menu_func(self, context):
self.layout.operator(ToggleIK.bl_idname, text=ToggleIK.bl_label)
def register():
bpy.utils.register_class(ToggleIK)
bpy.types.VIEW3D_MT_object.append(menu_func)
def unregister():
bpy.utils.unregister_class(ToggleIK)
bpy.types.VIEW3D_MT_object.remove(menu_func)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment