[Maya / Python] Fixing Undo / Redo Messages eg. <functools.partial object at 0x00

EDIT: I have found the solution:

class rpartial(partial):
    def __init__(self, *args):
        self.result = args[-1]
    def __repr__(self):
        return self.result

Subclass partial, in init give it *args so that it can read from the list of parameters provided, the final argument I provide is accessed with an index of -1 and becomes the result it prints. repr() returns the identification and maya prints it on undo/redo.

— Original post below —

I use a ton of functools.partial and if I undo/redo it looks like variations of:

// Undo: <functools.partial object at 0x000001F6075633B8> // 

As opposed to (for example):

// Undo: channelBoxCommand -key // 

How do I make it output the proper result?

you could make a Context Manager to handle Undo/Redo.

then when you need to run a piece of code that should be one undo/redo action, you can:


with undoCtxManager(chunkName = 'myScript'):
    myScript()
    myOtherScript()

[QUOTE=rgkovach123;29346]you could make a Context Manager to handle Undo/Redo.

then when you need to run a piece of code that should be one undo/redo action, you can:


with undoCtxManager(chunkName = 'myScript'):
    myScript()
    myOtherScript()

[/QUOTE]

Hey… in short it doesn’t seem to be able to change the message thats printed when you undo/redo. Chunk name doesn’t seem to affect it but I may be using it wrong somehow.

This is what I’ve done in the end (that doesn’t work). This can disable the undo queue without flushing for UI commands if you give it a 0 and that part works, however if you give it a string it’s meant to open a chunk and give it a name. Isn’t that name meant to be returned when you undo it? Well it’s not, it’s still the same as in my OP.

class Undo(object):
    def __init__(self, result):
        self.result = result
    def __enter__(self):
        if self.result == 0:
            cmds.undoInfo(stateWithoutFlush = 0)
        else:
            cmds.undoInfo(openChunk = 1, chunkName = self.result)
    def __exit__(self, *exc_info):
        if self.result == 0:
            cmds.undoInfo(stateWithoutFlush = 1)
        else:
            cmds.undoInfo(closeChunk = 1)

Usage:

with sc.Undo("Result: SetKey"):
    cmds.channelBox(cbox, e = 1, exe = ( "if( `getAttr -k \"#P.#A\"`||`getAttr -channelBox \"#P.#A\"` )setKeyframe \"#P.#A\";", 1))

Though your response lead me to finding the above info which is really really nice at least for UI commands, I hope I’ve just missed something obvious here :slight_smile:

Thanks very much

SOLVED!

class rpartial(partial):
    def __init__(self, *args):
        self.result = args[-1]
    def __repr__(self):
        return self.result

Subclass partial, in init give it *args so that it can read from the list of parameters provided, the final argument I provide is accessed with an index of -1 and becomes the result it prints. repr() returns the identification and maya prints it on undo/redo.

Someone give me a cookie.