#-------------------------------------------------------------------------------
# Name:        YJ_FK_RIG
# Purpose:     Create_IKFK_RIG
#
# Author:      YangJie
#
# Created:     08/04/2014
#Email:        691633493@
#-------------------------------------------------------------------------------

from maya.cmds import*
import maya.mel as mm
from YJ_Ctrl_Colour import*
from YJ_ParentShape import*
from YJ_IKFK_RIG_UI import*
from YJ_NameTool import*
from YJ_Rig_Pub import*


def YJ_CreatFkRig(StarBaseJnt,EndBaseJnt,judge):

    StarBaseJnt,EndBaseJnt = YJ_judge_get_jnt(StarBaseJnt,EndBaseJnt,judge)
    AllBaseJntList_O=YJ_GetBaseJntList(StarBaseJnt,EndBaseJnt)
    AllBaseJntList=Yj_Reset_JntSuf(AllBaseJntList_O)
    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)


def YJ_judge_get_jnt(StarBaseJnt,EndBaseJnt,judge):
    if judge == 1:
        StarBaseJnt,EndBaseJnt = StarBaseJnt,EndBaseJnt
    elif judge == 0:
        StarBaseJnt = textField('FK_star_tf',q=1 ,tx = 1)
        EndBaseJnt = textField('FK_end_tf',q=1 ,tx = 1)
    return StarBaseJnt,EndBaseJnt




#YJ_GetBaseJntList('joint1','joint1')



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)