按钮在Maya中调用函数

时间:2017-08-01 01:22:54

标签: python python-2.7 maya

我试图在一个调用类中另一个函数的函数中创建一个按钮。

这就是我的所作所为:

# button calling functions
import maya.cmds as cmds
import maya.mel as mel
from functools import partial

class B:
    def __init__(self):
        self.create_window()

    def printText(self, text, *args):
        print(text)
        return

    def create_window(self):
        window = cmds.window()
        cmds.rowColumnLayout(numberOfColumns=2, columnWidth=[(1, 50), (1, 70)])
        cmds.text(label='Name')
        axis = cmds.textField()
        cmds.showWindow(window)
        text_entered = cmds.textField(axis, query=True, text=True)
        #cmd = 'printText("{0}")'.format(text_entered)
        cmds.button(label="asd", command = partial(self.printText, text_entered))
        return       

a = B()

问题是我什么都没打印。我做错了什么?

2 个答案:

答案 0 :(得分:1)

建立了一个解决方案,但事实上它有点奇怪

import maya.cmds as cmds

class B:
    def __init__(self):
        self.create_window()

    def create_window(self):
        if cmds.window("UI", exists=True):
            cmds.deleteUI("UI")
        win = cmds.window("UI")
        cmds.columnLayout()
        textEntered = cmds.textField()

        def print_text_contents(a):
            print cmds.textField(textEntered, query=True, text=True)

        cmds.button(label='Confirm', command=print_text_contents)
        cmds.showWindow(win)


B()

答案 1 :(得分:0)

当你可以让文本字段成为类的成员变量时,我不明白你为什么需要部分功能

def printText(self, _ignored):
    print(">>" + self.text_entered)  # note this is an object, not the contents 
    return

def create_window(self):
    window = cmds.window()
    cmds.rowColumnLayout(numberOfColumns=2, columnWidth=[(1, 50), (1, 70)])
    cmds.text(label='Name')
    axis = cmds.textField()
    cmds.showWindow(window)
    self.text_entered = cmds.textField(axis, query=True, text=True)
    #cmd = 'printText("{0}")'.format(text_entered)
    cmds.button(label="asd", command =self.printText)
    return