如何在Maya python中打印intField的值

时间:2018-01-02 08:09:31

标签: python maya

我想使用python打印maya中用户给出的intField的值。

代码

import maya.cmds as cmds

def fun(number):
    print(number) 

cmds.window()
cmds.columnLayout()
num = cmds.intField(changeCommand = 'fun()')
cmds.showWindow()

用户将在intField中输入值,我想打印该值

2 个答案:

答案 0 :(得分:0)

你必须使用lambda或partial:

import maya.cmds as cmds
from functools import partial

# Here is how i layout this kind of case :
# First the function that is not related to the ui 
def anyFuction(number):
    print(number) 

# i create a parser who will query value by default but some ui elements may use other arguments
# I put kwargs for example if you want to query another flag (for some simple ui, i even put as argument the cmds.intField)
# note that i had if your function as some other things to pass throught
# if you have multiple fields to query you can bind the function into another function example below the code
def queryIntfield(function='', uiNameElem, *args, **kwargs):
    if kwargs:
        v = cmds.intField(uiNameElem, q=True, **kwargs)
    else:
        v = cmds.intField(uiNameElem, q=True, v=1)

    if len(args) > 1:    
        function(v, *args)
    elif function = '':
        return v
    else:
        function(v)


cmds.window()
cmds.columnLayout()
# create the field to get his name : num
num = cmds.intField(changeCommand = '')# do not put a function in comma
# edit the command placeholder 
cmds.intField(num, e=1, changeCommand = partial(ui_queryIntfield, anyFuction, num))
cmds.showWindow()

-----示例2

'''

# Example 2 

# Here is how i layout this kind of case :
# First
def anyFuction(number):
    print(number) 
# Second
def queryIntfield(uiNameElem, *args, **kwargs):
    if kwargs:
        v = cmds.intField(uiNameElem, q=True, **kwargs)
    else:
        v = cmds.intField(uiNameElem, q=True, v=1)
    return v
# Third, if it is a complex function, i duplicate the name anyFunction and add ui_ to know im binding this function with ui
def ui_anyFunction(uiNameElem, *args, **kwargs):
    # Do some stuff
    value = queryIntfield(uiNameElem)
    # and other stuff
    # .....
    # ..............
    anyFuction(value)
'''

答案 1 :(得分:0)

您可以使用lambda或partial,或者只需在UI元素已存在的范围内定义回调函数,以便它知道UI的名称:

import maya.cmds as cmds

def fun(number):
    print(number) 

cmds.window()
cmds.columnLayout()
num = cmds.intField()
def num_callback():
    print cmds.intField(num, q=True, value=True)
cmds.intField(num, e=True, changeCommand = num_callback)
cmds.showWindow()

通常,您希望避免使用字符串形式的回调赋值:如果您不在侦听器中,则maya无法找到该函数的常见问题来源。更多细节here