2014-09-25 128 views
0

我正在製作一個腳本來爲程序maya在python中進行命名約定。 我將使用它來命名由我的腳本創建的所有對象。如何用變量替換列表中的單個字符串?

例如,讓我們走左膝關節。該腳本會將 (「bind」,「shoulder」,「left」,「joint」)這樣的內容傳遞給另一個模塊 (prefix,name,side,obj_type)。然後,此輸入將通過用戶字典運行,以檢查現有值並更改爲新值,如果未找到任何值,則返回原始值。例如,「聯合」會變成「jnt」。

用戶將輸入類似(prefix_,名_,SIDE_,OBJ_TYPE,01)

我需要它,這樣它會檢查是否在用戶輸入任何變量名存在。例如,如果它在用戶輸入中找到任何變量名稱(例如「前綴」),則將該變量前綴所包含的內容替換爲它所在的索引。此外,字符串中未找到的任何內容都將被單獨留下,如「01」,它將簡單地添加到每個名稱上。

例如,按照上面將返回該「bn_shoulder_L_jnt01」

而且,如果事情是大寫的,就像第一個字母,或者所有的字母,我希望它自動大寫把信交給了。這就是爲什麼鍵入SIDE_會將「l」變成「L」的原因。

我希望這樣做盡可能靈活,但我目前的麻煩是如果它找到值,將其替換爲現有字符串中的變量。我嘗試過考慮一些事情,但沒有提出太多。這裏是我的代碼:

另外我目前有用戶輸入傳遞給init中的類。它會在整個模塊中工作和使用嗎?我仍然不能100%確定如何使用init,但是我希望當用戶輸入命名約定時,只要需要,它就可以在內存中使用。

編輯代碼:

from string import Template 

class Name: 

    def __init__(self, user_conv): 
     self.user_conv = user_conv 


    def user_dict(self, word): 
     """Matches given word with a dictionary, and returns converted abbreviated word. 

     Keyword Arguments: 
     string -- given string to be converted 
        example: joint > jnt 
     """ 

     # prefixes 
     user_library = { 
     'bind' : 'bn', 
     'driver' : 'drv', 

     # side 
     'back' : 'b', 
     'down' : 'd', 
     'front' : 'f', 
     'left' : 'l', 
     'right' : 'r', 
     'up' : 'u', 

     # obj_type 
     'cluster' : 'clstr', 
     'control' : 'ctrl', 
     'curve' : 'crv', 
     'effector' : 'efctr', 
     'group' : 'grp', 
     'ikHandle' : 'ikH', 
     'joint' : 'jnt', 
     'locator' : 'loc', 
     'nurbs' : 'geo', 
     'orientConstraint' : 'orCnstr', 
     'parentConstraint' : 'prntCnstr', 
     'pointConstraint' : 'ptCnstr', 
     'polyMesh' : 'geo', 

     # utilities 
     'addDoubleLinear' : 'adl', 
     'blendColors' : 'blndClr', 
     'BlendTwoAttr' : 'b2a', 
     'chooser' : 'chsr', 
     'clamp' : 'clmp', 
     'condition' : 'cn', 
     'curveInfo' : 'crvI', 
     'diffuse' : 'diffuse', 
     'displacement' : 'displ', 
     'multiplyDivide' : 'mdv', 
     'normal' : 'normal', 
     'place2d' : 'p2d', 
     'plusMinusAverage' : 'pma', 
     'reverse' : 'rv', 
     'setRange' : 'sr', 
     'shader' : 'shdr', 
     'shadingGroup' : 'SG', 
     'specular' : 'spec', 
     'transparency' : 'trans', 

     # sequential bones 
     'arm' : 'arm', 
     'fingser' : 'finger', 
     'index' : 'index', 
     'leg' : 'leg', 
     'limb' : 'limb', 
     'middle' : 'middle', 
     'pinky' : 'pinky', 
     'ring' : 'ring', 
     'spine' : 'spine', 
     'toe' : 'toe', 
     'thumb' : 'thumb', 

     # 
     'ankle' : 'ankle', 
     'ball' : 'ball', 
     'breast' : 'breast', 
     'chest' : 'chest', 
     'clavicle' : ' clavicle', 
     'elbow' : 'elbow', 
     'end' : 'e', 
     'head' : 'head', 
     'hair' : 'hair', 
     'knee' : 'knee', 
     'neck' : 'neck', 
     'pelvis' : 'pelvis', 
     'root' : 'root', 
     'shoulder' : 'shoulder', 
     'tail' : 'tail', 
     'thigh' : 'thigh', 
     'wrist' : 'wrist' 
     } 

     if word in user_library: 
      abbrevWord = user_library[word] 
     else: 
      abbrevWord = word 

     return [abbrevWord] 

    def convert(self, prefix, name, side, obj_type): 
     """Converts given information about object into user specified naming convention. 

     Keyword Arguments: 
     prefix -- what is prefixed before the name 
     name -- name of the object or node 
     side -- what side the object is on, example 'left' or 'right' 
     obj_type -- the type of the object, example 'joint' or 'multiplyDivide' 
     """ 
     self.prefix = self.user_dict(prefix) 
     self.name = self.user_dict(name) 
     self.side = self.user_dict(side) 
     self.obj_type = self.user_dict(obj_type) 

     print '%s, %s, %s, %s' %(prefix, name, side, obj_type) 

     self.new_string = Template (self.user_conv.lower()) 
     self.subs = {'prefix': prefix, 'name': name, 'side': side, 'obj_type': obj_type} 
     self.new_string.substitute(**self.subs) 

     print new_string 

     return new_string 

