#-------------------------------------------------------------------------------
# Name:        YJ_Leg_Rig
# Purpose:     Rig Legs
#
# Author:      yangjie
#
# Created:     05/07/2014
# Copyright:   (c) yangjie 2014
# Licence:     <your licence>
#-------------------------------------------------------------------------------
from YJ_Rig_Pub import*
from YJ_FK_Rig import*
from YJ_IK_Rig import*
from YJ_Create_Body_Rig_UI_p import*



L_LegJnt_List = YJ_Load_List('L_LegJnt_List.pkl')
R_LegJnt_List = YJ_GetMir_List(L_LegJnt_List)
L_FootJnt_List = YJ_Load_List('L_FootJnt_List.pkl')
R_FootJnt_List = YJ_GetMir_List(L_FootJnt_List)
L_Help_FootJnt_List = YJ_Load_List('L_Help_FootJnt_List.pkl')
R_Help_FootJnt_List = YJ_GetMir_List(L_Help_FootJnt_List)
L_LegFootJnt_List = L_LegJnt_List + L_FootJnt_List[0:2]
R_LegFootJnt_List = YJ_GetMir_List(L_LegFootJnt_List)

L_toeJnt_List = YJ_Load_List('L_toeJnt_List.pkl')


L_Arm_Jnt_List = YJ_Load_List('L_Arm_Jnt_List.pkl')
Hand_Help_Jnt_L = YJ_Load_List('Hand_Help_Jnt_L.pkl')
L_FingerAllList_Lis = YJ_Load_List('L_FingerAllList_Lis.pkl')

IKFK_Type = radioCollection('Leg_radioCollection',q=1,sl=1)

def YJ_Body_Rig():
    YJ_Creat_Ctrl('Cur5','all_anim')

# NamePre = 'L_'
def YJ_Leg_Rig():
    for i in L_Help_FootJnt_List:
        parent (i,w = 1)
    for i in R_Help_FootJnt_List:
        parent (i,w=1)
    L_LegAll_Grp = CreatGRpToTarg("L_LegAll_Grp",L_LegJnt_List[0])
    R_LegAll_Grp = CreatGRpToTarg("R_LegAll_Grp",R_LegJnt_List[0])
    YJ_Try_Parent(L_FootJnt_List[0],L_LegJnt_List[-1])
    YJ_Try_Parent(R_FootJnt_List[0],R_LegJnt_List[-1])
    YJ_Try_Parent(L_LegJnt_List[0],L_LegAll_Grp)
    YJ_Try_Parent(R_LegJnt_List[0],R_LegAll_Grp)
    L_LegFootFk_List,L_LegFootFkConstrnList,L_FK_Grp,R_LegFootFk_List,R_LegFootFkConstrnList,R_FK_Grp= YJ_Leg_FK_Rig(IKFK_Type,L_LegFootJnt_List,R_LegFootJnt_List,L_LegAll_Grp,R_LegAll_Grp)
    L_LegFootIk_List,L_LegIkCtrl_List,L_IK_All_Grp,R_LegFootIk_List,R_IK_All_Grp,R_LegIkCtrl_List = YJ_Leg_IK_Rig(L_LegFootJnt_List,L_Help_FootJnt_List,L_LegAll_Grp,R_LegFootJnt_List,R_Help_FootJnt_List,R_LegAll_Grp)

    L_IKFKCtrl,R_IKFKCtrl = YJ_Jud_Cre_LegIKFKCtrl(L_LegAll_Grp,R_LegAll_Grp,L_LegFootFkConstrnList,R_LegFootFkConstrnList,L_FK_Grp,L_IK_All_Grp,R_FK_Grp,R_IK_All_Grp)


