from maya.cmds import*
from  YJ_Bone_SearchAndReplace_Name import*
from YJ_ParentShape import*
from YJ_Creat_Curve import*
#############Rig 
###########
def YJ_CreatBlendCoL_T(NodeName,InObj1,InObj2,OutObj):
    shadingNode('blendColors', asUtility=1,n=NodeName)
    connectAttr('%s.t'%InObj1,'%s.color1'%NodeName,f=1)
    connectAttr('%s.t'%InObj2,'%s.color2'%NodeName,f=1)
    connectAttr('%s.output'%NodeName,'%.t'%OutObj,f=1)

def YJ_CreatBlendCoL_O(NodeName,InObj1,InObj2,OutObj):
    shadingNode('blendColors', asUtility=1,n=NodeName)
    connectAttr('%s.r'%InObj1,'%s.color1'%NodeName,f=1)
    connectAttr('%s.r'%InObj2,'%s.color2'%NodeName,f=1)
    connectAttr('%s.output'%NodeName,'%s.r'%OutObj,f=1)
########Move To Target
def YJ_MoveToTargetWd(Target,CurObj):
    TargetXYZ = xform(Target,ws=1,q=1,t=1)
    setAttr ("%s.t"%CurObj ,TargetXYZ [0],TargetXYZ [1],TargetXYZ [2] )

def CreatStretchCV(TargetWorldPv1 ,TargetWorldPv2,NewCurveName):
    TargetWorldPvVStar = xform(TargetWorldPv1 ,ws=1,q=1,t=1)
    TargetWorldPvVEnd = xform(TargetWorldPv2 ,ws=1,q=1,t=1)
    curve(n=NewCurveName,d=1,p=[TargetWorldPvVStar ,TargetWorldPvVEnd ], k=[0,1] )
    curveInfoNode =arclen(NewCurveName,ch=1)
    rename(curveInfoNode ,NewCurveName+'_Inf')
    TargetWorldPv1_shapeNameL= listRelatives(TargetWorldPv1 ,s=1)
    TargetWorldPv2_shapeNameL= listRelatives(TargetWorldPv2 ,s=1)
    TargetCV_shapeNameL= listRelatives(NewCurveName,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)

def YJ_MoveToTargetPivot(Target,CurObj):
    TargetXYZ = xform(Target,ws=1,q=1,t=1)
    move(TargetXYZ [0],TargetXYZ [1],TargetXYZ [2],"%s.scalePivot"%CurObj ,"%s.rotatePivot"%CurObj )

def CreatLocToTarget(LocName,Target):
    spaceLocator(n=LocName,p=(0, 0, 0) )
    YJ_MoveToTargetWd(Target,LocName)

