问题
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')
回答1:
Store your float field in a variable first.
my_float_field = cmds.floatField(enable = False)
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))
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)
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')
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'
.
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.
Hope this helps.
来源:https://stackoverflow.com/questions/27888857/enable-or-disable-a-field-in-maya-ui-with-python