2016-03-17 110 views
-2
import maya.cmds as cmds 

def replace(char): 
    locators = cmds.ls(tr=True, s=True,type=('joint')) or [] 

    try: 
     for lp in locators: 
      if char in lp: 
       newFilename = lp.replace(char, "a") 
       cmds.rename(lp, newFilename) 
    except: 
     print "yes" 

charReplace="Ghoul" 
charReplace2="SHJnt" 
charReplace3="head" 
charReplace4="spine" 
charReplace5="arm" 
charReplace6="leg" 

replace(charReplace) 
replace(charReplace2) 
replace(charReplace3) 
replace(charReplace4) 
replace(charReplace5) 
replace(charReplace6) 

我試圖重命名Maya場景中的所有節點。無法重命名Maya鎖定節點

這個當前的代碼只重命名這些節點:食屍鬼和SHJnt。

我無法重命名頭節點。

我收到以下錯誤,當我嘗試將其重命名: // Error: line 1: Cannot rename a locked node. //

我怎樣才能提高我的代碼可以重命名鎖定的節點?

+1

你好,歡迎來到StackOverflow。請避免使用包含代碼的屏幕快照,將它複製/粘貼到問題中的方式會更快,並且方便我們執行。如果您提供截圖,沒有人會重新複製您的代碼。 試着清楚解釋你的問題是什麼,並且你想解決它。 另外,如果您有任何問題,請嘗試提供錯誤堆棧。 你可以檢查[如何提出一個好問題](http://stackoverflow.com/help/how-to-ask)。一旦您的問題得到編輯和改進,我將很樂意爲您提供幫助。 掛在那裏。 – DrHaze

回答

0
import maya.cmds as cmds 

######################################## 
# This function renames Maya nodes whose name contains oldString value 
# newString is an optionnal argument 
######################################## 
def renameNode(oldString, newString="a"): 
    # I don't really understand why you are passing these flags in the ls() function 
    # node_list = cmds.ls(tr=True, s=True,type=('joint')) 
    # I would recommand to only list nodes that might interest you using * 
    # * acts as a wildcard 
    node_list = cmds.ls("*" + oldString + "*", tr=True) 

    # Iterate through each node in the list 
    for node in node_list: 
     # Now unlock the node entirely 
     # You can also check the doc for the flag lockName 
     cmds.lockNode(node, lock=False) 
     # Rename the node 
     cmds.rename(node, node.replace(oldString, newString)) 


renameNode("Ghoul") 
renameNode("SHJnt") 
renameNode("head") 
renameNode("spine") 
renameNode("arm", "NewName") 
renameNode("leg", "OtherName")  
  • newString是optionnal參數,它的默認值是
  • 此代碼將解鎖節點,但重命名
  • 我不是重命名形狀,重命名自動變換後,不會把它們鎖重新命名形狀。
+0

非常感謝! – Lightman