#-------------------------------------------------------------------------------
# Name:        YJ_Neck_Rig
# Purpose:     Neck_Rig
#
# Author:      YangJie
#
# Created:     13/07/2014
#Email:        691633493@
#-------------------------------------------------------------------------------
from YJ_Rig_Pub import*
from YJ_spline_ik import*

Neck_Jnt_List = YJ_Load_List('Neck_Jnt_List.pkl')
Neck_IKFK_Type = radioCollection('Neck_radioCollection',q=1,sl=1)
Neck_Ctrl_Nub = int(intFieldGrp('NeckCtrlSegIFG' ,q=1,v=1)[0])


def YJ_Neck_Rig():
    ZeroCreate = YJRigZeroGrp([Neck_Jnt_List[0]])
    parent(ZeroCreate,w=True )
    Neck_Grp = rename(ZeroCreate[0], 'Neck_Grp' )
    NeckIK_List = YJ_DuplicateJnt(Neck_Jnt_List,'jnt','NeckIK')
    ConstrnList = YJ_SetParentConstraint(NeckIK_List,Neck_Jnt_List)
    NeckCtrlLis,NeckCtrZeroGrplLis,Neck_O_All_grp= YJ_CreateSplineIK_All(NeckIK_List[0],NeckIK_List[-1],1,Neck_Ctrl_Nub)
    YJ_Try_Parent(Neck_O_All_grp,Neck_Grp)
    YJ_Scale_Shape(NeckCtrlLis,0.1)
    YJ_Jud_Cre_NeckIKFKCtrl(NeckCtrlLis,NeckCtrZeroGrplLis)
    return Neck_Grp


def YJ_Jud_Cre_NeckIKFKCtrl(NeckCtrlLis,NeckCtrZeroGrplLis):
    if Neck_IKFK_Type != 'Neck_IK':
        YJ_Connect_NeckAttr(NeckCtrlLis,NeckCtrZeroGrplLis)
    else:
        print 'ha'



def YJ_Neck_Fk_Ctrl(NeckCtrlLis,NeckCtrZeroGrplLis):
    NeckCtrlPLis = NeckCtrlLis[:-1]
    SpinGrpCharLis = NeckCtrZeroGrplLis[1:]
    ParCosList = []
    for i in range(len(NeckCtrlPLis)):
        Cons = parentConstraint( NeckCtrlPLis[i],SpinGrpCharLis[i],w=1,mo=1)
        ParCosList.append(Cons[0])
    return  ParCosList


def YJ_Connect_NeckAttr(NeckCtrlLis,NeckCtrZeroGrplLis):
    ParCosList = YJ_Neck_Fk_Ctrl(NeckCtrlLis,NeckCtrZeroGrplLis)
    NeckAttr = YJ_Add_NeckIKFK_Attr(NeckCtrlLis[0])
    NeckAttrStr = NeckCtrlLis[0] + "." + NeckAttr[1][1]
    RevName = shadingNode('reverse',asUtility=1,n="Neck_Rev" )
    connectAttr(NeckAttrStr,RevName+'.inputX')
    for i in ParCosList:
        Attr_i_LisAt = parentConstraint( i, q=1,wal=1)
        Attr_i_Str =i+'.'+Attr_i_LisAt[0]
        connectAttr(RevName+'.outputX',Attr_i_Str)


def YJ_Add_NeckIKFK_Attr(Ctrl):
    AttrList=[['Kind','Basic'],['Bl','IKFK']]
    YJ_AddAttr(Ctrl,AttrList)
    return AttrList