Create a gist now

Instantly share code, notes, and snippets.

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
@pboucher
Owner

Yeah, don't forget that xsi == Application. Doh! I'll fix it when our admin opens git protocol port in the firewall. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment