Skip to content

Instantly share code, notes, and snippets.

@pboucher
Created January 19, 2011 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pboucher/786568 to your computer and use it in GitHub Desktop.
Save pboucher/786568 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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