akerbeltz
akerbeltz

Reputation: 49

Button calling a function in Maya

I'm trying to make a button in a function that calls another function inside a class.

That's what I did:

# 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()

The problem is I get printed nothing. What am I doing wrong?

Upvotes: 1

Views: 1643

Answers (2)

OneCricketeer
OneCricketeer

Reputation: 191728

I don't see why you need a partial function when you can let the text field be a member variable of the class

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 

Upvotes: 0

akerbeltz
akerbeltz

Reputation: 49

founded a solution but i thing it's a bit strange

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

Upvotes: 1

Related Questions