Thanks for posting that.
I'm interested to see how you are actually using this version of the code,
because it does work differently than the original example. Your version
seems to combine the decorator and the callback into a single function
which will always attempt to call the saved function, and also create and
return a wrapping function. But I don't seem to be able to get it to work
very well. I'm assuming you want to use it like this?
@repeat_command()
def test():
print "foo"
The problem I see is that because you have combined everything into one
function, every time a new function is defined that uses that decorator, it
will run a previously saved function because it may not be None at the
time. Also, the wrapped function can no longer return a value, since it is
discarded and a new wrap() function object is returned and printing in the
script editor.
I threw together a cleaned up version of the example from the bottom of
that blog post:
https://gist.github.com/justinfx/f86320b6d9b567e6b8e2
It keeps the decorator and callback separate, only defines the callback
string once, and also catches a potential exception that cmds.repeatLast()
might raise, when you are using it to run code within the script editor
(which already sets repeatLast to the thing you are executing).
Justin
On Mon, May 25, 2015 at 8:30 PM Rémi Deletrain <[email protected]>
wrote:
> Yes of course!
>
> I past the cleaned script that works for me.
> If you have problems with the one made me know. If I find problems with
> and I corrected them I'll post an update.
>
> It's just a version corrected and with a vocabulary adapted to my scripts.
> Otherwise it is the same as the link
>
> #-----------------------------------------------------------------
> # REPEAT COMMAND
> #-----------------------------------------------------------------
>
> import pymel.core as pmc
>
> _repeat_function = None
> _args = None
> _kwargs = None
>
>
> def repeat_command():
>
> if _repeat_function is not None:
> _repeat_function(*_args, **_kwargs)
>
>
> def wrap(function):
> def wrapper(*args, **kwargs):
>
> global _repeat_function
> global _args
> global _kwargs
>
> _repeat_function = function
> _args = args
> _kwargs = kwargs
>
> commandToRepeat = ('python("%s.repeat_command()")' % __name__)
>
> ret = function(*args, **kwargs)
>
> pmc.repeatLast(addCommand=commandToRepeat,
> addCommandLabel=function.__name__)
>
> return ret
> return wrapper
> return wrap
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/7fc2fbae-3a95-46af-9c0e-25a895e606bd%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/7fc2fbae-3a95-46af-9c0e-25a895e606bd%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA12dhSwg-kCd%2BB%2BJy10fvtmOV7NkvFcPg1g7YmhdAzqaw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.