def YJ_Jud_Cre_LegIKFKCtrl(L_LegAll_Grp,R_LegAll_Grp,L_LegFootFkConstrnList,R_LegFootFkConstrnList,L_FK_Grp,L_IK_Grp,R_FK_Grp,R_IK_Grp):
    if IKFK_Type == 'Leg_IKFK':
        L_IKFKCtrl = YJ_Cre_LegIKFKCtrl(L_LegJnt_List[-1],'L_Leg_IKFK_Anim',L_LegAll_Grp,L_LegFootFkConstrnList,L_FK_Grp,L_IK_Grp)
        R_IKFKCtrl = YJ_Cre_LegIKFKCtrl(R_LegJnt_List[-1],'R_Leg_IKFK_Anim',R_LegAll_Grp,R_LegFootFkConstrnList,R_FK_Grp,R_IK_Grp)
    else:
        L_IKFKCtrl,R_IKFKCtrl = '',''
    return  L_IKFKCtrl,R_IKFKCtrl
# TarJnt,CtrlName,LegAll_Grp,ParConLis = L_LegJnt_List[-1],'L_Leg_IKFK_Anim',L_LegAll_Grp,L_LegFootFkConstrnList
def YJ_Cre_LegIKFKCtrl(TarJnt,CtrlName,LegAll_Grp,ParConLis,FK_Grp,IK_Grp):
    YJ_Creat_Ctrl('Cur12',CtrlName)
    YJ_Scale_Shape([CtrlName],0.05)
    YJ_MoveOrRotateToTargetWd(TarJnt,CtrlName,'t','t')
    YJ_Try_Parent(CtrlName,LegAll_Grp)
    AttrLis = YJ_AddIKFKCtrlAttr(CtrlName)
    IKFKAttr = CtrlName + '.' +AttrLis[1][1]
    YJ_ConnectIKFKAttr('FK','IK',IKFKAttr,ParConLis,FK_Grp,IK_Grp)
    return CtrlName

def YJ_AddIKFKCtrlAttr(Ctrl):
    AttrList=[['Kind','Basic'],['Bl','IKFK']]
    YJ_AddAttr(Ctrl,AttrList)
    return AttrList

##

def YJ_Leg_FK_Rig(IKFK_Type,L_LegFootJnt_List,R_LegFootJnt_List,L_LegAll_Grp,R_LegAll_Grp):
    if IKFK_Type != 'Leg_IK':
        L_LegFootFk_List,L_LegFootFkConstrnList,L_FK_Grp = YJ_Jud_Leg_Fk_Rig(L_LegFootJnt_List,'L',L_LegAll_Grp)
        R_LegFootFk_List,R_LegFootFkConstrnList,R_FK_Grp = YJ_Jud_Leg_Fk_Rig(R_LegFootJnt_List,'R',R_LegAll_Grp)
    else:
        L_LegFootFk_List,L_LegFootFkConstrnList,L_FK_Grp,R_LegFootFk_List,R_LegFootFkConstrnList,R_FK_Grp = [],[],[],[],[],[]

    return L_LegFootFk_List,L_LegFootFkConstrnList,L_FK_Grp,R_LegFootFk_List,R_LegFootFkConstrnList,R_FK_Grp



#YJ_Scale_Shape(L_LegJnt_List,0.2) LegList =L_LegFootJnt_List,NamePre ='L',LegAll_Grp =L_LegAll_Grp
def YJ_Jud_Leg_Fk_Rig(LegList,NamePre,LegAll_Grp):
    LegFk_List = YJ_DuplicateJnt(LegList,'jnt','FK')
    ZeroCreate = YJRigZeroGrp([LegFk_List[0]])
    parent(ZeroCreate,w=True )
    FK_Grp = rename(ZeroCreate[0], NamePre+'_Fk_Grp' )
    YJ_CreatFkRig(LegFk_List[0],LegFk_List[-1],1,0)
    ConstrnList = YJ_SetParentConstraint(LegFk_List,LegList)
    YJ_Scale_Shape(LegFk_List[:-1],0.2)
    YJ_Try_Parent(FK_Grp,LegAll_Grp)
    return LegFk_List[:-1],ConstrnList,FK_Grp

#LegList = L_LegFootJnt_List,NamePre = 'L'

