pboucher
1/19/2011 - 6:08 PM

SetValue decorator for Softimage that can temporarily change any value for the duration of a method call and restore it afterward. Originall

SetValue decorator for Softimage that can temporarily change any value for the duration of a method call and restore it afterward. Originally posted at http://www.softimageblog.com/archives/357

@tempSetValues({'preferences.General.undo': 0, 'preferences.scripting.cmdlog': False})
def doStuff(obj):
	# Do stuff
	# The undo stack will be set to 0 and cmdlog to False before execution and
	# restored no matter if the call succeeds or fails.
	pass
import functools

xsi = Application

def tempSetValues(newVals):
    def closure(func):
        @functools.wraps(func)
        def inner(*args, **kwargs):
            # Save current values and set new ones
            oldVals = {}
            for k, v in newVals.iteritems():
                oldVals[k] = xsi.GetValue(k)
                xsi.SetValue(k, v, "")

            # Run the wrapped function
            try:
                return func(*args, **kwargs)
            finally:
                # Restore the old values
                for k, v in oldVals.iteritems():
                    xsi.SetValue(k, v, "")
        return inner
    return closure