You can yank the code from pymel... (right Chadrik?):

 

class Callback:

    """

    Enables deferred function evaulation with 'baked' arguments.

    """

 

    _callData = None

    @staticmethod

    def _doCall():

        (func, args, kwargs) = Callback._callData

        Callback._callData = func(*args, **kwargs)

 

    def __init__(self,func,*args,**kwargs):

        self.func = func

        self.args = args

        self.kwargs = kwargs

 

    def __call__(self, *args, **kwargs):

        Callback._callData = (self.func, self.args, self.kwargs)

        core.mel.python("__import__('%s').Callback._doCall()" % __name__)

        return Callback._callData    

 

 

Another benefit of this implementation is that it handles 'undo' properly,
by pushing the function call through the MEL interperter.

 

From: [email protected]
[mailto:[email protected]] On Behalf Of Sylvain Berger
Sent: Wednesday, June 10, 2009 2:39 PM
To: [email protected]
Subject: [Maya-Python] Re: using partial in window classes

 

yeah I know, I would like to be able to do it without pymel if possible.

 

I want to keep my options open so that I don't always have to rely on a 3rd
party module to do things.

 

I kinda found a stupid workaround...basically I need to add one dummy
argument to the function I call with partial... not elegent but it works

 

On Wed, Jun 10, 2009 at 5:23 PM, Ofer Koren <[email protected]> wrote:


pymel has Callback object class that works well for this purpose.
It works like partial except that it'll ignore any incoming args/kwargs that
are passed when the callback is invoked:


cb = pm.Callback(myFunc, arg1=1)
cb("args", dont='matter')


import pymel as pm
pm.button(l="Test button", c=pm.Callback(myFunc, 1, 2, arg1="a", arg2="b"))






-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of sberger
Sent: Wednesday, June 10, 2009 1:25 PM
To: python_inside_maya
Subject: [Maya-Python] Re: using partial in window classes


please note that the example I sent are reversed... the first one is
the keywords example, and the second one the non keywords example



On Jun 10, 4:10 pm, sberger <[email protected]> wrote:
> Hi guys, I found this trick for Maya. When you create a window class,
> I can use the partial function to build my commands sent to my
> buttons.
>
> The problem is I can only manage to make this work with one arguments,
> when I send 2 or more arguments i get errors.
>
> Here is the code:
>
> from functools import partial
> import maya.cmds as cmds
> class ButtonWin(object):
>         def __init__(self):
>                 window='testWin'
>                 if (cmds.window(window, exists=True)):
>                         cmds.deleteUI(window, window=True)
>                 self.win = cmds.window(window)
>                 self.layout = cmds.columnLayout(parent=self.win)
>                 for x in range(10):
>                         partialFunc =
 partial(self.report,buttonIndex=x,value=x*2)
>                         print 'function:', partialFunc.func
>                         print 'args:', partialFunc.args
>                         print 'keywords:', partialFunc.keywords
>                         cmds.button(label="Click Here %d"%x,
parent=self.layout,
> command=partialFunc)
>                         cmds.showWindow()
>         def report(self,buttonIndex=0,value=0):
>                 print "button %d got %s"%(buttonIndex,value)
> f = ButtonWin()
>
> I have tried using keywords arguments but I still get an error:
> from functools import partial
> import maya.cmds as cmds
> class ButtonWin(object):
>         def __init__(self):
>                 window='testWin'
>                 if (cmds.window(window, exists=True)):
>                         cmds.deleteUI(window, window=True)
>                 self.win = cmds.window(window)
>                 self.layout = cmds.columnLayout(parent=self.win)
>                 for x in range(10):
>                         partialFunc =  partial(self.report,x,x*2)
>                         print 'function:', partialFunc.func
>                         print 'args:', partialFunc.args
>                         print 'keywords:', partialFunc.keywords
>                         cmds.button(label="Click Here %d"%x,
parent=self.layout,
> command=partialFunc)
>                         cmds.showWindow()
>         def report(self,buttonIndex,value):
>                 print "button %d got %s"%(buttonIndex,value)
> f = ButtonWin()
>
> Anyone have experience using this partial function?
>
> Thanks







-- 
They say, "Evil prevails when good men fail to act." What they ought to say
is, "Evil prevails." 
Nicolas Cage as Yuri Orlov in Lord of War.




--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/python_inside_maya
-~----------~----~----~----~------~----~------~--~---

Reply via email to