This is perfect for a python decorator. Here is an example:
Code:
from reaper_python import *
class Undoable(object):
ALL = 0xFFFFFFFF
TRACKCFG = 1
FX = 2
ITEMS = 4
MISCCFG = 8
def __init__(self, msg, undo_flags = ALL):
self._msg = msg
self._undo_flags = undo_flags
def __call__(self, f):
def wrapped():
RPR_Undo_BeginBlock()
RPR_PreventUIRefresh(1)
try:
f()
except Exception, e:
RPR_MB(str(e), "Error", 0)
RPR_Undo_EndBlock(self._msg, self._undo_flags)
RPR_PreventUIRefresh(-1)
RPR_UpdateArrange()
RPR_UpdateTimeline()
RPR_TrackList_AdjustWindows(False)
return wrapped
@Undoable("Insert new track")
def my_script():
RPR_InsertTrackAtIndex(0, False)
my_script()
The code in my_script is not important, it just inserts a new track. But all the undo and gui refresh calls are done inside the Undoable class which you decorate the function my_script with. You specify your undo message as the first argument to the Undoable decorator.
You can set a different undo flag like so:
Code:
@Undoable("Insert new track", Undoable.TRACKCFG)
def my_script():
RPR_InsertTrackAtIndex(0, False)