#YJ_CreatFkRig()YJ_GetBaseJntList(StarBaseJnt,EndBaseJnt)/
###YJ_DuplicateJntChain(AllBaseJntList,Suf)
#
#StarBaseJnt='L_upLeg_skin'
#EndBaseJnt='L_foot_skin'
#Axis='rz'
#YJ_CreateIkRig('L_upLeg_skin','L_foot_skin','rz')
#PvAnim=PvAnimName
#IK_solver='ikSCsolver',YJ_IK_Start_Joint=ToeIkJnt,YJ_IK_End_Joint=ToeEndIkJnt
#EndAnim=IkCtrlName
from maya.cmds import*
import maya.mel as mm


def YJ_CreateIkRig(StarBaseJnt,EndBaseJnt,Axis):
    AllBaseJntList=YJ_GetBaseJntList(StarBaseJnt,EndBaseJnt)
    newJntList=YJ_DuplicateJntChain(AllBaseJntList,'ik')
    StarIkJnt=newJntList[0]
    SecIKJnt=newJntList[1]
    EndIkJnt=newJntList[-1]
    YJ_setPreferredAngles(SecIKJnt,Axis,-45)
    YJ_IK_Name,IkCtrlName,PvAnimName,IkAnimGrpName,IkPvGrpName=YJ_CreateRpIK(StarIkJnt,SecIKJnt,EndIkJnt)
    YJ_SetParentConstraint(newJntList,AllBaseJntList)
    YJ_CreatIkStretch(StarIkJnt,SecIKJnt,EndIkJnt,IkCtrlName,PvAnimName,IkPvGrpName,IkAnimGrpName)
    YJ_MovePvAnim(Axis,PvAnimName)

def YJ_setPreferredAngles(InJnt,RoAttr,JntAng):
    setAttr(InJnt+'.'+RoAttr,JntAng)
    joint(InJnt,e=1 ,spa=1,ch=1)
    setAttr(InJnt+'.'+RoAttr,0)

def YJ_CreateRpIK(StarIkJnt,SecIKJnt,EndIkJnt):
    NameSufClass=YJ_returnSufAndPre()
    YJ_IK_Name=YJ_Creat_IK(StarIkJnt,EndIkJnt,'ikRPsolver')
    IkCtrlName=sdd_createCurveControl(1,EndIkJnt+NameSufClass['anim'])
    YJ_MoveOrRotateToTargetWd(YJ_IK_Name,IkCtrlName,'t','t')
    YJ_MoveOrRotateToTargetWd(YJ_IK_Name,IkCtrlName,'ro','r')
    parent(YJ_IK_Name,IkCtrlName)
    IkAnimGrpName=YJRigZeroGrp([IkCtrlName])
    PvAnimName=StarIkJnt+NameSufClass['pv']
    sdd_createCurveControl(10,PvAnimName)
    YJ_MoveOrRotateToTargetWd(SecIKJnt,PvAnimName,'t','t')
    YJ_MoveOrRotateToTargetWd(SecIKJnt,PvAnimName,'ro','r')

    poleVectorConstraint( PvAnimName, YJ_IK_Name)
    IkPvGrpName=YJRigZeroGrp([PvAnimName])
    YJ_LockAndHideAttr([IkCtrlName,PvAnimName],['sx','sy','sz','v'],1,0)

    return YJ_IK_Name,IkCtrlName,PvAnimName,IkAnimGrpName,IkPvGrpName


def YJ_Creat_IK(YJ_IK_Start_Joint,YJ_IK_End_Joint,IK_solver):
    YJ_IK_Name=YJ_IK_Start_Joint+'_IKHandle'
    YJ_IK_Effector_Name=YJ_IK_Name+'_effector'
    ikHandle(n=YJ_IK_Name, sj=YJ_IK_Start_Joint, ee=YJ_IK_End_Joint,solver=IK_solver)
    EfUPJntName=listRelatives(YJ_IK_End_Joint,p=1,typ='joint')[-1]
    effectorN=listRelatives(EfUPJntName,c=1,typ='ikEffector')[-1]
    rename (effectorN,YJ_IK_Effector_Name)
    return YJ_IK_Name

def YJ_MoveOrRotateToTargetWd(Target,CurObj,InAttr,OutAttr):
    exec'TargetXYZ = xform(Target,ws=1,q=1,%s=1)'%InAttr
    setAttr (CurObj+'.'+OutAttr ,TargetXYZ [0],TargetXYZ [1],TargetXYZ [2] )