def YJ_Leg_IK_Rig(L_LegFootJnt_List,L_Help_FootJnt_List,L_LegAll_Grp,R_LegFootJnt_List,R_Help_FootJnt_List,R_LegAll_Grp):
    if IKFK_Type != 'Leg_FK':
        L_LegFootIk_List,L_LegIkCtrl_List,L_IK_All_Grp = YJ_Jud_Leg_Ik_Rig(L_LegFootJnt_List,L_Help_FootJnt_List,'L',L_LegAll_Grp)
        R_LegFootIk_List,R_LegIkCtrl_List,R_IK_All_Grp= YJ_Jud_Leg_Ik_Rig(R_LegFootJnt_List,R_Help_FootJnt_List,'R',R_LegAll_Grp)
    else:
        L_LegFootIk_List,L_LegIkCtrl_List,L_IK_All_Grp,R_LegFootIk_List,R_IK_All_Grp,R_LegIkCtrl_List = [],[],[],[],[],[]

    return L_LegFootIk_List,L_LegIkCtrl_List,L_IK_All_Grp,R_LegFootIk_List,R_IK_All_Grp,R_LegIkCtrl_List
#LegList,Help_FootJnt_List,NamePre,L_LegAll_Grp = L_LegFootJnt_List,L_Help_FootJnt_List,'L',L_LegAll_Grp
def YJ_Jud_Leg_Ik_Rig(LegList,Help_FootJnt_List,NamePre,L_LegAll_Grp):
    LegIk_List = YJ_DuplicateJnt(LegList,'jnt','IK')
    ZeroCreate = YJRigZeroGrp([LegIk_List[0]])
    parent(ZeroCreate,w=True )
    IK_All_Grp = rename(ZeroCreate[0], NamePre+'_Ik_Grp' )
    IKGrp,LegIkCtrl_List,LegFootIK_Handle= YJ_CreateIkRig(LegIk_List[0],LegIk_List[2],1,'rz','-')
    YJ_Try_Parent(IKGrp,IK_All_Grp)
    ConstrnList = YJ_SetParentConstraint(LegIk_List,LegList)
    YJ_Scale_Shape(LegIkCtrl_List,0.2)
    YJ_Jud_Foot_Ik_Rig(LegIk_List,LegIkCtrl_List,LegFootIK_Handle,Help_FootJnt_List,NamePre)
    YJ_Try_Parent(IK_All_Grp,L_LegAll_Grp)
    return LegIk_List,LegIkCtrl_List,IK_All_Grp
#LegIkCtrl_List,LegFootIK_Handle

# LegFootJnt_List ,Help_FootJnt_List,NamePre = L_LegFootJnt_List,  L_Help_FootJnt_List ,'L'
def YJ_Jud_Foot_Ik_Rig(LegIk_List,LegIkCtrl_List,LegFootIK_Handle,Help_FootJnt_List,NamePre):
    FootJnt_List =LegIk_List[-3:]
    HelpJnt_List = YJ_Get_PivJnt_Lis(FootJnt_List,Help_FootJnt_List,NamePre)
    HelpPiv_List = YJ_Get_PivGrp_Lis(HelpJnt_List)
    delete( HelpJnt_List)
    FootSCIkHandle=YJ_Creat_IK(FootJnt_List[0],FootJnt_List[1],"ikSCsolver")
    ToeSCIkHandle=YJ_Creat_IK(FootJnt_List[1],FootJnt_List[2],'ikSCsolver')
    YJ_Clean_Hierarchy(HelpPiv_List,FootSCIkHandle,ToeSCIkHandle,LegIkCtrl_List,LegFootIK_Handle)
    YJ_AddFootAnimAttr(LegIkCtrl_List[0])
    footExpr = YJ_AddFootExpr(LegIkCtrl_List,HelpPiv_List)

