Python for maya class question

python

#1

hi ,guys , I want try ClassMetohd, but I don’t know why I made a mistake.thx

import maya.cmds as mc
class test(object):
    def __init__(self):
        name='test'
        if mc.window(name,ex=True):
            mc.deleteUI(name)
        mc.window(name)
        self.testUI()
        mc.showWindow(name)
        
    def testUI(self):
        mc.columnLayout()
        mc.button('test1',c='')    # what shall I do?
        mc.button('test2',c='self.check(V=1)')  # its wrong NameError: file <maya console> line 1: name 'self' is not defined # 
        mc.button('test3',c=self.check)  # its work,but its not I want
        
    def check(self,V=0):
        if V==1:
            print 'ok'
        elif V==2:
            print 'not ok'
        print '???'
        
test()

#2

You might want to double check the formatting in youe post, it’s a bit hard to follow with the indents as you have them.

That said - you can’t use the ‘string’ form of assigning a callback inside a class. You want to use the object form instead: no quotes around the method name. If you want to pass an argument to the callback you need a lambda or a functools.partial .

The entire question is covered in detail in this blog post


#3

thx ,It’s very useful to me. :upside_down_face: