#-------------------------------------------------------------------------------
# Name:        YJ_Rig_Pub
# Purpose:     Rig will use
#
# Author:      YangJie
#
# Created:     10/04/2014
# Email:      691633493@
#-------------------------------------------------------------------------------
from maya.cmds import*
from YJ_NameTool import*

import pickle

ListPath='E:\\YJ_maya_srcipt\\prefs\\save\\'
def YJ_Save_List(InList,FileName):
    CurListPath = ListPath + FileName
    output = open(CurListPath, 'wb')
    pickle.dump(InList, output,-1)
    output.close()

def YJ_Load_List(FileName):
    CurListPath = ListPath + FileName
    pkl_file = open(CurListPath, 'rb')
    data = pickle.load(pkl_file)
    pkl_file.close()
    return data


def YJ_MirJoint(InJointList):
    StarJnt = InJointList[0]
    OutJntList = mirrorJoint(StarJnt,myz=1,mb=1,sr=['L','R'])
    return OutJntList

def Yj_Reset_JntSuf(in_List):
    CurList=[]
    for i in in_List:
        if i[-5:] != '_skin':
            New_List = [i]
            CurCh=i+'_skin'
            YJ_Add_Suffix('_skin',New_List,1)
            CurList.append(CurCh)
        else:
            CurList.append(i)

    return CurList

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_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_getNewList(InList):
    OutList=[]
    for i in InList:
        OutList.append(i)
    return OutList

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 YJ_Try_Parent(Fir,Sec):
    try:
        parent(Fir,Sec)
    except:
        pass


def YJ_resetList_RemoveEmp(inList):
    outList = []
    for i in inList:
        if i != []:
            outList.append(i)
    return outList

def YJ_FreezeAttr(JntList):
    StarJnt = JntList[0]
    if objectType(StarJnt) != 'joint':
        makeIdentity(StarJnt,apply=1, t=1,r=1 ,s=1 ,n=0,pn=1)
    elif objectType(StarJnt) == 'joint':
        makeIdentity(StarJnt,apply=1, t=1,r=1 ,s=1 ,n=0,jo=1,pn=1)

#AddAttr
def YJ_AddAttr(CtrL,AttrList):
    for i in range(len(AttrList)):
        if AttrList[i][0] == 'Kind':
            addAttr( CtrL,longName = AttrList[i][1], attributeType='float',k=1)
            setAttr(CtrL+'.'+AttrList[i][1],e=1,cb=1)
        if AttrList[i][0] == 'Fl':
            addAttr( CtrL,longName = AttrList[i][1], attributeType='float',k=1)
        if AttrList[i][0] == 'Bl':
            addAttr( CtrL,longName = AttrList[i][1], attributeType='float' ,min = 0 ,max = 1, k=1)


def YJ_GetMir_List(InList):
    OutList=[]
    for i in range(len(InList)) :
        if 'L_' in InList[i]:
            OutList.append(InList[i].replace('L_','R_'))
        elif 'R_' in InList[i]:
            OutList.append(InList[i] .replace('R_','L_') )
        else :
            OutList = OutList.append(InList[i])
    return OutList

def YJ_OrientJnt(JntList,orientJoint,SecAO):
    StarJnt=JntList[0]
    EndJnt=JntList[-1]
    joint(StarJnt,e=1,oj=orientJoint,sao=SecAO,ch=1,zso=1)
    joint(EndJnt,e=1,oj='none',ch=0,zso=1)


####DupJnts
def YJ_DuplicateJnt(InJntList,OldSuf,Suf):
    newJntList=[]
    for i in InJntList:
        NewName=i.replace(OldSuf,Suf)
        NewJnt=duplicate(i,rr=1,po=1,n=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_Scale_Shape(CtrlList,ScaleV):
    ShapeList = []
    for i in CtrlList:
        ShapeNameLis= listRelatives(i,s=1)
        ShapeName = ShapeNameLis[0]
        ShapeList.append(ShapeName)
        scale( ScaleV, ScaleV, ScaleV, ShapeName+'.cv[:]', r=1)


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


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)
    setAttr (YJ_IK_Name+".v",0)
    return YJ_IK_Name

def CreatGRpToTarg(GrpName,Target):
    Grp = group( em=True, name=GrpName)
    YJ_MoveOrRotateToTargetWd(Target,Grp,'t','t')
    return Grp


def YJ_getTranValue_PM(obj,Attr):
    Va = getAttr(obj+Attr)
    if Va >= 0:
        PM = 1
    else:
        PM = -1
    return PM


#####
def YJ_GetShape_List(Ctrl_Lis):
    ShapeList = []
    for i in Ctrl_Lis:
        shapeName = listRelatives(i,s=1)[0]
        ShapeList.append(shapeName)
    return ShapeList


##key1 fk
#Key1,Key2,Attr,ParConLis = 'FK','IK',IKFKAttr,ParConLis
def YJ_ConnectIKFKAttr(Key1,Key2,Attr,ParConLis,FK_Grp,IK_Grp):
    Key1_Attr_List = []
    Key2_Attr_List = []
    for i in ParConLis:
        Attr_i_LisAt = parentConstraint( i, q=1,wal=1)
        Attr_i_Lis =[i+'.'+Attr_i_LisAt[0]]+[i+'.'+Attr_i_LisAt[1]]
        for j in Attr_i_Lis:
            if Key1 in j :
                Key1_Attr_List.append(j)
            if Key2 in j :
                Key2_Attr_List.append(j)
    NameLis =  Attr.split('.')
    RevName = shadingNode('reverse',asUtility=1,n=NameLis[0]+"_Rev" )
    connectAttr(Attr,RevName+'.inputX')

    for i in range(len(Key1_Attr_List)):
        connectAttr(Attr,Key2_Attr_List[i])
        connectAttr(RevName+'.outputX',Key1_Attr_List[i])

    connectAttr(RevName+'.outputX',FK_Grp+".v")
    connectAttr(Attr,IK_Grp+".v")


    return Key1_Attr_List,Key1_Attr_List