test code: 

    # test file to test naming convention to see if its functioning properly 

    import neo_name 
    reload(neo_name) 

def ui_test(): 
    """types user can input 
    #prefix 
    #name 
    #side 
    #type 
    constants (such as 01, present in ALL objects/nodes/etc.) 
    """ 
    user_conv = '${prefix}_${name}_${side}_${obj_type}${01}' 

    name = neo_name.Name(user_conv) 
    name.convert('bind', 'shoulder', 'left', 'joint') 

ui_test() 

現在收到這個錯誤,不知道該怎麼利用它: 綁定,肩,左,聯合 回溯(最近通話最後一個): 文件「C:\用戶\ Gregory \ Documents \ Gregory的文件夾\ Artwork \ 3D Scripts_MyScripts \ neoAutoRig \ scripts \ test_neo_name.py「,第19行,在 ui_test() 文件」C:\ Users \ Gregory \ Documents \ Gregory's Folder \ Artwork \ 3D Scripts_MyScripts \ neoAutoRig \ scripts \ test_neo_name.py「,第17行,在ui_test中 name.convert('bind','shoulder','left','joint')

文件「C:\ Users \ Gregory \ Documents \ Gregory's Folder \ Artwork \ 3D Scripts_MyScripts \ neoAutoRig \ scripts \ neo_name.py」,in line 133,in convert self.new_string.substitute(prefix = prefix,name =文件「C:\ Program Files \ Autodesk \ Autodesk \ Maya2014 \ bin \ python27.zip \ string.py「,第169行,轉換爲 文件」C:\ Program Files \ Autodesk \ Maya2014 \ bin \ python27.zip \ string「。PY」,線路146,在_invalid ValueError異常:無效的佔位符串:1號線,列38

+0

什麼是你真實的問題?目前還不清楚你有什麼問題。 – 2014-09-25 20:27:24

回答

0

這也是蟒string.Template類很好的應用:

從字符串導入模板

t = Template ("${prefix}_${side}_${limb}") 
t.substitute(prefix ='character', side='R', limb='hand') 
>>> 'character_R_hand' 

你可以得到通過將分隔符視爲有條件的(默認爲「'),執行大寫規則等等e.substitute採取任何標誌(如上面的例子)或字典:

t = Template ("${prefix}_${side}_${limb}") 
subs = {'prefix':'dict', 'side':'L', 'limb':'bicep'} 
t.substitute(**subs) 
>>> 'dict_L_hand' 
+0

謝謝,這正是我一直在尋找的! – 2014-09-26 05:55:11

+0

儘管現在我收到了一個值錯誤,但我會發布上述詳細信息並編輯代碼。 – 2014-09-26 06:50:32

+0

可能是您提供的密鑰與佔位符中的字符串不匹配的拼寫錯誤。如果您使用'safe_substitute',而不是您將得到的錯誤:相反,您會看到未在輸出中填寫的佔位符\ – theodox 2014-09-26 16:31:22

0

你得讓你的字符串映射到實際的命令

d = {'bind':'bn', 'left':'L', 'joint':'jnt'} 
l = ['bind', 'shoulder', 'left', 'joint', '01'] 

然後使用字典get這需要第二個參數,如果沒有找到密鑰,你可以通過原始的參數使用,然後用之間'_'聯合起來的一切。

'_'.join(d.get(i,i) for i in l) 

輸出

'bn_shoulder_L_jnt_01' 

在你的情況,這將是

'_'.join(userLibrary.get(word,word) for word in prefixes)