Lambda or Partial? Is there a preferred solution to pass an argument to a callback?
Using partial:
#partial
from functools import partial
import maya.cmds as mc
class ButtonWin(object):
def __init__(self):
self.win = mc.window()
self.layout = mc.columnLayout(parent=self.win)
for numberX in range(10):
mc.button(label="Click Here %d" % numberX, parent=self.layout, command=partial(self.report, numberX)) #PARTIAL
mc.showWindow()
def report(self, buttonIndex, value):
print "button %d got %s" % (buttonIndex, value)
f = ButtonWin()
Or Lambda:
#Lambda
import maya.cmds as mc
class ButtonWin(object):
def __init__(self):
self.win = mc.window()
self.layout = mc.columnLayout(parent=self.win)
for numberX in range(10):
mc.button(label="Click Here %d" % numberX, parent=self.layout, command=lambda x: self.report(numberX, "True")) #LAMBDA
mc.showWindow()
@staticmethod
def report(buttonIndex, value):
print "button %d got %s" % (buttonIndex, value)
f = ButtonWin()
Thanks in advance!