Created
January 19, 2011 18:08
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah, don't forget that xsi == Application. Doh! I'll fix it when our admin opens git protocol port in the firewall. ;)