#########Create IK Stretch
def YJ_Creat_Stretch(IK_Start_Joint,IK_Sec_Joint,IK_End_Joint,EndAnim,PvAnim):

    IK_Start_Loc=IK_Start_Joint.replace('IK','CV_Loc')
    IK_Sec_Loc=IK_Sec_Joint.replace('IK','CV_Loc')
    IK_End_Loc=IK_End_Joint.replace('IK','CV_Loc')

    IK_Start_CV=IK_Start_Joint.replace('IK','CV')
    IK_Sec_CV=IK_Sec_Joint.replace('IK','CV')
    IK_End_CV=IK_End_Joint.replace('IK','CV')

    CreatLocToTarget(IK_Start_Loc,IK_Start_Joint)
    CreatLocToTarget(IK_Sec_Loc,IK_Sec_Joint)
    CreatLocToTarget(IK_End_Loc,IK_End_Joint)
    CreatStretchCV(IK_Start_Loc ,IK_Sec_Loc ,IK_Start_CV)
    CreatStretchCV(IK_Sec_Loc ,IK_End_Loc ,IK_Sec_CV)
    CreatStretchCV(IK_Start_Loc ,IK_End_Loc ,IK_End_CV)
    addAttr( IK_Sec_Loc,longName='Lock', attributeType='double',min=0,max=1,dv=1,k=1)
    addAttr( EndAnim,longName='Stretch', attributeType='double',min=0,max=1,dv=1,k=1)
    IK_Grp=IK_Start_Joint+'_Grp'
    Dis_Grp=IK_Start_Joint+'_Dis_Grp'
    group( em=True, name=IK_Grp)
    YJ_MoveToTargetWd(IK_Start_Joint,IK_Grp)
    group( em=True, name=Dis_Grp)
    YJ_MoveToTargetWd(IK_Start_Joint,Dis_Grp)
    parent(IK_Start_Loc,IK_Grp)
    parent(IK_Start_Joint,IK_Grp)
    parent(IK_Start_CV,Dis_Grp)
    parent(IK_Sec_CV,Dis_Grp)
    parent(IK_End_CV,Dis_Grp)
    parent(IK_End_Loc,EndAnim)


    StartLen=getAttr('%s_Inf.arcLength'%IK_Start_CV)
    SecLen=getAttr('%s_Inf.arcLength'%IK_Sec_CV)
    AllLen=StartLen+SecLen

    YJ_CreateMDNode(IK_Start_CV,StartLen,2)
    YJ_CreateMDNode(IK_Sec_CV,SecLen,2)
    YJ_CreateMDNode(IK_End_CV,AllLen,2)

    YJ_CreateMDNode(IK_Start_CV,StartLen,1)  
    YJ_CreateMDNode(IK_Sec_CV,SecLen,1)  
    YJ_CreateMDNode(IK_End_CV,AllLen,1)
    YJ_CreateConNode(IK_End_CV)
    createNode( 'blendTwoAttr', n='%s_blendTwo'% IK_Start_CV)
    createNode( 'blendTwoAttr', n='%s_blendTwo'%IK_Sec_CV )

    
    connectAttr('%s_Inf.arcLength'%IK_Start_CV,'%s_Div.input1X'%IK_Start_CV,f=1)
    connectAttr('%s_Inf.arcLength'%IK_Sec_CV,'%s_Div.input1X'%IK_Sec_CV,f=1)
    connectAttr('%s_Inf.arcLength'%IK_End_CV,'%s_Div.input1X'%IK_End_CV,f=1)
    connectAttr('%s_Div.outputX'%IK_End_CV,'%s_Mul.input1X'%IK_End_CV,f=1)
    connectAttr( '%s_Mul.outputX'%IK_End_CV,'%s_Con.firstTerm'%IK_End_CV,f=1)
    connectAttr( '%s_Mul.outputX'%IK_End_CV,'%s_Con.colorIfTrueR'%IK_End_CV,f=1)
    connectAttr( '%s_blendTwo.output'%IK_Start_CV,'%s_Mul.input1X'%IK_Start_CV,f=1)
    connectAttr( '%s_blendTwo.output'%IK_Sec_CV,'%s_Mul.input1X'%IK_Sec_CV,f=1)
    connectAttr( '%s_Con.outColorR'%IK_End_CV,'%s_blendTwo.input[0]'%IK_Sec_CV,f=1)
    connectAttr( '%s_Div.outputX'%IK_Sec_CV,'%s_blendTwo.input[1]'%IK_Sec_CV,f=1)
    connectAttr( '%s_Con.outColorR'%IK_End_CV,'%s_blendTwo.input[0]'%IK_Start_CV,f=1)
    connectAttr( '%s_Div.outputX'%IK_Start_CV,'%s_blendTwo.input[1]'%IK_Start_CV,f=1)
    connectAttr( '%s_Mul.outputX'%IK_Sec_CV,'%s.translateX'%IK_End_Joint,f=1)
    connectAttr( '%s_Mul.outputX'%IK_Start_CV,'%s.translateX'%IK_Sec_Joint,f=1)
    connectAttr( '%s_Loc.Lock'%IK_Sec_CV,'%s_blendTwo.attributesBlender'%IK_Start_CV,f=1)
    connectAttr( '%s_Loc.Lock'%IK_Sec_CV,'%s_blendTwo.attributesBlender'%IK_Sec_CV,f=1)

    connectAttr('%s.Stretch'%EndAnim,'%s_Mul.input2X'%IK_End_CV,f=1)
    connectAttr('%s.Lock'%PvAnim,'%s.Lock'%IK_Sec_Loc,f=1)

