【问题标题】:enable or disable a field in maya ui with python使用python启用或禁用maya ui中的字段
【发布时间】:2015-01-12 00:24:01
【问题描述】:

我正在尝试在 Maya 的 python 脚本的 ui 中启用/禁用浮点字段,但我不知道它是如何完成的。这是一个例子:

import maya.cmds as cmds

def createUI(windowTitle):

    windowID = 'myWindoWID'

    if cmds.window(windowID, exists = True):
        cmds.deleteUI(windowID)

    cmds.window( windowID, title = windowTitle, sizeable = False, resizeToFitChildren = True) 

    cmds.rowColumnLayout(numberOfColumns = 3, columnWidth = [(1,120), (2,120), (3,120)])

    cmds.floatField(enable = False)
    cmds.button(label = 'enable the float field', command = enableFloatField)
    cmds.button(label = 'disable the float field', command = disableFloatField)        

    cmds.showWindow()

def enableFloatField(*args):
    #enable the float field
    print 'float field enabled'


def disableFloatField(*args):
    #disable the float field
    print 'float field disabled'


createUI('my window')

【问题讨论】:

  • 非常感谢您的帮助和解释。 :)

标签: python user-interface maya


【解决方案1】:

首先将您的浮点字段存储在一个变量中。

my_float_field = cmds.floatField(enable = False)

我们使用 functools.partial 将此变量传递给按钮的命令方法。

cmds.button(label = 'enable the float field', command = partial(enableFloatField, my_float_field))
cmds.button(label = 'disable the float field', command = partial(disableFloatField, my_float_field))

然后,在您的方法中,我们在编辑模式下调用 cmds.floatField() 并编辑您作为参数发送的特定浮点字段。

def enableFloatField(float_field, *args):
    #enable the float field
    cmds.floatField(float_field, edit=True, enable=True)


def disableFloatField(float_field, *args):
    #disable the float field
    cmds.floatField(float_field, edit=True, enable=False)

记得导入 functools。

from functools import partial

所以你的整个代码是:

from functools import partial
import maya.cmds as cmds

def createUI(windowTitle):

    windowID = 'myWindoWID'

    if cmds.window(windowID, exists = True):
        cmds.deleteUI(windowID)

    cmds.window( windowID, title = windowTitle, sizeable = False, resizeToFitChildren = True) 

    cmds.rowColumnLayout(numberOfColumns = 3, columnWidth = [(1,120), (2,120), (3,120)])

    my_float_field = cmds.floatField(enable = False)
    cmds.button(label = 'enable the float field', command = partial(enableFloatField, my_float_field))
    cmds.button(label = 'disable the float field', command = partial(disableFloatField, my_float_field))

    cmds.showWindow()

def enableFloatField(float_field, *args):
    #enable the float field
    cmds.floatField(float_field, edit=True, enable=True)


def disableFloatField(float_field, *args):
    #disable the float field
    cmds.floatField(float_field, edit=True, enable=False)


createUI('my window')

cmds.floatField(float_field, edit=True, enable=False) 中要注意的重要一点是 edit=True 标志。此标志在edit 模式下调用 UI 方法,这意味着您传递给此 UI 方法的任何参数都将用于编辑现有的 UI 元素,这将是该方法的第一个参数;在本例中为 float_field,其中包含浮动字段的名称,可能类似于 'myWindoWID|rowColumnLayout6|floatField11'

另一个这样的模式标志是query=True,它可以让您查询 UI 元素的参数。如果这两个标志都不存在,Maya 将假定该方法是在 create 模式下调用的。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多