def YJRigZeroGrp(TargetObjList):
    for TargetObj in TargetObjList:
        ParentObj=listRelatives(TargetObj,p=1)
        TargetXYZ = xform(TargetObj,ws=1,q=1,t=1)
        TargetXYZRo = xform(TargetObj,ws=1,q=1,ro=1)
        ZeroGrpName=TargetObj+'_ZeroGrp1'
        while objExists(ZeroGrpName):
            ZeroNub=int(ZeroGrpName[-1])+1
            ZeroGrpName=ZeroGrpName.replace(ZeroGrpName[-1],str(ZeroNub))
        group( em=True, name=ZeroGrpName )
        move (TargetXYZ [0],TargetXYZ [1],TargetXYZ [2],ZeroGrpName,r=1, os=1,wd=1)
        setAttr(ZeroGrpName+'.r', TargetXYZRo[0], TargetXYZRo[1], TargetXYZRo[2])
        parent( TargetObj, ZeroGrpName )
        if not ParentObj == None:
            parent( ZeroGrpName, ParentObj )
    return ZeroGrpName



def CreatLocToTarget(LocName,Target):
    NewLocName=spaceLocator(n=LocName,p=(0, 0, 0) )
    YJ_MoveOrRotateToTargetWd(Target,LocName,'t','t')
    return LocName


def CreatStretchCV(TargetWorldPv1 ,TargetWorldPv2,CVName):
    CvSuf=YJ_returnSufAndPre()['CvInf']
    TargetWorldPvVStar = xform(TargetWorldPv1 ,ws=1,q=1,t=1)
    TargetWorldPvVEnd = xform(TargetWorldPv2 ,ws=1,q=1,t=1)
    curve(n=CVName,d=1,p=[TargetWorldPvVStar ,TargetWorldPvVEnd ], k=[0,1] )
    curveInfoNode =arclen(CVName,ch=1)
    curveInfName=rename(curveInfoNode ,CVName+CvSuf)
    TargetWorldPv1_shapeNameL= listRelatives(TargetWorldPv1 ,s=1)
    TargetWorldPv2_shapeNameL= listRelatives(TargetWorldPv2 ,s=1)
    TargetCV_shapeNameL= listRelatives(CVName,s=1)
    connectAttr('%s.worldPosition[0]'%TargetWorldPv1_shapeNameL[-1],'%s.controlPoints[0]'%TargetCV_shapeNameL[-1],f=1)
    connectAttr('%s.worldPosition[0]'%TargetWorldPv2_shapeNameL[-1],'%s.controlPoints[1]'%TargetCV_shapeNameL[-1],f=1)
    return CVName,curveInfName

def YJ_CreateMDNode(IK_CV,CVLen,op):
    MSuf=YJ_returnSufAndPre()['mdM']
    DSuf=YJ_returnSufAndPre()['mdD']
    if op == 1:
        MD_Name=IK_CV+MSuf
    else:
        MD_Name=IK_CV+DSuf
    shadingNode('multiplyDivide',asUtility=1,n=MD_Name )
    setAttr('%s.operation'%MD_Name,op)
    setAttr('%s.input2X'%MD_Name,CVLen)
    return MD_Name

