Search code examples
pythonfunctionclassmaya

calling a function in a class when button is pressed? in python


I'm running into a a few errors when trying to call a function. I have a button that when pressed should print snapped.

#code for button
cmds.button(label='FK 2 IK', command = 'Fk2Ik()', width=100)
cmds.button(label='IK 2 FK',  command = Snapping.Ik2Fk(), width=100)
cmds.setParent('..')
cmds.separator(h=5, style = 'none')
cmds.separator(h=5)




#code for function
class Snapping(self):   

    def Ik2Fk(self):
        print "Snapped"


'''
the error I get is this

# Error: TypeError: file <maya console> line 119: unbound method Ik2Fk() must be called with Snapping instance as first argument (got nothing instead) #

Solution

  • class Snapping(self):   
        @staticmethod
        def Ik2Fk():
            print "Snapped"
    
    cmds.button(label='FK 2 IK', command = 'Fk2Ik()', width=100)
    cmds.button(label='IK 2 FK',  command = Snapping.Ik2Fk, width=100)
    cmds.setParent('..')
    cmds.separator(h=5, style = 'none')
    cmds.separator(h=5)