#===============================================================================================
# Author: SunDongDong
# WinProc: sdd_FastRig()
# E-mail: 136941679@
# Company: China-Najing-OF3D
# Version: 1.1.1
#===============================================================================================

from maya.cmds import *
import maya.mel as mm

def sdd_FastRigWin():
    if(window('FastRigWin',q=1,ex=1)):
        deleteUI('FastRigWin',window=1)
    window('FastRigWin',rtf=1,menuBar=1,s=1,t='FastRigWin')

    menu( label='File', tearOff=True )
    menuItem( label='reset' )
    menuItem( label='save' )

    tabLayout('frMainTL')
    ##Tab1
    columnLayout('frTempCL')
    frameLayout(p='frTempCL',l='Template',cll=0,cl=0,mh=10,mw=50,w=240)
    columnLayout()
    optionMenuGrp('frSpineSegIS',l='Type:',cw2=[40,100],ad2=2,cc='sdd_changeTypeGrp()')
    menuItem( label='Human' )
    menuItem( label='Horse' )
    menuItem( label='Cat' )
    menuItem( label='Monster' )

    frameLayout(p='frTempCL',l='Segment',cll=0,cl=0,mh=10,mw=10,w=240)
    columnLayout()
    intFieldGrp('frNeckSegIG',l='Neck Seg:',extraLabel='seg',v1=1,cw3=[80,80,30],ad3=3)
    intFieldGrp('frSpineSegIG',l='Spine Seg:',extraLabel='seg',v1=4,cw3=[80,80,30],ad3=3)
    rowLayout(nc=2,cw2=[200,30])
    intFieldGrp('frTailSegIG',l='Tail Seg:',en=0,extraLabel='seg',v1=5,cw3=[80,80,30],ad3=3)
    checkBox('frTailSegCB',l='',cc='sdd_chageTailControl()')

    frameLayout(p='frTempCL',l="Extra*",cll=0,cl=0,mh=10,mw=10,w=240)
    columnLayout('frExtraCL')
    rowLayout(nc=2,cw2=[200,30])
    intFieldGrp('frHandNumIG',l='Mtil-Arm:',en=0,extraLabel='num',v1=0,cw3=[80,80,30],ad3=3)
    checkBox('frHandNumCB',l='',cc='sdd_enabelControl("intFieldGrp","frHandNumIG")')
    rowLayout(p='frExtraCL',nc=2,cw2=[200,30])
    intFieldGrp('frLegNumIG',l='Mtil-Leg:',en=0,extraLabel='num',v1=0,cw3=[80,80,30],ad3=3)
    checkBox('frLegNumCB',l='',cc='sdd_enabelControl("intFieldGrp","frLegNumIG")')

    frameLayout(p='frTempCL',l="Jnt Import",cll=0,cl=0,mh=10,mw=10,w=240)
    columnLayout()
    button(w=210,h=35,l='Import',c='sdd_importJntTemptale()')

    ##Tab2
    columnLayout('frRigCL',p='frMainTL')

    frameLayout(p='frRigCL',l="Rigging Options",cll=0,cl=0,mh=10,mw=10,w=240)
    columnLayout('frRigOptionCL')

    radioCollection()
    rowLayout(p='frRigOptionCL',nc=4,cw4=[50,55,55,55],cal=[(1,'right')])
    text(l='Arm:',w=40)
    radioButton(l='FK')
    radioButton(l='IK')
    radioButton(l='IKFK',sl=1)

    radioCollection()
    rowLayout(p='frRigOptionCL',nc=4,cw4=[50,55,55,55],cal=[(1,'right')])
    text(l='Leg:',w=40)
    radioButton(l='FK')
    radioButton(l='IK')
    radioButton(l='IKFK',sl=1)

    radioCollection()
    rowLayout(p='frRigOptionCL',nc=4,cw4=[50,55,55,55],cal=[(1,'right')])
    text(l='Spine:',w=40)
    radioButton(l='FK',sl=1)
    text(l='')
    radioButton(l='IKFK')

    radioCollection()
    rowLayout(p='frRigOptionCL',nc=4,cw4=[50,55,55,55],cal=[(1,'right')])
    text(l='Neck:',w=40)
    radioButton(l='FK',sl=1)
    text(l='')
    radioButton(l='IKFK')

    radioCollection()
    rowLayout(p='frRigOptionCL',nc=4,cw4=[50,55,55,55],cal=[(1,'right')])
    text('frRigTailTx',l='Tail:',w=40,en=0)
    radioButton('frRigTailFkRB',l='FK',sl=1,en=0)
    text(l='',en=0)
    radioButton('frRigTailIkFkRB',l='IKFK',en=0)

    frameLayout(p='frRigCL',l="Build",cll=0,cl=0,mh=10,mw=10,w=240)
    columnLayout()
    button(w=210,h=35,l='Rigging',c='pass')
    #Tab End
    tabLayout('frMainTL',e=1,tli=[(1,'Template'),(2,'Ringging')])
    showWindow('FastRigWin')

