#====================================================================================================
#YJ_NameTool
#
#Designer:YangJie
#Email:691633493@
#Version:1.0
#============================================================================================
from maya.cmds import*
from YJ_NameTool_UI import*
import re
import maya.mel as mm

def YJ_Name():
    NewName=textField('yj_name_tx',q=1,tx=1)
    if NewName!='':
        NewNameR=NewName.replace(' ','_')
        if '@' not in  NewNameR:
            NewNameR=NewNameR+"@"
        Sel_list=ls(sl=1,l=1)
        Sel_Nub=len(Sel_list)
        NewName_Nub_of=re.sub("@","0",NewNameR)
        NewName_Nub_Me=re.findall('0',NewName_Nub_of)

        if len(NewName_Nub_Me)==0:
            NewName_Nub_Me.append('0')
            NewName_Nub_New=NewName_Nub_Me.remove(NewName_Nub_Me[-1])
        else:
            NewName_Nub_Me.remove(NewName_Nub_Me[-1])
            NewName_Nub_New=NewName_Nub_Me
        NewName_Tru_Nub=''
        for i in NewName_Nub_New:
            NewName_Tru_Nub=NewName_Tru_Nub+i
        NewName_Nub_New2=NewName_Tru_Nub+'0'
        NewName_List=[]
        for j in range(0,Sel_Nub):
            NewNameBaseNub=j+1
            NewNameBaseNub_Str=str(NewNameBaseNub)
            NewNameBaseNub_Fin=NewName_Tru_Nub+NewNameBaseNub_Str
            NewNameNubed=re.sub(NewName_Nub_New2,NewNameBaseNub_Fin,NewName_Nub_of)
            NewName_List.append(NewNameNubed)

        k=0
        while k<len(Sel_list):
            Name_Sel_Ls=Sel_list
            rename (Name_Sel_Ls[k],NewName_List[k])
            Sel_list=ls(sl=1,l=1)
            k=k+1


#Add Suffix
def YJ_Add_Suffix(in_suf,in_List,judge):
    if judge == 0:
        Suf = textField('yj_Suffix_tx',q=1,tx=1)
        NameList=ls(sl=1,l=1)
    elif judge == 1:
        Suf = in_suf
        NameList = in_List

    ListNub=len(NameList)

    OldNameList = getOldNameList(NameList)

    SufNameList = getSufPathList(OldNameList,Suf)

    CurNameList = NameList
    for i in range(0,ListNub):
        rename (CurNameList[i],SufNameList[i])
        CurPath = getCur_Path(CurNameList[i],SufNameList[i])
        CurNameList =  ResetPathList(CurNameList[i],CurPath,CurNameList)




##get curName
def getCurName(InPatnName):
    InPatn_List=InPatnName.split("|")
    CurName =InPatn_List[-1]
    return  CurName

#get CurPath
def getCur_Path(InPatnName,Curname):
    InPatn_List = InPatnName.split("|")
    InPatn_List.remove(InPatn_List[-1])
    InPatn_List.append(Curname)
    CurPath_list = InPatn_List
    CurPath = ''
    for i in CurPath_list:
        CurPath=CurPath+'|'+i
    return  CurPath


#get SufPath
def getSufPathList(inNameList,suf):
    suf_List=[]
    for i in inNameList:
        suf_i = i + suf
        suf_List.append(suf_i)
    return  suf_List

#get OldName inNamePathList=ls(sl=1,l=1)
def getOldNameList(inNamePathList):
    OldName_List=[]
    for i in inNamePathList:
        i_Name = getCurName(i)
        OldName_List.append(i_Name)
    return  OldName_List

#reset pathlist
def ResetPathList(orgPath,rePath,Path_org_List):
    out_path_list=[]
    for i in Path_org_List:
        try:
            j = i.replace(orgPath,rePath)
        except:
            j=i
        out_path_list.append(j)
    return out_path_list


#Add Prefix
def YJ_Add_Prefix():
    Pre=textField('yj_Pre_tx',q=1,tx=1)
    NameList=ls(sl=1,l=1)
    ListNub=len(NameList)

    OldNameList = getOldNameList(NameList)

    PreNameList = getPrePathList(OldNameList,Pre)

    CurNameList = NameList
    for i in range(0,ListNub):
        rename (CurNameList[i],PreNameList[i])
        CurPath = getCur_Path(CurNameList[i],PreNameList[i])
        CurNameList =  ResetPathList(CurNameList[i],CurPath,CurNameList)


#getPrePathList
def getPrePathList(inNameList,Pre):
    Pre_List=[]
    for i in inNameList:
        Pre_i = Pre + i
        Pre_List.append(Pre_i)
    return  Pre_List