def YJ_CreateMDNode(IK_CV,CVLen,op):
    if op == 1:
        MD_Name=IK_CV+'_Mul'
    else:
        MD_Name=IK_CV+'_Div'     
    shadingNode('multiplyDivide',asUtility=1,n=MD_Name )    
    setAttr('%s.operation'%MD_Name,op)     
    setAttr('%s.input2X'%MD_Name,CVLen)

def YJ_CreateConNode(ConName): 
    ConNameNew=ConName+'_Con'
    shadingNode('condition',asUtility=1,n=ConNameNew) 
    setAttr('%s.operation'%ConNameNew,2) 
    setAttr('%s.secondTerm'%ConNameNew,1)
#########Rotate To Target
def YJ_RotateToTargetWd(Target,CurObj):
    TargetXYZ = xform(Target,ws=1,q=1,ro=1)
    setAttr ("%s.r"%CurObj ,TargetXYZ [0],TargetXYZ [1],TargetXYZ [2] )
########Rig IK
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)
    rename ("effector1",YJ_IK_Effector_Name)

##Creat L_leg_Fk_Shape
def YJ_Leg_Fk_shape(Leg_Fk_name):
    YJ_Creat_Ctrl(4,'CircleCurve')
    select ('CircleCurve.cv[0:7]',r=1)
    rotate (0,0,90,r=1,p=('0cm','0cm','0cm'),os=1)
    select ('CircleCurve',r=1)
    select (Leg_Fk_name,add=1)
    YJ_Parent_Shape()
    YJRigZeroGrp([Leg_Fk_name])

    
def YJ_HumanLegRig_FK(LegSide):
    #####Rig Left Leg>>>FK
    select ('%s_upLeg_Bone'%LegSide,r=1) 
    duplicate(rr=1,rc=1)
    YJ_SearchReplace_name('Bone1','FK')
    YJ_Leg_Fk_shape('%s_upLeg_FK'%LegSide)
    YJ_Leg_Fk_shape('%s_lowLeg_FK'%LegSide)
    YJ_Leg_Fk_shape('%s_foot_FK'%LegSide)
    YJ_Leg_Fk_shape('%s_toe_FK'%LegSide)