#GUI Function - enable control
#
def sdd_getWindowControlParameter():
    #Get Windows Control Parameter
    neckSeg   =intFieldGrp('frNeckSegIG',q=1,v=1)[0]-1
    spineSeg  =intFieldGrp('frSpineSegIG',q=1,v=1)[0]-1
    tailSeg   =0
    handNum   =0
    legNum    =0
    if(checkBox('frTailSegCB',q=1,v=1)):
        tailSeg=intFieldGrp('frTailSegIG',q=1,v=1)[0]-1
    if(checkBox('frHandNumCB',q=1,v=1)):
        handNum=intFieldGrp('frHandNumIG',q=1,v=1)[0]
    if(checkBox('frHandNumCB',q=1,v=1)):
        legNum =intFieldGrp('frLegNumIG',q=1,v=1)[0]
    return neckSeg,spineSeg,tailSeg,handNum,legNum

def sdd_enabelControl(cType,idx):
    if(mm.eval('%s -q -en "%s"'%(cType,idx))):
        mm.eval('%s -e -en 0 "%s"'%(cType,idx))
    else:
        mm.eval('%s -e -en 1 "%s"'%(cType,idx))

def sdd_chageTailControl():
    enb=1
    if(intFieldGrp('frTailSegIG',q=1,en=1)):
        enb=0
    intFieldGrp('frTailSegIG',e=1,en=enb)
    text('frRigTailTx',e=1,en=enb)
    radioButton('frRigTailFkRB',e=1,en=enb)
    radioButton('frRigTailIkFkRB',e=1,en=enb)



def sdd_changeTypeGrp():
    typ=optionMenuGrp('frSpineSegIS',q=1,sl=1)
    if(typ==2 or typ==3):
        checkBox('frTailSegCB',e=1,v=1)
        intFieldGrp('frTailSegIG',e=1,en=1)
    else:
        checkBox('frTailSegCB',e=1,v=0)
        intFieldGrp('frTailSegIG',e=1,en=0)

def sdd_importJntTemptale():
    typ=optionMenuGrp('frSpineSegIS',q=1,sl=1)
    if(typ==1):
        sdd_importHumanMonsterJntTemptale('Human')
    if(typ==2):
        sdd_importHorseCatJntTemptale('Horse')
    if(typ==3):
        sdd_importHorseCatJntTemptale('Cat')
    if(typ==4):
        sdd_importHumanMonsterJntTemptale('Monster')


#Init Joint Prefix
#
def sdd_returnJntPrefix():
    lPre='L_'
    rPre='R_'
    suf ='_ski'
    rad =0.1
    ext ='_ext'
    return lPre,rPre,suf,rad,ext

#Init Human Defult Name
#
def sdd_returnHumanMonsterJntInitName(typ):
    armBName   =['shoulder','arm','foreArm','hand']
    thumbBName =['thumb','thumb_end']
    indexBName =['index','index_end']
    middleBName=['middle','middle_end']
    ringBName  =['ring','ring_end']
    pinkyBName =['pinky','pinky_end']
    footBName  =['heel','toe','toeTop']
    spineBName =['spine','spine_end']
    neckBName  =['neck','neck_end']
    hipBName   =['hip']
    headBName  =['headTop']
    tailBName  =['tail','tail_end']
    jawBName   =['jaw','jaw_end']
    eyeBName   =['eye']

    if(typ=='Human'):
        legBName   =['upLeg','lowLeg','foot']
    else:
        legBName   =['rootLeg','upLeg','lowLeg','ankle','foot']
    return armBName,thumbBName,indexBName,middleBName,ringBName,pinkyBName,legBName,footBName,spineBName,neckBName,hipBName,headBName,tailBName,jawBName,eyeBName

