Search code examples
pythonmaya

Can't find the fix for: takes exactly 1 argument (2 given)


I got into python recently and I already got stuck with 'takes exactly 1 argument (2 given)' problem.

I've searched for it around and most of the time and read about missing to add the self part.

Despite adding that I can't solve it, am I missing wome important point?

import maya.cmds as cmds

class ButtonPress:

    def __init__(self):
        self.value = 0

    def buildUI(self):
        window = cmds.window(title = 'Press Button', w = 100, h = 50)
        columnL = cmds.columnLayout(w = 100, h = 50)
        cmds.button(parent = columnL, label = 'Press me', w = 100, h = 50, command = self.__increaseAndPrint)
        cmds.showWindow(window)

    def __increaseAndPrint(self):
        self.value += 1
        print self.value

Thanks for the help.

EDIT: I use the class in maya's script editor with:

ButtonPress().buildUI()

And I get: Error: __increaseAndPrint() takes exactly 1 argument (2 given) When pressing the UI button.

Sorry.


Solution

  • import maya.cmds as cmds
    
    class ButtonPress:
    
        def __init__(self):
            self.value = 0
    
        def buildUI(self):
            window = cmds.window(title = 'Press Button', w = 100, h = 50)
            columnL = cmds.columnLayout(w = 100, h = 50)
            cmds.button(parent = columnL, label = 'Press me', w = 100, h = 50, command = self.__increaseAndPrint)
            cmds.showWindow(window)
    
        def __increaseAndPrint(self, *args):
            # maya throwing through ui a default bool argument as last.
            # you need *args to catch and dismissed it
            self.value += 1
            print self.value