def YJ_HumanLegRig_IK(LegSide):
    select ('%s_upLeg_Bone'%LegSide,r=1)
    duplicate(rr=1,rc=1)
    YJ_SearchReplace_name('Bone1','IK') 

    setAttr('%s_lowLeg_IK.rotateZ'%LegSide,-45)
    joint('%s_lowLeg_IK'%LegSide,e=1 ,spa=1,ch=1)
    setAttr('%s_lowLeg_IK.rotateZ'%LegSide,0)

    YJ_Creat_IK('%s_upLeg_IK'%LegSide,'%s_foot_IK'%LegSide,'ikRPsolver')
    YJ_Creat_IK('%s_foot_IK'%LegSide,'%s_toe_IK'%LegSide,'ikSCsolver')
    YJ_Creat_IK('%s_toe_IK'%LegSide,'%s_toeEnd_IK'%LegSide,'ikSCsolver')

    CreatLocToTarget('%s_Foot_InSide_LocPv'%LegSide,'%s_foot_inside_PvBone'%LegSide)
    CreatLocToTarget('%s_Foot_OutSide_LocPv'%LegSide,'%s_foot_outside_PvBone'%LegSide)
    CreatLocToTarget('%s_Foot_Toe_LocPv'%LegSide,'%s_toeEnd_CBone'%LegSide)
    CreatLocToTarget('%s_Ball_LocPv'%LegSide,'%s_toe_CBone'%LegSide)
    CreatLocToTarget('%s_Heel_LocPv'%LegSide,'%s_Heel_PvBone'%LegSide)
    CreatLocToTarget('%s_wiggle_LocPv'%LegSide,'%s_toe_CBone'%LegSide)
    parent('%s_Ball_LocPv'%LegSide,'%s_Foot_InSide_LocPv'%LegSide)
    parent('%s_Foot_InSide_LocPv'%LegSide,'%s_Foot_OutSide_LocPv'%LegSide)
    parent('%s_Foot_OutSide_LocPv'%LegSide,'%s_Foot_Toe_LocPv'%LegSide)
    parent('%s_Foot_Toe_LocPv'%LegSide,'%s_Heel_LocPv'%LegSide)
    parent('%s_wiggle_LocPv'%LegSide,'%s_Heel_LocPv'%LegSide)
    parent('%s_upLeg_IK_IKHandle'%LegSide,'%s_Ball_LocPv'%LegSide)
    parent('%s_toe_IK_IKHandle'%LegSide,'%s_wiggle_LocPv'%LegSide)
    parent('%s_foot_IK_IKHandle'%LegSide,'%s_wiggle_LocPv'%LegSide)

    YJ_Creat_Ctrl(5,'%s_foot_IK_anim'%LegSide)
    YJ_MoveToTargetWd('%s_wiggle_LocPv'%LegSide,'%s_foot_IK_anim'%LegSide)
    YJ_MoveToTargetPivot('%s_foot_IK'%LegSide,'%s_foot_IK_anim'%LegSide)
    parent('%s_Heel_LocPv'%LegSide,'%s_foot_IK_anim'%LegSide)
    YJRigZeroGrp(['%s_foot_IK_anim'%LegSide])
    CreatLocToTarget('%s_Pv_loc'%LegSide,'%s_lowLeg_IK'%LegSide)
    YJ_RotateToTargetWd('%s_Pv_loc'%LegSide,'%s_lowLeg_IK'%LegSide)
    poleVectorConstraint( '%s_Pv_loc'%LegSide, '%s_upLeg_IK_IKHandle'%LegSide)

    addAttr( '%s_foot_IK_anim'%LegSide,longName='Roll', attributeType='double',k=1)
    addAttr( '%s_foot_IK_anim'%LegSide,longName='ToeLift', attributeType='double',k=1)
    addAttr( '%s_foot_IK_anim'%LegSide,longName='ToeStraight', attributeType='double',k=1)
    addAttr( '%s_foot_IK_anim'%LegSide,longName='Lean', attributeType='double',k=1)
    addAttr( '%s_foot_IK_anim'%LegSide,longName='Side', attributeType='double',k=1)
    addAttr( '%s_foot_IK_anim'%LegSide,longName='ToeSpin', attributeType='double',k=1)
    addAttr( '%s_foot_IK_anim'%LegSide,longName='ToeWiggle', attributeType='double',k=1)
    addAttr( '%s_foot_IK_anim'%LegSide,longName='Stretch', attributeType='double',k=1)

    expression( s="$roll = %s_foot_IK_anim.Roll;\n$toeLift = %s_foot_IK_anim.ToeLift;\
               \n$toeStraight = %s_foot_IK_anim.ToeStraight;\n$lean = %s_foot_IK_anim.Lean;\
               \n$side = %s_foot_IK_anim.Side;\n$spin = %s_foot_IK_anim.ToeSpin;\
               \n$wiggle = %s_foot_IK_anim.ToeWiggle;\n%s_Heel_LocPv.rotateX = min($roll,0);\
               \n%s_Ball_LocPv.rotateX = (linstep(0,$toeLift,$roll)) *(1-(linstep($toeLift,$toeStraight,$roll))) * $roll;\
               \n%s_Foot_Toe_LocPv.rotateX = linstep($toeLift, $toeStraight, $roll) * $roll;\
               \n%s_Ball_LocPv.rotateY = $lean;\
               \n%s_Foot_OutSide_LocPv.rotateZ = min($side,0);\n%s_Foot_InSide_LocPv.rotateZ = max(0,$side);\
               \n%s_Foot_Toe_LocPv.rotateY = $spin;\n%s_wiggle_LocPv.rotateX = $wiggle;"%(LegSide,LegSide,LegSide,LegSide,LegSide,LegSide,LegSide,LegSide,LegSide,LegSide,LegSide,LegSide,LegSide,LegSide,LegSide),n='%s_foot_expr'%LegSide )