#-------------------------------------------------------------------------------
# Name:       YJ_IK_Rig
# Purpose:
#
# Author:      YangJie
# Created:     15/04/2014
# Email:      691633493@
#-------------------------------------------------------------------------------
from maya.cmds import*
import maya.mel as mm
from YJ_Rig_Pub import*
from YJ_Ctrl_Colour import*
from YJ_FK_Rig import*
#StarBaseJnt,EndBaseJnt = YJ_judge_get_jnt('hh','hh',0)
#StarBaseJnt,EndBaseJnt,judge,Axis,PN = 'R_up_leg_jnt','R_foot_jnt',1,'rz','-'

def YJ_CreateIkRig(StarBaseJnt,EndBaseJnt,judge,Axis,PN):
    StarBaseJnt,EndBaseJnt,Axis,PN = YJ_judge_get_jntAX(StarBaseJnt,EndBaseJnt,Axis,PN,judge)
    AllBaseJntList_O=YJ_GetBaseJntList(StarBaseJnt,EndBaseJnt)
    YJ_setPreferredAngles(AllBaseJntList_O[1],Axis,PN)
    YJ_IK_Name,IkCtrlName,PvAnimName,IkAnimGrpName,IkPvGrpName=YJ_CreateRpIK(AllBaseJntList_O[0],AllBaseJntList_O[1],AllBaseJntList_O[-1])
    IkAllGrp,IK_LocSec,VPM= YJ_CreatIkStretch(AllBaseJntList_O[0],AllBaseJntList_O[1],AllBaseJntList_O[-1],IkCtrlName,PvAnimName,IkPvGrpName,IkAnimGrpName)
    PM_Final = YJ_GetTru_PV_PM(PN,VPM)
    YJ_MovePvAnim(Axis,PvAnimName,PM_Final)
    CrtlList = [IkCtrlName] + [PvAnimName]
    return IkAllGrp,CrtlList,YJ_IK_Name
def YJ_judge_get_jntAX(StarBaseJnt,EndBaseJnt,Axis,PN,judge):
    if judge == 1:
        StarBaseJnt,EndBaseJnt,Axis,PN = StarBaseJnt,EndBaseJnt,Axis,PN
    elif judge == 0:
        StarBaseJnt = textField('IK_star_tf',q=1 ,tx = 1)
        EndBaseJnt = textField('IK_end_tf',q=1 ,tx = 1)
        Axis = radioCollection('IK_RC',q=1,sl=1)
        PN_Sl=radioCollection('IK_PN_RC',q=1,sl=1)
        if PN_Sl =="Positive" :
            PN = '+'
        elif PN_Sl =="Negative" :
            PN = '-'
    return StarBaseJnt,EndBaseJnt,Axis,PN


def YJ_setPreferredAngles(InJnt,RoAttr,PN):
    AngStr=PN+'75'
    Ang=int(AngStr)
    setAttr(InJnt+'.'+RoAttr,Ang)
    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')
    YJ_Creat_Ctrl("Cur0",EndIkJnt+NameSufClass['anim'])
    IkCtrlName=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']
    YJ_Creat_Ctrl("Cur7",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 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

#StarIkJnt,SecIKJnt,EndIkJnt,EndAnim,PvAnim,IkPvGrpName,IkAnimGrpName = StarIkJnt,SecIKJnt,EndIkJnt,IkCtrlName,PvAnimName,IkPvGrpName,IkAnimGrpName
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+CvSuf+GrpSuf
    IK_LocStart=CreatLocToTarget(StarIkJnt+LocSuf,StarIkJnt)
    IK_LocSec=CreatLocToTarget(SecIKJnt+LocSuf,SecIKJnt)
    IK_LocEnd=CreatLocToTarget(EndIkJnt+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+CvSuf)
    IK_Sec_CV,IK_Sec_CV_inf=CreatStretchCV(IK_LocSec ,IK_LocEnd,SecIKJnt+CvSuf)
    IK_End_CV,IK_End_CV_inf=CreatStretchCV(IK_LocStart ,IK_LocEnd,EndIkJnt+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)
    #
    #
    VPM = YJ_getTranValue_PM(SecIKJnt,'.tx')
    StartLenVa =VPM *StartLen
    SecLenVa = VPM * SecLen
    AllLenVa = VPM * AllLen

    IK_Start_CVM=YJ_CreateMDNode(IK_Start_CV,StartLenVa,1)
    IK_Sec_CVM=YJ_CreateMDNode(IK_Sec_CV,SecLenVa,1)
    IK_End_CVM=YJ_CreateMDNode(IK_End_CV,AllLenVa,1)

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

    ConStreNameNew=EndIkJnt+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+AllGrpSuf)
    hide(IK_LocStart,IK_LocSec,IK_LocEnd,CvGrp )
    return IkAllGrp,IK_LocSec,VPM

def YJ_MovePvAnim(Axis,PvAnimName,VPM):

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


def YJ_GetTru_PV_PM(PN,VPM):
    PNStr = PN+'1'
    PNVa =  int(PNStr)*(-1)
    PMout = PNVa*VPM
    return PMout