def YJ_CreatIkStretch(StarIkJnt,SecIKJnt,EndIkJnt,EndAnim,PvAnim,IkPvGrpName,IkAnimGrpName):
    LocSuf=YJ_returnSufAndPre()['loc']
    IkSuf=YJ_returnSufAndPre()['ik']
    CvSuf=YJ_returnSufAndPre()['cv']
    GrpSuf=YJ_returnSufAndPre()['grp']
    ConSuf=YJ_returnSufAndPre()['con']
    BTSuf=YJ_returnSufAndPre()['blendTwo']
    AllGrpSuf=YJ_returnSufAndPre()['allgrp']

    CvGrpName=StarIkJnt.replace(IkSuf,CvSuf)+GrpSuf
    IK_LocStart=CreatLocToTarget(StarIkJnt.replace(IkSuf,LocSuf),StarIkJnt)
    IK_LocSec=CreatLocToTarget(SecIKJnt.replace(IkSuf,LocSuf),SecIKJnt)
    IK_LocEnd=CreatLocToTarget(EndIkJnt.replace(IkSuf,LocSuf),EndIkJnt)


    parentConstraint( IK_LocStart,StarIkJnt,w=1,mo=1 )
    parent(IK_LocEnd,EndAnim)

    IK_Start_CV,IK_Start_CV_inf=CreatStretchCV(IK_LocStart ,IK_LocSec,StarIkJnt.replace(IkSuf,CvSuf))
    IK_Sec_CV,IK_Sec_CV_inf=CreatStretchCV(IK_LocSec ,IK_LocEnd,SecIKJnt.replace(IkSuf,CvSuf))
    IK_End_CV,IK_End_CV_inf=CreatStretchCV(IK_LocStart ,IK_LocEnd,EndIkJnt.replace(IkSuf,CvSuf))
    CvGrp=group( IK_Start_CV,IK_Sec_CV,IK_End_CV, n=CvGrpName )

    addAttr( IK_LocSec,longName='lock', attributeType='double',min=0,max=1,dv=0,k=1)
    addAttr( PvAnim,longName='lock', attributeType='double',min=0,max=1,dv=0,k=1)
    addAttr( EndAnim,longName='stretch', attributeType='double',min=0,max=1,dv=1,k=1)

    connectAttr('%s.lock'%PvAnim,'%s.lock'%IK_LocSec,f=1)
    parent(IK_LocSec,PvAnim)

    StartLen=getAttr('%s.arcLength'%IK_Start_CV_inf)
    SecLen=getAttr('%s.arcLength'%IK_Sec_CV_inf)
    AllLen=StartLen+SecLen

    IK_Start_CVD=YJ_CreateMDNode(IK_Start_CV,StartLen,2)
    IK_Sec_CVD=YJ_CreateMDNode(IK_Sec_CV,SecLen,2)
    IK_End_CVD=YJ_CreateMDNode(IK_End_CV,AllLen,2)

    IK_Start_CVM=YJ_CreateMDNode(IK_Start_CV,StartLen,1)
    IK_Sec_CVM=YJ_CreateMDNode(IK_Sec_CV,SecLen,1)
    IK_End_CVM=YJ_CreateMDNode(IK_End_CV,AllLen,1)

    ConNameNew=SecIKJnt.replace(IkSuf,ConSuf)
    shadingNode('condition',asUtility=1,n=ConNameNew)
    setAttr('%s.operation'%ConNameNew,2)
    setAttr('%s.secondTerm'%ConNameNew,1)

    ConStreNameNew=EndIkJnt.replace(IkSuf,ConSuf)
    shadingNode('condition',asUtility=1,n=ConStreNameNew)
    setAttr('%s.operation'%ConStreNameNew,2)
    setAttr('%s.secondTerm'%ConStreNameNew,1)

    SecIKBT=createNode( 'blendTwoAttr', n= IK_Start_CV+BTSuf)
    EndIKBT=createNode( 'blendTwoAttr', n=IK_Sec_CV+BTSuf )


    connectAttr('%s.arcLength'%IK_Start_CV_inf,'%s.input1X'%IK_Start_CVD,f=1)
    connectAttr('%s.arcLength'%IK_Sec_CV_inf,'%s.input1X'%IK_Sec_CVD,f=1)
    connectAttr('%s.arcLength'%IK_End_CV_inf,'%s.input1X'%IK_End_CVD,f=1)

    connectAttr('%s.outputX'%IK_End_CVD,'%s.input1X'%IK_End_CVM,f=1)
    connectAttr( '%s.outputX'%IK_End_CVM,'%s.firstTerm'%ConNameNew,f=1)
    connectAttr( '%s.outputX'%IK_End_CVM,'%s.colorIfTrueR'%ConNameNew,f=1)

    connectAttr( '%s.output'%SecIKBT,'%s.input1X'%IK_Start_CVM,f=1)
    connectAttr( '%s.output'%EndIKBT,'%s.input1X'%IK_Sec_CVM,f=1)
    connectAttr( '%s.outColorR'%ConNameNew,'%s.input[0]'%EndIKBT,f=1)

    connectAttr( '%s.outputX'%IK_Sec_CVD,'%s.input[1]'%EndIKBT,f=1)
    connectAttr( '%s.outColorR'%ConNameNew,'%s.input[0]'%SecIKBT,f=1)
    connectAttr( '%s.outputX'%IK_Start_CVD,'%s.input[1]'%SecIKBT,f=1)

    connectAttr( '%s.outputX'%IK_Sec_CVM,'%s.translateX'%EndIkJnt,f=1)
    connectAttr( '%s.outputX'%IK_Start_CVM,'%s.translateX'%SecIKJnt,f=1)

    connectAttr( '%s.lock'%IK_LocSec,'%s.attributesBlender'%SecIKBT,f=1)
    connectAttr( '%s.lock'%IK_LocSec,'%s.attributesBlender'%EndIKBT,f=1)
    connectAttr('%s.stretch'%EndAnim,'%s.input2X'%IK_End_CVM,f=1)

    connectAttr('%s.outputX'%IK_Start_CVD,'%s.colorIfTrueR'%ConStreNameNew,f=1)
    connectAttr('%s.outputX'%IK_Start_CVM,'%s.firstTerm'%ConStreNameNew,f=1)

    IkAllGrp=group(StarIkJnt,CvGrp,IK_LocStart,IkPvGrpName,IkAnimGrpName,n=StarIkJnt.replace(IkSuf,AllGrpSuf))
    hide(IK_LocStart,IK_LocSec,IK_LocEnd,CvGrp )
    return IkAllGrp,IK_LocSec

def YJ_MovePvAnim(Axis,PvAnimName):
    if Axis=='ry':
        setAttr('%s.tz'%PvAnimName,-2)
    elif Axis=='rz':
        setAttr('%s.ty'%PvAnimName,2)