使用python启用或禁用Maya ui中的字段 [英] enable or disable a field in maya ui with python

查看:172
本文介绍了使用python启用或禁用Maya ui中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to enable/disable a float field in the ui of a python script for maya, but I dont know how it is done. Here is an example:

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

推荐答案

首先将float字段存储在变量中.

Store your float field in a variable first.

my_float_field = cmds.floatField(enable = False)

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

We use functools.partial to pass this variable to your buttons' command methods.

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()并编辑您作为参数发送的特定float字段.

In your methods, we then call cmds.floatField() in edit mode and edit your particular float field that you sent as a parameter.

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.

Remember to import functools.

from functools import partial

因此,您的整个代码应为:

So your whole code would be:

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方法的任何参数都将用于 edit 编辑现有的UI元素,该元素将是该方法的第一个参数;在这种情况下,为float_field,其中包含您的float字段的名称,该名称可能类似于'myWindoWID|rowColumnLayout6|floatField11'.

The important thing to note here in cmds.floatField(float_field, edit=True, enable=False) is the edit=True flag. This flag calls the UI method in edit mode, meaning any paramaters you pass into this UI method will be used to edit an existing UI element, which will be the first parameter of the method; in this case float_field, which contains the name of your float field which might look something like 'myWindoWID|rowColumnLayout6|floatField11'.

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

Another such mode flag is query=True, which will let you query parameter(s) of a UI element. If both of these flags are not present, Maya will assume that the method was called in create mode.

希望这会有所帮助.

这篇关于使用python启用或禁用Maya ui中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