#Init Human Defult Postion
#
def sdd_returnHumanMonsterJntInitPos(typ):
    armBPos   =[[0.42, 13.5, 0.0],[1.48, 13.5, 0],[3.8, 13.5, -0.2],[6.1, 13.5, 0]]
    thumbBPos =[[6.5, 13.5, 0.4],[7.33, 13.5, 0.4]]
    indexBPos =[[7, 13.5, 0.2],[7.78,13.5, 0.2]]
    middleBPos=[[7, 13.5,0],[7.78,13.5,0]]
    ringBPos  =[[7, 13.5,-0.2],[7.78,13.5,-0.2]]
    pinkyBPos =[[7, 13.5,-0.4],[7.78,13.5,-0.4]]
    footBPos  =[[0.73, 0, -0.6],[0.73, 0, 1.44],[0.73,0, 2.25]]
    spineBPos =[[0.0, 9.2, 0.0],[0, 12.4, 0]]
    hipBPos   =[[0.0, 9, 0.0]]
    neckBPos  =[[0.0,14.4,0.0],[0,15.6,0]]
    headBPos  =[[0,17,0]]
    tailBPos  =[[0, 9, -1],[0.0, 9, -4]]
    jawBPos   =[0, 16, 0.25],[0, 15.5, 1]
    eyeBPos   =[[0.5, 16.5, 0.45]]

    if(typ=='Human'):
        legBPos   =[[0.73, 8.5, 0],[0.73, 5.33, 0.18],[0.73, 1.3,0]]
    else:
        legBPos   =[[0.73, 8.5, 0],[0.73, 6.7, 0.44] ,[0.73, 4.73, -0.28],[0.73, 2.29, 0],[0.73, 0.68, 0]]
    return armBPos,thumbBPos,indexBPos,middleBPos,ringBPos,pinkyBPos,legBPos,footBPos,spineBPos,neckBPos,hipBPos,headBPos,tailBPos,jawBPos,eyeBPos

#Init Horse Defult Name
#
def sdd_returnHorseJntInitName():
    armBName   =['scapula_skin', 'shoulder_skin', 'frontUpLeg_skin', 'frontKnee_skin', 'frontFoot_skin', 'frontToe_skin', 'frontToeEnd_skin']
    legBName   =['rootLeg_skin', 'backUpLeg_skin', 'backKnee_skin', 'backFoot_skin', 'backToe_skin', 'backToeEnd_skin']
    spineBName =['spine','spine_end']
    tailBName  =['tail','tail_end']
    neckBName  =['neck','neck_end']
    headBName  =['headTop']
    hipBName   =['hip']
    jawBName   =['jaw','jaw_end']
    eyeBName   =['eye']
    return armBName,legBName,spineBName,tailBName,neckBName,headBName,hipBName,jawBName,eyeBName

#Init Horse Defult Postion
#
def sdd_returnHorseJntInitPos():
    armBPos   =[[1.55, 10.54, 3.98], [1.55, 7.44, 4], [1.55, 5.58, 4.48], [1.55, 3.1, 4.54], [1.55, 1.24, 4.48], [1.55, 0.5, 4.98], [1.55, 0, 5.4]]
    legBPos   =[[1.55, 8.37, -4.82], [1.55, 5.77, -4.51], [1.55, 3.4, -5.75], [1.55, 1.36, -5.87], [1.55, 0.62, -5.63], [1.55, 0, -5.13]]
    spineBPos =[[0.0, 9.92, -3.5],[0.0, 9.92, 3.5]]
    tailBPos  =[[0, 9.92, -6],[0.0, 9.92, -12.5]]
    neckBPos  =[[0.0, 9.92, 6.34],[0, 13, 10.17]]
    headBPos  =[[0, 14, 13]]
    hipBPos   =[[0.0, 9.92, -5.13]]
    jawBPos	  =[[0, 13, 13.2],[0, 12, 15.8]]
    eyeBPos   =[[0.96, 13.5, 14.5]]
    return armBPos,legBPos,spineBPos,tailBPos,neckBPos,headBPos,hipBPos,jawBPos,eyeBPos

#Init Cat Defult Name
#
def sdd_returnCatJntInitName():
    armBName   =['shoulder_skin', 'frontUpLeg_skin', 'frontKnee_skin', 'frontFoot_skin', 'frontToe_skin', 'frontToeEnd_skin']
    legBName   =['rootLeg_skin', 'backUpLeg_skin', 'backKnee_skin', 'backFoot_skin', 'backToe_skin', 'backToeEnd_skin']
    spineBName =['spine','spine_end']
    tailBName  =['tail','tail_end']
    neckBName  =['neck','neck_end']
    headBName  =['headTop']
    hipBName   =['hip']
    jawBName   =['jaw','jaw_end']
    eyeBName   =['eye']
    return armBName,legBName,spineBName,tailBName,neckBName,headBName,hipBName,jawBName,eyeBName

