Last active
December 10, 2019 16:59
-
-
Save scroll/fa3c724109583c4cd211932ce952ae6d to your computer and use it in GitHub Desktop.
IKFK Snapping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import maya.cmds as mc | |
import maya.OpenMaya as om | |
ik_control = 'IK_ctl' | |
pv_control = 'PV_ctl' | |
ik_joints = ['001_ik', '002_ik', '003_ik'] | |
fk_controls = ['001_ctl', '002_ctl', '003_ctl'] | |
ik_attr = 'Settings_locShape.IkFk' | |
def get_rotation(transform): | |
return mc.getAttr('{}.rotate'.format(transform))[0] | |
def set_rotation(transform, rotation): | |
mc.setAttr('{}.rotate'.format(transform), *rotation) | |
def get_token(): | |
name = mc.ls(sl=True)[0] | |
return name[0:6] | |
def ik2fk(): | |
token = get_token() | |
for i, j in enumerate(fk_controls): | |
rot = get_rotation(token + ik_joints[i]) | |
set_rotation(token + j, rot) | |
mc.setAttr(token + ik_attr, 1) | |
def fk2ik(): | |
token = get_token() | |
# Get the fk controls as points in space & convert to Vec | |
p1 = mc.xform(token + fk_controls[0], ws=True, q=True, t=True) | |
v1 = om.MVector(*p1) | |
p2 = mc.xform(token + fk_controls[1], ws=True, q=True, t=True) | |
v2 = om.MVector(*p2) | |
p3 = mc.xform(token + fk_controls[2], ws=True, q=True, t=True) | |
v3 = om.MVector(*p3) | |
# Position the IK control on the FK wrist and rotate it | |
ik_rot = mc.xform(token + fk_controls[2], ws=True, q=True, ro=True) | |
mc.xform(token + ik_control, ws=True, t=p3, ro=ik_rot) | |
# find an average between p1 and p3 | |
midpoint = (v3 + v1) / 2 | |
# get a direction vector | |
pv_dir = v2 - midpoint | |
# calculate the exact position | |
pv_pos = (pv_dir * 3) + midpoint | |
# position the Pole Vector on that extended point | |
mc.xform(token + pv_control, ws=True, t=pv_pos) | |
mc.setAttr(token + ik_attr, 0) | |
ik2fk() | |
fk2ik() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment