Two ways…
OneUndo decorater by Cesar Saez
from functools import wraps def OneUndo(function): @wraps(function) def _inner(*args, **kwargs): try: Application.BeginUndo() f = function(*args, **kwargs) finally: Application.EndUndo() return f return _inner # # And a basic example... # @OneUndo def CreateNulls(p_iCounter=100): lNulls = [] for i in range(p_iCounter): lNulls.append( Application.ActiveSceneRoot.AddNull() ) return lNulls CreateNulls()
Undo with statement by ethivierge
from win32com.client import constants as c from win32com.client.dynamic import Dispatch as d xsi = Application log = xsi.LogMessage collSel = xsi.Selection class xsiUndo(): def __enter__(self): xsi.BeginUndo() def __exit__(self, type, value, traceback): xsi.EndUndo() def testFunc(): log("running test") with xsiUndo(): testFunc()
Sorry to be such a pain, but two more “"” sneaked (snuck?) into the second listing… 😉
And now WordPress automagically changes my post: I meant the " token
No pain at all, thanks for the copy edit.
So do any of this methods have advantages over the other? Or is it pretty much the same thing?
Exercise left to the reader… 🙂
But I think…a decorator is associated with a specific function, whereas with with you can use with with any function that you want to pair with the undo bracket.
http://preshing.com/20110920/the-python-with-statement-by-example
Sorry about all the alliteration. haha