#Init Cat Defult Postion
#
def sdd_returnCatJntInitPos():
    armBPos   =[[0.77, 5.27, 2.0], [0.77, 3.72, 2.71], [0.77, 2.17, 2.25], [0.77, 0.62, 2.34], [0.77, 0.25, 2.5], [0.77, 0, 2.71]]
    legBPos   =[[0.77, 4.65, -3.41], [0.77, 3.72, -3.1], [0.77, 2, -3.1], [0.77, 0.68, -3.93], [0.77, 0.31, -3.81], [0.77, 0, -3.56]]
    spineBPos =[[0.0, 5, -2.5],[0.0, 5, 2.5]]
    tailBPos  =[[0, 5, -4],[0.0, 5, -7]]
    neckBPos  =[[0.0, 5, 3.2],[0, 5.4, 4.1]]
    headBPos  =[[0, 6, 5.5]]
    hipBPos   =[[0.0, 5, -3.5]]
    jawBPos	  =[[0, 5.25, 4.9],[0, 4.6, 5.7]]
    eyeBPos   =[[0.47, 5.5, 5.5]]
    return armBPos,legBPos,spineBPos,tailBPos,neckBPos,headBPos,hipBPos,jawBPos,eyeBPos


#
#
def sdd_importHumanMonsterJntTemptale(typ):
    #Get Defult Name And Postion
    armBName,thumbBName,indexBName,middleBName,ringBName,pinkyBName,legBName,footBName,spineBName,neckBName,hipBName,headBName,tailBName,jawBName,eyeBName=sdd_returnHumanMonsterJntInitName(typ)
    armBPos,thumbBPos,indexBPos,middleBPos,ringBPos,pinkyBPos,legBPos,footBPos,spineBPos,neckBPos,hipBPos,headBPos,tailBPos,jawBPos,eyeBPos=sdd_returnHumanMonsterJntInitPos(typ)
    lPre,rPre,suf,rad,ext =sdd_returnJntPrefix()
    neckSeg,spineSeg,tailSeg,handNum,legNum=sdd_getWindowControlParameter()
    #Left Hand Joint
    lArmName   =sdd_CreateJointByList(armBPos,armBName,lPre,suf,rad)

    #Left Finger Joint
    lThumbName =sdd_CreateJointByList(thumbBPos,thumbBName,lPre,suf,rad)
    lIndexName =sdd_CreateJointByList(indexBPos,indexBName,lPre,suf,rad)
    lMiddleName=sdd_CreateJointByList(middleBPos,middleBName,lPre,suf,rad)
    lRingName  =sdd_CreateJointByList(ringBPos,ringBName,lPre,suf,rad)
    lPinkyName =sdd_CreateJointByList(pinkyBPos,pinkyBName,lPre,suf,rad)

    lThumbName =sdd_splitJoint(lThumbName[0],lThumbName[1],2)
    lIndexName =sdd_splitJoint(lIndexName[0],lIndexName[1],2)
    lMiddleName=sdd_splitJoint(lMiddleName[0],lMiddleName[1],2)
    lRingName  =sdd_splitJoint(lRingName[0],lRingName[1],2)
    lPinkyName =sdd_splitJoint(lPinkyName[0],lPinkyName[1],2)

    lThumbName =sdd_RenameByOrder(lThumbName,thumbBName[0],lPre,suf)
    lIndexName =sdd_RenameByOrder(lIndexName,indexBName[0],lPre,suf)
    lMiddleName=sdd_RenameByOrder(lMiddleName,middleBName[0],lPre,suf)
    lRingName  =sdd_RenameByOrder(lRingName,ringBName[0],lPre,suf)
    lPinkyName =sdd_RenameByOrder(lPinkyName,pinkyBName[0],lPre,suf)


    #Spine Joint
    spineName  =sdd_CreateJointByList(spineBPos,spineBName,'',suf,rad)
    spineName  =sdd_splitJoint(spineName[0],spineName[1],spineSeg)
    spineName  =sdd_RenameByOrder(spineName,spineBName[0],'',suf)

    #Neck Joint
    neckName   =sdd_CreateJointByList(neckBPos,neckBName,'',suf,rad)
    neckName   =sdd_splitJoint(neckName[0],neckName[1],neckSeg)
    neckName   =sdd_RenameByOrder(neckName,neckBName[0],'',suf)

    #Head Joint
    headName   =sdd_CreateJointByList(headBPos,headBName,'',suf,rad)

    #Jaw Joint
    jawName    =sdd_CreateJointByList(jawBPos,jawBName,'',suf,rad)

    #Eye Joint
    eyeName    =sdd_CreateJointByList(eyeBPos,eyeBName,lPre,suf,rad)

    #Hip Joint
    hipName    =duplicate(spineName[0],po=1,n=hipBName[0]+suf)
    setAttr(hipName[0]+'.radi',rad*2)

    #Tail Joint
    if(tailSeg!=0):
        tailName   =sdd_CreateJointByList(tailBPos,tailBName,lPre,suf,rad)
        tailName   =sdd_splitJoint(tailName[0],tailName[1],tailSeg)
        tailName   =sdd_RenameByOrder(tailName,tailBName[0],'',suf)
    #Leg Joint
    lLegName   =sdd_CreateJointByList(legBPos,legBName,lPre,suf,rad)

    #Foot Joint
    lFootName  =sdd_CreateJointByList(footBPos,footBName,lPre,suf,rad)

    #All Circle
    allCnt=circle(nr=[0,1,0],r=8,ch=0,n='all_anim')[0]

    #Hierarchical Arrangement
    parent(headName[0],jawName[0],eyeName[0],neckName[-1])
    parent(lFootName[0],lFootName[1],lLegName[-1])
    parent(lArmName[0],hipName[0],lLegName[0],spineName[0],neckName[0],allCnt)
    if(tailSeg!=0):
        parent(tailName[0],allCnt)
    parent(lThumbName[0],lIndexName[0],lMiddleName[0],lRingName[0],lPinkyName[0],lArmName[-1])



    #Lock Attr
    attrList=['.sx','.sy','.sz']
    objList =lThumbName+lIndexName+lMiddleName+lRingName+lPinkyName
    objList =objList+lArmName+spineName+neckName+headName+hipName+lLegName+lFootName+jawName+eyeName

    if(tailSeg!=0):
        objList =objList+tailName

    sdd_lockAttrByList(objList,attrList)

    attrList=['.ty','.tz']
    objList =lThumbName[1:]+lIndexName[1:]+lMiddleName[1:]+lRingName[1:]+lPinkyName[1:]
    objList =objList+lArmName[2:]+spineName[1:]+neckName[1:]+[jawName[1]]

    if(typ=='Human'):
        objList =objList+lLegName[1:]
    else:
        objList =objList+lLegName[2:]

    if(tailSeg!=0):
        objList =objList+tailName[1:]

    sdd_lockAttrByList(objList,attrList)

    attrList=['.rx','.ry']

    if(typ=='Human'):
        objList =[lArmName[2],lLegName[1]]+lFootName
    else:
        objList =[lArmName[2]]+lLegName[2:3]+lFootName

    sdd_lockAttrByList(objList,attrList)

    attrList=['.tz']
    objList =lFootName
    sdd_lockAttrByList(objList,attrList)

    #Mirrot Left Joint To Right
    mirrorJoint(lArmName[0],myz=1,mb=1,sr=[lPre,rPre])
    mirrorJoint(lLegName[0],myz=1,mb=1,sr=[lPre,rPre])
    mirrorJoint(eyeName[0],myz=1,mb=1,sr=[lPre,rPre])

    #Expression For Mirror
    attrList=['.tx','.ty','.tz','.rx','.ry','.rz']
    opList  =['-','-','-','','','']
    objList =lThumbName+lIndexName+lMiddleName+lRingName+lPinkyName+lArmName[1:]+lLegName[1:]+lFootName
    sdd_createMirrorExpression(objList,lPre,rPre,attrList,opList)

    opList  =['-','','','','','']
    objList =[lArmName[0],lLegName[0]]
    sdd_createMirrorExpression(objList,lPre,rPre,attrList,opList)

    opList  =['','','-','','','']
    objList =[eyeName[0]]
    sdd_createMirrorExpression(objList,lPre,rPre,attrList,opList)
    #duplicate
    #multiple

    for i in range(handNum):
        multi_suf=ext+'%s'%(i+1)+suf
        #Left Hand Joint
        lArmName   =sdd_CreateJointByList(armBPos,armBName,lPre,multi_suf,rad)

        #Left Finger Joint
        lThumbName =sdd_CreateJointByList(thumbBPos,thumbBName,lPre,multi_suf,rad)
        lIndexName =sdd_CreateJointByList(indexBPos,indexBName,lPre,multi_suf,rad)
        lMiddleName=sdd_CreateJointByList(middleBPos,middleBName,lPre,multi_suf,rad)
        lRingName  =sdd_CreateJointByList(ringBPos,ringBName,lPre,multi_suf,rad)
        lPinkyName =sdd_CreateJointByList(pinkyBPos,pinkyBName,lPre,multi_suf,rad)

        lThumbName =sdd_splitJoint(lThumbName[0],lThumbName[1],2)
        lIndexName =sdd_splitJoint(lIndexName[0],lIndexName[1],2)
        lMiddleName=sdd_splitJoint(lMiddleName[0],lMiddleName[1],2)
        lRingName  =sdd_splitJoint(lRingName[0],lRingName[1],2)
        lPinkyName =sdd_splitJoint(lPinkyName[0],lPinkyName[1],2)

        lThumbName =sdd_RenameByOrder(lThumbName,thumbBName[0],lPre,multi_suf)
        lIndexName =sdd_RenameByOrder(lIndexName,indexBName[0],lPre,multi_suf)
        lMiddleName=sdd_RenameByOrder(lMiddleName,middleBName[0],lPre,multi_suf)
        lRingName  =sdd_RenameByOrder(lRingName,ringBName[0],lPre,multi_suf)
        lPinkyName =sdd_RenameByOrder(lPinkyName,pinkyBName[0],lPre,multi_suf)

        #Hierarchical Arrangement
        parent(lArmName[0],allCnt)
        parent(lThumbName[0],lIndexName[0],lMiddleName[0],lRingName[0],lPinkyName[0],lArmName[-1])

        #Lock Attr
        attrList=['.sx','.sy','.sz']
        objList =lThumbName+lIndexName+lMiddleName+lRingName+lPinkyName+lArmName
        sdd_lockAttrByList(objList,attrList)

        attrList=['.ty','.tz']
        objList =lThumbName[1:]+lIndexName[1:]+lMiddleName[1:]+lRingName[1:]+lPinkyName[1:]+lArmName[2:]
        sdd_lockAttrByList(objList,attrList)

        attrList=['.rx','.ry']
        objList =[lArmName[2]]
        sdd_lockAttrByList(objList,attrList)
        #Mirrot Left Joint To Right
        mirrorJoint(lArmName[0],myz=1,mb=1,sr=[lPre,rPre])

        #Expression For Mirror
        attrList=['.tx','.ty','.tz','.rx','.ry','.rz']
        opList  =['-','-','-','','','']
        objList =lThumbName+lIndexName+lMiddleName+lRingName+lPinkyName+lArmName[1:]
        sdd_createMirrorExpression(objList,lPre,rPre,attrList,opList)

        opList  =['-','','','','','']
        objList =[lArmName[0]]
        sdd_createMirrorExpression(objList,lPre,rPre,attrList,opList)

        #move
        move(0,0,-1*(i+1),lArmName[0],r=1)

    for i in range(legNum):
        multi_suf=ext+'%s'%(i+1)+suf
        #Leg Joint
        lLegName   =sdd_CreateJointByList(legBPos,legBName,lPre,multi_suf,rad)

        #Foot Joint
        lFootName  =sdd_CreateJointByList(footBPos,footBName,lPre,multi_suf,rad)

        #Hierarchical Arrangement
        parent(lFootName[0],lFootName[1],lLegName[-1])
        parent(lLegName[0],allCnt)
        #Lock Attr
        attrList=['.sx','.sy','.sz']
        objList =lLegName+lFootName
        sdd_lockAttrByList(objList,attrList)

        attrList=['.ty','.tz']

        if(typ=='Human'):
            objList =lLegName[1:]
        else:
            objList =lLegName[2:]

        sdd_lockAttrByList(objList,attrList)

        attrList=['.rx','.ry']
        if(typ=='Human'):
            objList =[lLegName[1]]+lFootName
        else:
            objList =lLegName[2:3]+lFootName

        sdd_lockAttrByList(objList,attrList)

        attrList=['.tz']
        objList =lFootName
        sdd_lockAttrByList(objList,attrList)

        #Mirrot Left Joint To Right
        mirrorJoint(lLegName[0],myz=1,mb=1,sr=[lPre,rPre])

        #Expression For Mirror
        attrList=['.tx','.ty','.tz','.rx','.ry','.rz']
        opList  =['-','-','-','','','']
        objList =lLegName[1:]+lFootName
        sdd_createMirrorExpression(objList,lPre,rPre,attrList,opList)

        opList  =['-','','','','','']
        objList =[lLegName[0]]
        sdd_createMirrorExpression(objList,lPre,rPre,attrList,opList)

        #move
        move((i+1),0,0,lLegName[0],r=1)



