文本不断反馈当前选择

时间:2017-03-26 13:04:40

标签: python-2.7 maya

我正在编写一个在Maya中具有UI的脚本。我有一个文本框,应该根据用户选择列表中的最后一项不断更新。如何让用户无需手动按下按钮或其他东西来调用功能,我将如何不断检查。 THX

编辑:酷我正在寻找的东西。谢谢。你的脚本运行得很好。我尝试将其实现到我的大脚本中,这导致UI中的每个按钮都必须被按两次才能生效。但我稍后会处理。但是,如下所示在单独的脚本中测试我自己的版本有时会产生以下错误:'sl'必须传递一个布尔参数

#________________________________________________________________________________________________________________________________________________
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()  

1 个答案:

答案 0 :(得分:0)

您需要使用scriptJob来观看选择更改事件并做出回应。您需要将scriptJob作为特定UI的一部分,以便在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代码有助于避免错误的错误消息。由于选择为空而发生实际错误 - 但是ScriptJob在上一行中错误地报告了它:

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()