import pymel.core as pm
import maya.cmds as cmds
base_jnt = [
'root_jnt', 'body_jnt',
'neck_jnt', 'neck_piece_main_jnt', 'neck_piece_L_00_jnt', 'neck_piece_R_00_jnt', 'neck_piece_C_00_jnt',
'head_jnt', 'jaw_jnt',
'shoulder_deform_L_jnt','pauldron_main_L_jnt', 'pauldron_A_L_00_jnt', 'pauldron_B_L_00_jnt',
'elbow_deform_L_jnt', 'hand_deform_L_jnt',
'shoulder_deform_R_jnt','pauldron_main_R_jnt', 'pauldron_A_R_00_jnt', 'pauldron_B_R_00_jnt',
'elbow_deform_R_jnt', 'hand_deform_R_jnt',
'shoulder_FK_L_jnt', 'elbow_FK_L_jnt', 'hand_FK_L_jnt',
'shoulder_FK_R_jnt', 'elbow_FK_R_jnt', 'hand_FK_R_jnt',
]
def connect_joints_con():
for base in base_jnt:
base_obj = pm.PyNode(base)
if not base_obj: print (f"{base_obj.getName()} does not exist")
con_name = base_obj.getName().replace("_jnt", "_con")
con_obj = pm.PyNode(con_name)
if not con_obj: print (f"{con_obj.getName()} does not exist")
orient_constr = pm.orientConstraint(con_obj, base_obj, mo=True)
point_constr = pm.pointConstraint(con_obj, base_obj, mo=True)
scale_constr = pm.scaleConstraint(con_obj, base_obj, mo=True)
def setup_ik_fk_switch(main_chain, fk_chain, ik_chain, switch_ctrl, arm_ctrl, pole_ctrl):
ik_handle, effector = pm.ikHandle(startJoint=ik_chain[0], endEffector=ik_chain[-1], solver="ikRPsolver", name=ik_chain[0] + "_ikHandle")
pm.rename(effector, ik_chain[-1] + "_eff")
pm.parent(ik_handle, arm_ctrl)
pm.poleVectorConstraint(pole_ctrl, ik_handle)
pm.parentConstraint(main_chain[-1], switch_ctrl + "_grp")
pm.orientConstraint(arm_ctrl, ik_chain[-1], mo=True)
switch_ctrl = pm.PyNode(switch_ctrl)
for main_jnt, fk_jnt, ik_jnt in zip(main_chain, fk_chain, ik_chain):
main_jnt = pm.PyNode(main_jnt)
fk_jnt = pm.PyNode(fk_jnt)
ik_jnt = pm.PyNode(ik_jnt)
switch_ctrl = pm.PyNode(switch_ctrl)
arm_ctrl = pm.PyNode(arm_ctrl)
pole_ctrl = pm.PyNode(pole_ctrl)
if not pm.isConnected(switch_ctrl.FKIKSwitch, arm_ctrl.visibility):
pm.connectAttr(switch_ctrl.FKIKSwitch, arm_ctrl.visibility)
if not pm.isConnected(switch_ctrl.FKIKSwitch, pole_ctrl.visibility):
pm.connectAttr(switch_ctrl.FKIKSwitch, pole_ctrl.visibility)
orient_constr = pm.orientConstraint(fk_jnt, ik_jnt, main_jnt)
pm.connectAttr(switch_ctrl.FKIKSwitch, orient_constr.attr(ik_jnt.nodeName() + "W1"))
rev_node = pm.createNode("reverse", name=main_jnt.nodeName() + "_ikFkRev")
pm.connectAttr(switch_ctrl.FKIKSwitch, rev_node + ".inputX")
pm.connectAttr(rev_node + ".outputX", orient_constr.attr(fk_jnt.nodeName() + "W0"))
fk_con_name = fk_jnt.getName().replace("_jnt", "_con")
fk_con_obj = pm.PyNode(fk_con_name)
pm.connectAttr(rev_node + ".outputX", fk_con_obj.visibility)
orient_constr = pm.pointConstraint(fk_jnt, ik_jnt, main_jnt)
pm.connectAttr(switch_ctrl.FKIKSwitch, orient_constr.attr(ik_jnt.nodeName() + "W1"))
rev_node = pm.createNode("reverse", name=main_jnt.nodeName() + "_ikFkRev")
pm.connectAttr(switch_ctrl.FKIKSwitch, rev_node + ".inputX")
pm.connectAttr(rev_node + ".outputX", orient_constr.attr(fk_jnt.nodeName() + "W0"))
orient_constr = pm.scaleConstraint(fk_jnt, ik_jnt, main_jnt)
pm.connectAttr(switch_ctrl.FKIKSwitch, orient_constr.attr(ik_jnt.nodeName() + "W1"))
rev_node = pm.createNode("reverse", name=main_jnt.nodeName() + "_ikFkRev")
pm.connectAttr(switch_ctrl.FKIKSwitch, rev_node + ".inputX")
pm.connectAttr(rev_node + ".outputX", orient_constr.attr(fk_jnt.nodeName() + "W0"))
connect_joints_con()
main_joints = ['shoulder_deform_L_con_grp', 'elbow_deform_L_con_grp', 'hand_deform_L_con_grp']
fk_joints = ['shoulder_FK_L_jnt', 'elbow_FK_L_jnt', 'hand_FK_L_jnt']
ik_joints = ['shoulder_IK_L_jnt', 'elbow_IK_L_jnt', 'hand_IK_L_jnt']
switch_ctrl = 'arm_IKFK_L_switch_con'
arm_ctrl = 'arm_IK_L_con'
pole_ctrl = 'arm_pole_L_con'
setup_ik_fk_switch(main_joints, fk_joints, ik_joints, switch_ctrl, arm_ctrl, pole_ctrl)
main_joints = ['shoulder_deform_R_con_grp', 'elbow_deform_R_con_grp', 'hand_deform_R_con_grp']
fk_joints = ['shoulder_FK_R_jnt', 'elbow_FK_R_jnt', 'hand_FK_R_jnt']
ik_joints = ['shoulder_IK_R_jnt', 'elbow_IK_R_jnt', 'hand_IK_R_jnt']
switch_ctrl = 'arm_IKFK_R_switch_con'
arm_ctrl = 'arm_IK_R_con'
pole_ctrl = 'arm_pole_R_con'
setup_ik_fk_switch(main_joints, fk_joints, ik_joints, switch_ctrl, arm_ctrl, pole_ctrl)