def sdd_importHorseCatJntTemptale(typ):

    #Get Defult Name And Postion exec('sdd_returnHorseJntInitName()')
    armBName,legBName,spineBName,tailBName,neckBName,headBName,hipBName,jawBName,eyeBName=globals()['sdd_return'+typ+'JntInitName']()
    armBPos,legBPos,spineBPos,tailBPos,neckBPos,headBPos,hipBPos,jawBPos,eyeBPos=globals()['sdd_return'+typ+'JntInitPos']()
    lPre,rPre,suf,rad,ext =sdd_returnJntPrefix()
    neckSeg,spineSeg,tailSeg,handNum,legNum=sdd_getWindowControlParameter()
    #Get Windows Control Parameter
    neckSeg   =intFieldGrp('frNeckSegIG',q=1,v=1)[0]
    spineSeg  =intFieldGrp('frSpineSegIG',q=1,v=1)[0]
    tailSeg   =intFieldGrp('frTailSegIG',q=1,v=1)[0]
    handNum   =intFieldGrp('frHandNumIG',q=1,v=1)[0]
    legNum    =intFieldGrp('frLegNumIG',q=1,v=1)[0]


    #Left Hand Joint
    lArmName   =sdd_CreateJointByList(armBPos,armBName,lPre,suf,rad)

    #Left Leg Joint
    lLegName   =sdd_CreateJointByList(legBPos,legBName,lPre,suf,rad)

    #Spine Joint
    spineName  =sdd_CreateJointByList(spineBPos,spineBName,'',suf,rad)
    spineName  =sdd_splitJoint(spineName[0],spineName[1],spineSeg)
    spineName  =sdd_RenameByOrder(spineName,spineBName[0],'',suf)

    #Tail Joint
    tailName   =sdd_CreateJointByList(tailBPos,tailBName,'',suf,rad)
    tailName   =sdd_splitJoint(tailName[0],tailName[1],tailSeg)
    tailName   =sdd_RenameByOrder(tailName,tailBName[0],'',suf)

    #Neck Joint
    neckName   =sdd_CreateJointByList(neckBPos,neckBName,'',suf,rad)
    neckName   =sdd_splitJoint(neckName[0],neckName[1],neckSeg)
    neckName   =sdd_RenameByOrder(neckName,neckBName[0],'',suf)

    #Head Joint
    headName   =sdd_CreateJointByList(headBPos,headBName,'',suf,rad)

    #Jaw Joint
    jawName    =sdd_CreateJointByList(jawBPos,jawBName,'',suf,rad)

    #Eye Joint
    eyeName    =sdd_CreateJointByList(eyeBPos,eyeBName,lPre,suf,rad)

    #Hip Joint
    hipName    =duplicate(spineName[0],po=1,n=hipBName[0]+suf)
    xform(hipName,ws=1,t=hipBPos[0])
    setAttr(hipName[0]+'.radi',rad*2)

    #All Circle
    allCnt=circle(nr=[0,1,0],r=8,ch=0,n='all_anim')[0]

    #Hierarchical Arrangement
    parent(headName[0],jawName[0],eyeName[0],neckName[-1])
    parent(lArmName[0],hipName[0],lLegName[0],spineName[0],neckName[0],tailName[0],allCnt)

    #Lock Attr
    attrList=['.sx','.sy','.sz']
    objList =lArmName+spineName+neckName+headName+hipName+lLegName+tailName+eyeName
    sdd_lockAttrByList(objList,attrList)

    attrList=['.ty','.tz']
    if typ=='Horse':
        objList =lArmName[3:4]
    else:
        objList =lArmName[2:3]
    objList =objList+spineName[1:]+neckName[1:]+lLegName[2:3]+tailName[1:]+[jawName[1]]
    sdd_lockAttrByList(objList,attrList)

    attrList=['.rx','.ry']
    if typ=='Horse':
        objList =[lArmName[3],lLegName[2]]
    else:
        objList =[lArmName[2],lLegName[2]]
    sdd_lockAttrByList(objList,attrList)

    #Mirrot Left Joint To Right
    mirrorJoint(lArmName[0],myz=1,mb=1,sr=[lPre,rPre])
    mirrorJoint(lLegName[0],myz=1,mb=1,sr=[lPre,rPre])
    mirrorJoint(eyeName[0],myz=1,mb=1,sr=[lPre,rPre])

    #Expression For Mirror
    attrList=['.tx','.ty','.tz','.rx','.ry','.rz']
    opList  =['-','-','-','','','']
    objList =lArmName[1:]+lLegName[1:]
    sdd_createMirrorExpression(objList,lPre,rPre,attrList,opList)

    opList  =['-','','','','','']
    objList =[lArmName[0],lLegName[0]]
    sdd_createMirrorExpression(objList,lPre,rPre,attrList,opList)

    opList  =['','','-','','','']
    objList =[eyeName[0]]
    sdd_createMirrorExpression(objList,lPre,rPre,attrList,opList)

    #duplicate
    #multiple

    for i in range(handNum):
        multi_suf=ext+'%s'%(i+1)+suf
        #Left Hand Joint
        lArmName   =sdd_CreateJointByList(armBPos,armBName,lPre,multi_suf,rad)

        #Hierarchical Arrangement
        parent(lArmName[0],allCnt)

        #Lock Attr
        attrList=['.sx','.sy','.sz']
        objList =lArmName
        sdd_lockAttrByList(objList,attrList)

        attrList=['.ty','.tz']
        if typ=='Horse':
            objList =lArmName[3:4]
        else:
            objList =lArmName[2:3]
        sdd_lockAttrByList(objList,attrList)

        attrList=['.rx','.ry']
        if typ=='Horse':
            objList =[lArmName[3]]
        else:
            objList =[lArmName[2]]
        sdd_lockAttrByList(objList,attrList)
        #Mirrot Left Joint To Right
        mirrorJoint(lArmName[0],myz=1,mb=1,sr=[lPre,rPre])

        #Expression For Mirror
        attrList=['.tx','.ty','.tz','.rx','.ry','.rz']
        opList  =['-','-','-','','','']
        objList =lArmName[1:]
        sdd_createMirrorExpression(objList,lPre,rPre,attrList,opList)

        opList  =['-','','','','','']
        objList =[lArmName[0]]
        sdd_createMirrorExpression(objList,lPre,rPre,attrList,opList)

        #move
        move(1*(i+1),0,0,lArmName[0],r=1)

    for i in range(legNum):
        multi_suf=ext+'%s'%(i+1)+suf
        #Leg Joint
        lLegName   =sdd_CreateJointByList(legBPos,legBName,lPre,multi_suf,rad)

        #Hierarchical Arrangement
        parent(lLegName[0],allCnt)

        #Lock Attr
        attrList=['.sx','.sy','.sz']
        objList =lLegName
        sdd_lockAttrByList(objList,attrList)

        attrList=['.ty','.tz']
        objList =lLegName[2:3]
        sdd_lockAttrByList(objList,attrList)

        attrList=['.rx','.ry']
        objList =[lLegName[2]]
        sdd_lockAttrByList(objList,attrList)


        #Mirrot Left Joint To Right
        mirrorJoint(lLegName[0],myz=1,mb=1,sr=[lPre,rPre])

        #Expression For Mirror
        attrList=['.tx','.ty','.tz','.rx','.ry','.rz']
        opList  =['-','-','-','','','']
        objList =lLegName[1:]
        sdd_createMirrorExpression(objList,lPre,rPre,attrList,opList)

        opList  =['-','','','','','']
        objList =[lLegName[0]]
        sdd_createMirrorExpression(objList,lPre,rPre,attrList,opList)

        #move
        move((i+1),0,0,lLegName[0],r=1)


