#====================================================================================================
#Create_IKFK_RIG
#
#Designer:YangJie
#Email:691633493@
#Version:1.0
#============================================================================================
from maya.cmds import*
import maya.mel as mm
from YJ_Ctrl_Colour import*
from YJ_ParentShape import*
#YJ_CreatFkRig('joint1','joint3')
#StarBaseJnt='joint1' EndBaseJnt='joint3'

def YJ_CreatFkRig(StarBaseJnt,EndBaseJnt):

    AllBaseJntList=YJ_GetBaseJntList(StarBaseJnt,EndBaseJnt)
    newJntList=YJ_DuplicateJntChain(AllBaseJntList,'fk')
    YJ_CreatFkLength(newJntList)
    JntParConList=YJ_SetParentConstraint(newJntList,AllBaseJntList)
    YJ_LockAndHideAttr(newJntList,['tx','ty','tz','sx','sy','sz','v','radi'],1,0)

#YJ_GetBaseJntList('joint1','joint1')
def YJ_GetBaseJntList(StarBaseJnt,EndBaseJnt):
    AllBaseJntList=[]
    JudgeJnt=StarBaseJnt
    while JudgeJnt != EndBaseJnt:
        AllBaseJntList.append(JudgeJnt)
        JudgeJnt=listRelatives(JudgeJnt,c=1,typ='joint')
        if len(JudgeJnt)!=0:
            JudgeJnt=JudgeJnt[-1]
        if JudgeJnt==None and len(AllBaseJntList)==1 :
            AllBaseJntList=[]
            break

    if len(AllBaseJntList) != 0:
        AllBaseJntList.append(EndBaseJnt)
    return AllBaseJntList


def YJ_Arm_Fk_shape(Arm_Fk_name):
    CurveName=Arm_Fk_name+'Curve'
    YJ_Creat_Ctrl("Cur5",CurveName)
    YJ_Parent_Shape(CurveName,Arm_Fk_name,0,0)

def YJ_CreatFkLength(InJntList):
    for i in InJntList[:-1]:
        addAttr( i,longName='length', attributeType='double',dv=1,k=1)
        ChrJnt=listRelatives(i,c=1,typ='joint')[0]
        length=getAttr(ChrJnt+'.tx')
        shadingNode('multiplyDivide',asUtility=1,n=i+'_MD')
        setAttr('%s.input2X'%(i+'_MD'),length)
        connectAttr('%s.length'%i,'%s.input1X'%(i+'_MD'),f=1)
        connectAttr('%s.outputX'%(i+'_MD'),'%s.tx'%ChrJnt,f=1)


def YJ_DuplicateJntChain(AllBaseJntList,Su):
    SufClass=YJ_returnSufAndPre()
    skin=SufClass['skin']
    SufN=SufClass[Su]
    newJntList=[]
    for i in AllBaseJntList:
        NewName=i[:-len(skin)]+SufN
        if not objExists(NewName):
            NewJnt=duplicate(i,rr=1,po=1,n=NewName)
        if Su == 'fk':
            YJ_Arm_Fk_shape(NewName)
        newJntList.append(NewName)
    for i in range(len(newJntList)-1):
        parent(newJntList[i+1],newJntList[i])
    return newJntList



def YJ_returnSufAndPre():
    SufAndPreClass={'fk':'_fk','ik':'_ik','skin':'_skin','SideL':'L_','SideR':'R_','anim':'_anim','pv':'_pv_anim','loc':'_loc',
    'CvInf':'_cvinf','cv':'_cv','grp':'_grp','mdM':'_mul','mdD':'_div','con':'_con','blendTwo':'_blendtwo','allgrp':'_allgrp'
    }
    return SufAndPreClass

def YJ_SetParentConstraint(newJntList,AllBaseJntList):
    LisNub=len(newJntList)
    JntParConstraintList=[]
    for i in range(0,LisNub):
        parentConstraint( newJntList[i],AllBaseJntList[i],w=1 )
        JntParCons=listRelatives(AllBaseJntList[i],c=1,ni=1,typ='parentConstraint')[0]
        JntParConstraintList.append(JntParCons)
    return JntParConstraintList

def YJ_LockAndHideAttr(CtrlList,AttrList,Lo,KeAndCb):
    for j in CtrlList:
        for i in AttrList:
            setAttr (j+'.'+i,lock=Lo,k=KeAndCb,cb=KeAndCb)