def YJ_AddFootExpr(LegIkCtrl_List,HelpPiv_List):
    string="$roll ="+ LegIkCtrl_List[0]+".roll;\n$toeLift = "+LegIkCtrl_List[0]+".toelift;\n$toeStraight ="+ LegIkCtrl_List[0]+'.toestraight;\n$lean = '+LegIkCtrl_List[0]+'.lean;\n$side ='\
               +LegIkCtrl_List[0]+'.side;\n$spin =' +LegIkCtrl_List[0]+'.toespin;\n$wiggle ='+ LegIkCtrl_List[0]+'.toewiggle;\n'+HelpPiv_List[0]+'.rotateX = min($roll,0);\n'\
               +HelpPiv_List[4]+'.rotateX = (linstep(0,$toeLift,$roll)) *(1-(linstep($toeLift,$toeStraight,$roll))) * $roll;\n'\
               +HelpPiv_List[3]+'.rotateX = linstep($toeLift, $toeStraight, $roll) * $roll;\n'+HelpPiv_List[4]+'.rotateY = $lean;\n'\
               +HelpPiv_List[2]+'.rotateZ = min($side,0);\n'+HelpPiv_List[1]+'.rotateZ= max(0,$side);\n'\
               +HelpPiv_List[3]+'.rotateY = $spin;\n'+HelpPiv_List[5]+'.rotateX = $wiggle;'
    exprName=LegIkCtrl_List[0]+'_expr'
    expression( s=string,n=exprName)
    return exprName

def YJ_AddFootAnimAttr(Ctrl):
    AttrList=[['Kind','Basic'],['Fl','roll'],['Fl','toelift'],['Fl','toestraight'],['Fl','lean'],['Fl','side'],['Fl','toespin'],['Fl','toewiggle']]
    YJ_AddAttr(Ctrl,AttrList)

def YJ_Clean_Hierarchy(HelpPiv_List,FootSCIkHandle,ToeSCIkHandle,LegIkCtrl_List,LegFootIK_Handle):
    YJ_Try_Parent(HelpPiv_List[4],HelpPiv_List[1])
    YJ_Try_Parent(HelpPiv_List[1],HelpPiv_List[2])
    YJ_Try_Parent(HelpPiv_List[2],HelpPiv_List[3])
    YJ_Try_Parent(HelpPiv_List[3],HelpPiv_List[0])
    YJ_Try_Parent(HelpPiv_List[5],HelpPiv_List[1])
    YJ_Try_Parent(FootSCIkHandle,HelpPiv_List[5])
    YJ_Try_Parent(ToeSCIkHandle,HelpPiv_List[5])
    YJ_Try_Parent(LegFootIK_Handle,HelpPiv_List[4])
    YJ_Try_Parent(HelpPiv_List[0],LegIkCtrl_List[0])



def YJ_Get_PivJnt_Lis(FootJnt_List,Help_FootJnt_List,NamePre):
    OutJntList = Help_FootJnt_List
    ToeHelpJnt = duplicate( FootJnt_List[2],po=1,n = NamePre +"_toe_helpjnt")
    parent (ToeHelpJnt,w=1)
    BallHelpJnt = duplicate( FootJnt_List[1],po=1,n = NamePre +"_ball_helpjnt")
    parent (BallHelpJnt,w=1)
    WigglePiv = duplicate( FootJnt_List[1],po=1,n = NamePre +"_wiggle_helpjnt")
    parent (WigglePiv,w=1)
    OutJntList = OutJntList + ToeHelpJnt+BallHelpJnt+WigglePiv
    return OutJntList

def YJ_Get_PivGrp_Lis(HelpJnt_List):
    OutP_List = []
    for i in HelpJnt_List:
        OutP_Mem = YJ_Create_GrpToTarPiv(i)
        OutP_List.append(OutP_Mem)
    return OutP_List

def YJ_Create_GrpToTarPiv(Targ):
    Name =  Targ.replace('helpjnt','Piv')
    OutGrp = group( em=1, n=Name)
    YJ_MoveOrRotateToTargetWd(Targ,OutGrp,'t','t')
    return OutGrp