2014-01-06 24 views
1

我已經爲一個工具編寫了一段代碼,當在maya中選擇一個關節時,當選擇了關節時,用戶按下界面上的按鈕它應該重新命名該按鈕的文本的關節的工具。該代碼在maya的腳本編輯器中編譯,並且工具UI顯示正確。然而,當你選擇一個關節,然後按jnt_L_toe按鈕(唯一應該當前工作的),聯合名稱不會被替換爲jnt_L_toe,我的問題是爲什麼?Python腳本將在maya中編譯但不執行任何操作

下面是代碼:

#Global variable contains all joints in model 
joints_list = maya.cmds.ls(type="joint") 

#Variable names 
Ltoe = "jnt_L_toe" 

# create the window 
wnd_name = maya.cmds.window(title="Rename-A-Joint", widthHeight=[300, 500]) 

# create the layout 
maya.cmds.rowColumnLayout(numberOfColumns = 2, rowSpacing=[(1,5), (2,5)], columnWidth=[(1,120),(2,120)]) 
maya.cmds.text(label="Please select a \n joint then one\n of the following\n buttons to rename it:", font = "boldLabelFont") 
maya.cmds.text(label="    \n         \n      ", font = "boldLabelFont") 

# create the controls 
maya.cmds.text(label="Legs", font = "boldLabelFont") 
maya.cmds.text(label="Hands", font = "boldLabelFont") 
maya.cmds.button(label="jnt_L_toe", command="renameJoint(Ltoe)") 
maya.cmds.button(label="jnt_L_thumb1", command="pass") 
maya.cmds.button(label="jnt_L_ball", command="pass") 
maya.cmds.button(label="jnt_L_thumb2", command="pass") 
maya.cmds.button(label="jnt_L_ankle", command="pass") 
maya.cmds.button(label="jnt_L_thumb3", command="pass") 
maya.cmds.button(label="jnt_L_knee", command="pass") 
maya.cmds.button(label="jnt_L_thumb4", command="pass") 
maya.cmds.button(label="jnt_L_thigh", command="pass") 
maya.cmds.button(label="jnt_L_thumb5", command="pass") 
maya.cmds.text(label="Arms", font = "boldLabelFont") 
maya.cmds.button(label="jnt_L_index1", command="pass") 
maya.cmds.button(label="jnt_L_clavicle", command="pass") 
maya.cmds.button(label="jnt_L_index2", command="pass") 
maya.cmds.button(label="jnt_L_shoulder", command="pass") 
maya.cmds.button(label="jnt_L_index3", command="pass") 
maya.cmds.button(label="jnt_L_elbow", command="pass") 
maya.cmds.button(label="jnt_L_index4", command="pass") 
maya.cmds.button(label="jnt_L_forearm", command="pass") 
maya.cmds.button(label="jnt_L_middle1", command="pass") 
maya.cmds.button(label="jnt_L_wrist", command="pass") 
maya.cmds.button(label="jnt_L_middle2", command="pass") 
maya.cmds.text(label="") 
maya.cmds.button(label="jnt_L_middle3", command="pass") 
maya.cmds.text(label="") 
maya.cmds.button(label="jnt_L_middle4", command="pass") 
maya.cmds.text(label="") 
maya.cmds.button(label="jnt_L_ring1", command="pass") 
maya.cmds.text(label="") 
maya.cmds.button(label="jnt_L_ring2", command="pass") 
maya.cmds.text(label="") 
maya.cmds.button(label="jnt_L_ring3", command="pass") 
maya.cmds.text(label="") 
maya.cmds.button(label="jnt_L_ring4", command="pass") 
maya.cmds.text(label="") 
maya.cmds.button(label="jnt_L_pinky1", command="pass") 
maya.cmds.text(label="") 
maya.cmds.button(label="jnt_L_pinky2", command="pass") 
maya.cmds.text(label="") 
maya.cmds.button(label="jnt_L_pinky3", command="pass") 
maya.cmds.text(label="") 
maya.cmds.button(label="jnt_L_pinky4", command="pass") 

# show the window 
maya.cmds.showWindow(wnd_name) 

#Function to change name of joint 
def renameJoint(name): 
    currentjoint = cmds.ls(type = "joint", selection=True) 
    for connect in joints_list: 
     if(connect == currentjoint): 
      cmds.rename(connect, 'name')` 

回答

1

有沒有錯,每個看到的代碼。那是你展示的代碼之外的問題。你不能真正採用python片段,而忽略導入語句,因爲這些是故事的核心內容。你也應該一般發佈報告的錯誤。

最可能的問題是您在函數中使用的不是名稱空間而是在您的身體中。看到你在主體使用maya.cmds這將表明您已經導入:

import maya.cmds 

在另一方面函數使用這表明CMDS:

import maya.cmds as cmd 

這是迄今爲止普遍慣例做兩者都沒有真正意義。不過很難說它是你真正想要的問題。

另一個錯誤可以發現:

def renameJoint(name): 
    currentjoint = cmds.ls(type = "joint", selection=True) 
    for connect in joints_list: 
     if(connect == currentjoint): 
      cmds.rename(connect, 'name')` 

這也許應該是:

def renameJoint(name): 
    currentjoint = cmds.ls(type = "joint", selection=True) 
    for connect in joints_list: 
     if(connect == currentjoint[0]): 
      cmds.rename(connect, name) 

有點神祕,但肯定。無論如何,我建議你改變你的代碼類似於:

import maya.cmds as cmds 

def renameJoint(name): 
    currentjoint = cmds.ls(type = "joint", selection=True) 
    if currentjoint[0] in joints_list: 
     cmds.rename(currentjoint[0], name) 

def multipleButtonGrp(title,lst): 
    cmds.text(label=title, font = "boldLabelFont") 
    cmds.text(label="") 
    for item in lst: 
     cmds.button(item, label=item, command="renameJoint('%s')"%item) 

joints_list = maya.cmds.ls(type="joint") 

wnd_name = cmds.window(title="Rename-A-Joint", widthHeight=[300, 500]) 

cmds.rowColumnLayout(numberOfColumns = 2) #add your options 
multipleButtonGrp("Hands", 
       ["jnt_L_toe", "jnt_L_thumb1", 
       "jnt_L_ball", "jnt_L_thumb2", 
       "jnt_L_ankle", "jnt_L_thumb3", 
       "jnt_L_knee", "jnt_L_thumb4", 
       "jnt_L_thigh", "jnt_L_thumb5"]) 

cmds.showWindow(wnd_name) 
+0

感謝您提供的解決方案。是的,因爲你推斷原始的舊的導入maya.cmds是在開始時使用的(我忘記了包含在代碼中)。然而,整個腳本仍然運行沒有毛刺保存renameJoint函數,其中行if(connect == currentjoint)是妨礙它的東西。您顯然已經爲UI提供了更加雄辯的解決方案。唯一的問題是空白按鈕不應該出現在腿和手臂後的列 - 所以我想檢查是必要的。無論如何再次感謝 - 我沒有15代表,否則我已經投了答案几次:) – Enchanter

+0

不,但你可以接受答案(複選標記),並比你有15分。除了現在你做:) – joojaa

相關問題