intField 不显示变化

intField does not display changes

我正在编写一个脚本来简化使用 Vray 时的繁琐任务,但我坚持使用 intFields,它应该允许用户输入一个 int 值,该值在按下按钮时触发特定操作。我将代码简化为只有必要的部分。无论我将值更改为什么,它在脚本编辑器输出中始终为 0。

import maya.cmds as cmds

idManagerUI = cmds.window(title='Vray ID Manager', s = False, wh = (300,500))

cmds.columnLayout(adj = True)

cmds.text (l = 'type in MultimatteID to select matching shaders \n or specify ObjectID to select matching objects \n __________________________________________ \n')

cmds.text (l = 'MultimatteID: \n')
cmds.intField( "MultimatteID", editable = True)
MultimatteIdButton = cmds.button(l = 'Go!', w = 30, h = 50, c = 'multimatteChecker()')
cmds.text (l = '\n')

cmds.showWindow(idManagerUI)

MultimatteIdInput = cmds.intField( "MultimatteID", q = True, v = True)


def multimatteChecker():
    print MultimatteIdInput

三件事:

首先,如所写,您无法确定 intField MultimatteID 是否确实获得了您认为它应该具有的名称。 Maya 小部件名称是唯一的,就像 Maya 对象名称一样——您可以将其命名为 MultimatteID 但实际上会取回一个名为 MultimatteID2 的小部件,因为您在某处(可见或不可见)有一个未删除的 window一个类似命名的控件。

其次,您粘贴的代码在创建 window 后立即查询控件的值。它应该始终打印出您在创建时赋予它的值。

最后 -- 不要在按钮中使用字符串版本的命令分配。当您从侦听器中的代码移动到工作脚本时,它是不可靠的。

这应该可以满足您的要求:

    idManagerUI = cmds.window(title='Vray ID Manager', s = False, wh = (300,500))
    cmds.columnLayout(adj = True)
    cmds.text (l = 'type in MultimatteID to select matching shaders \n or specify ObjectID to select matching objects \n __________________________________________ \n')
    cmds.text (l = 'MultimatteID: \n')
    # store the intField name
    intfield = cmds.intField( "MultimatteID", editable = True)
    cmds.text (l = '\n')

    # define the function before assigning it. 
    # at this point in the code it knows what 'intfield' is....
    def multimatteChecker(_):
        print cmds.intField( intfield, q = True, v = True)

    #assign using the function object directly
    MultimatteIdButton = cmds.button(l = 'Go!', w = 30, h = 50, c = multimatteChecker)