Last active
August 29, 2015 14:07
-
-
Save mottosso/24b88b4df1bbdca09a11 to your computer and use it in GitHub Desktop.
Imprint and restore default values
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
"""Store and restore default values | |
Current keyable values are stored in a `defaults` attribute | |
within each node using `store()` and later restored using | |
`restore()`. This way, channels which could otherwise not | |
have a native default value (such as plain transform and rotate) | |
may carry defaults alongside user-defined attributes. | |
""" | |
import json | |
def store(node): | |
"""Store current values as default values""" | |
defaults = dict() | |
for attr in cmds.listAttr(node, keyable=True): | |
value = cmds.getAttr("%s.%s" % (node, attr)) | |
defaults[attr] = value | |
defaults_string = json.dumps(defaults) | |
if not cmds.objExists(node + ".defaults"): | |
cmds.addAttr(node, ln="defaults", dt='string') | |
cmds.setAttr(node + ".defaults", defaults_string, type='string') | |
def store_selected(): | |
for node in cmds.ls(sl=1): | |
store(node) | |
def restore(node): | |
"""Restore defaults from imprint""" | |
defaults = cmds.getAttr(node + ".defaults") | |
defaults_dict = json.loads(defaults) | |
for key, value in defaults_dict.iteritems(): | |
cmds.setAttr(node + ".%s" % key, value) | |
def restore_selected(): | |
for node in cmds.ls(sl=1): | |
restore(node) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment