2017-03-26 26 views
0

我寫這在瑪雅UI的腳本。我有一個文本框應該不斷更新基於用戶選擇列表中的最後一個項目。我怎麼會去的東西,通常會持續checken其內部消除用戶手動按下一個按鈕或東西時調用的函數。 THX文本不斷給予反饋關於當前選擇

編輯:酷,這就是我一直在尋找。謝謝。你的腳本工作得很好。我嘗試將其實施到我的大腳本中,導致UI中的每個按鈕都必須按兩次才能生效。但我會在稍後處理。然而,在一個單獨的測試腳本我自己的版本,有時如下產生以下錯誤:「SL」必須通過一個boolean參數

#________________________________________________________________________________________________________________________________________________ 
import maya.cmds as cmds 

idManagerUI = cmds.window(title='Vray ID Manager', s = False, wh = (305,500)) 
cmds.columnLayout(adj = True) 
cmds.text (l = '__________________________________________ \n your current selection has this ID: \n') 
curSelTxt = cmds.text (l = '', backgroundColor = [0.2, 0.2, 0.2]) 


def update_label(*_): 
    upCurrentObjectSel = cmds.ls(sl=True) 
    upCurrentObjectSelShapes = cmds.listRelatives(upCurrentObjectSel) 
    upLastobj = upCurrentObjectSelShapes[-1] 
    print upLastobj 
    cmds.text(curSelTxt, e=True, label = upLastobj) 

cmds.scriptJob(event=("SelectionChanged", update_label), p= curSelTxt) 

cmds.showWindow(idManagerUI) 

而且,做兩個這些ScriptJobs使得腳本停止正常工作的全部。

#________________________________________________________________________________________________________________________________________________ 
import maya.cmds as cmds 

idManagerUI = cmds.window(title='Vray ID Manager', s = False, wh = (305,500)) 
cmds.columnLayout(adj = True) 
cmds.text (l = '__________________________________________ \n your current selection has this ID: \n') 
curSelObjTxt = cmds.text (l = '', backgroundColor = [0.2, 0.2, 0.2]) 
curSelShadTxt = cmds.text (l = '', backgroundColor = [0.2, 0.2, 0.2]) 

def update_obj_label(*_): 

    upCurrentObjectSel = cmds.ls(sl=True) 
    upCurrentObjectSelShapes = cmds.listRelatives(upCurrentObjectSel) 
    upLastobj = upCurrentObjectSelShapes[-1] 
    objID = cmds.getAttr(upLastobj + '.vrayObjectID') 
    print objID 
    cmds.text(curSelObjTxt, e=True, label = objID) 

def update_shader_label(*_): 
    upCurrentShaderSel = cmds.ls(sl=True, type= shaderTypes) 
    upCurrentShaderSelLast = upCurrentShaderSel[-1] 
    shadID = cmds.getAttr(upCurrentShaderSelLast + '.vrayMaterialId') 
    cmds.text(curSelShadTxt, e=True, label = shadID) 
    print shadID 

cmds.scriptJob(event=("SelectionChanged", update_obj_label), p= curSelObjTxt) 
cmds.scriptJob(event=("SelectionChanged", update_shader_label), p= curSelShadTxt) 

cmds.showWindow(idManagerUI) 

編輯: 還是你的最新劇本的版本產生錯誤不時。 另外,我注意到在我自己的版本的話,它有時工作可愛,那麼一點都沒有,一旦我如將我的活動窗口切換到Firefox等,然後返回到Maya,有時它根本不起作用。這只是我的腳本中的一個功能。與他們(s.below)它是完全無法使用的。結果非常不穩定。

import maya.cmds as cmds 

def curSelTxtShower(): 
    idManagerUI = cmds.window(title='Vray ID Manager', s = False, wh = (305,500)) 
    cmds.columnLayout(adj = True) 
    cmds.text (l = '__________________________________________ \n your current selection has this ID: \n') 
    curSelObjTxt = cmds.text (l = '', backgroundColor = [0.2, 0.2, 0.2]) 
    curSelShadTxt = cmds.text (l = '', backgroundColor = [0.2, 0.2, 0.2]) 

    def update_obj_label(*_): 
     upCurrentObjectSelShapes = cmds.listRelatives(s=True) or ["nothing selected"] 
     upLastobj = upCurrentObjectSelShapes[-1] 
     if upLastobj is not "nothing selected": 
      if cmds.attributeQuery('vrayObjectID', node = upLastobj, ex = True) is True: 
       objID = cmds.getAttr(upLastobj + '.vrayObjectID') 
       cmds.text(curSelObjTxt, e=True, label = objID) 
     else: 
      cmds.text(curSelObjTxt, e=True, label = 'curSel has no ObjID assigned') 

    def update_shader_label(*_): 
     upCurrentShaderSel = cmds.ls(sl=True, type= shaderTypes) 
     upCurrentShaderSelLast = upCurrentShaderSel[-1] 
     if cmds.attributeQuery('vrayMaterialId', node = upCurrentShaderSelLast, ex = True) is True: 
      shadID = cmds.getAttr(upCurrentShaderSelLast + '.vrayMaterialId') 
      cmds.text(curSelShadTxt, e=True, label = shadID) 
      print shadID 
     else: 
      cmds.text(curSelShadTxt, e=True, label = 'curSel has no MatID assigned') 

    cmds.scriptJob(event=("SelectionChanged", lambda *x: update_obj_label()), p= curSelObjTxt) 
    cmds.scriptJob(event=("SelectionChanged", lambda *x: update_shader_label()), p= curSelShadTxt) 
    cmds.showWindow(idManagerUI)  


curSelTxtShower() 

回答

0

您需要使用scriptJob才能觀察選擇更改事件並作出迴應。您需要將腳本作業父項添加到特定的用戶界面中,以便在UI對象消失時將其刪除。

絕對最小的例子是這樣的:

import maya.cmds as cmds 

    wind = cmds.window() 
    col = cmds.columnLayout() 
    txt = cmds.text(label="", width = 240) 

    def update_label(*_): 
     cmds.text(txt, e=True, label = str(cmds.ls(sl=True))) 

    cmds.scriptJob(event=("SelectionChanged", update_label), p= txt) 

    cmds.showWindow(wind) 

編輯

這個版本的OP代碼有助於避免虛假的錯誤信息。實際的錯誤發生時,由於空選擇 - 但腳本作業虛假地報告在上一行:

def scoped(): 
    idManagerUI = cmds.window(title='Vray ID Manager', s = False, wh = (305,500)) 
    cmds.columnLayout(adj = True) 
    cmds.text (l = '__________________________________________ \n your current selection has this ID: \n') 
    curSelTxt = cmds.text (l = '', backgroundColor = [0.2, 0.2, 0.2]) 


    def update_label(*_): 
     # listRelatives works off the current selection already 
     upCurrentObjectSelShapes = cmds.listRelatives(s=True) or ["nothing selected"] 
     upLastobj = upCurrentObjectSelShapes[-1] 
     cmds.text(curSelTxt, e=True, label = upLastobj) 


    cmds.showWindow(idManagerUI) 
    cmds.scriptJob(event=("SelectionChanged", update_label), p= idManagerUI) 

scoped() 
+0

謝謝,但它會產生以下錯誤randomely:'sl'必須通過布爾參數看我更新後在頂部 – leabum

+0

瑪雅有時會從scriptjobs誤導性的錯誤消息。請參閱編輯 – theodox

+0

謝謝。但我的版本變得非常不穩定 – leabum