#Function To Create Joint By Postion And Name List
#
def sdd_CreateJointByList(posList,nameList,pre,suf,rad):
    select(cl=1)
    retList=[]

    for i in range(len(posList)):
        name=joint(p=[posList[i][0],posList[i][1],posList[i][2]],n=pre+nameList[i]+suf,rad=rad)
        retList.append(name)

    for i in retList:
        if(i!=retList[-1]):
            joint(i,e=1,oj='xyz',sao='zup')
        else:
            setAttr(i+'.jo',0,0,0,typ='float3')

    return retList

#Function To Split Joint
#
def sdd_splitJoint(parentJnt,childJnt,segment):
    radiusJoint = getAttr(parentJnt+".radius")
    childTrans = getAttr(childJnt+".tx")
    trans = childTrans / (segment+1)
    ret=[]
    for i in range(segment):
        ret.append(insertJoint(parentJnt))
    for i in ret:
        setAttr(i+".tx",trans)
        setAttr(i+".radius",radiusJoint)
    setAttr(childJnt+".tx",trans)
    ret.reverse()
    ret=[parentJnt]+ret+[childJnt]
    return ret

#Function To Change Name By Order
#
def sdd_RenameByOrder(objList,baseName,pre,suf):
    for i in range(len(objList)):
        objList[i]=rename(objList[i],pre+baseName+'_%s'%(i+1)+suf)
    return objList

#Function To Lock Attr
#
def sdd_lockAttrByList(objList,attrList):
    for a in objList:
        for l in attrList:
            setAttr(a+l,l=1)

def sdd_keableAttrByList(jntList,attrList):
    for a in jntList:
        for l in attrList:
            setAttr(a+l,cb=0,k=0)

#Function To Return The Mirror Of Expression String
#
def sdd_createMirrorExpression(baseList,lPre,rPre,attrList,opList):
    for lObj in baseList:
        for a in range(len(attrList)):
            if not(getAttr(lObj+attrList[a],l=1)):
                mirExpStr=''
                attr=attrList[a]
                rObj=rPre+lObj[len(lPre):]
                mirExpStr=mirExpStr+rObj+attr+'='+opList[a]+lObj+attr+';\n'
                expression(s=mirExpStr,n='mirror_JntTemp_exp')





if __name__=='__main__':
    sdd_FastRigWin()