Skip to content

Instantly share code, notes, and snippets.

@internetimagery
Last active August 29, 2015 14:19
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 internetimagery/a5a962092f8a07c02108 to your computer and use it in GitHub Desktop.
Save internetimagery/a5a962092f8a07c02108 to your computer and use it in GitHub Desktop.
maya fileInfo wrapper in a dict (use like a dictionary)
import maya.cmds as cmds
import collections
import cPickle
class FileInfo(collections.MutableMapping):
"""
Dictionary style interface for fileInfo
"""
def _encode(s, txt):
return cPickle.dumps(txt)
def _decode(s, u):
u = u.decode("unicode_escape")
try:
return cPickle.loads(str(u))
except ValueError:
return u
def _key(s, k):
if k in ["application", "product", "version", "cutIdentifier", "osv", "license"]:
return "%s_" % k
return k
def __init__(s):
s.data = dict()
init = cmds.fileInfo(q=True)
if init:
s.data = dict((k, s._decode(v)) for k, v in (lambda x: zip(x[::2], x[1::2]))(cmds.fileInfo(q=True)))
s.update(s.data)
def __getitem__(s, k):
k = s._key(k)
s.data[k] = s._decode(cmds.fileInfo(k, q=True)[:1])
return s.data[k]
def __setitem__(s, k, v):
k = s._key(k)
cmds.fileInfo(k, s._encode(v))
s.data[k] = v
def __delitem__(s, k):
k = s._key(k)
cmds.fileInfo(rm=k)
del s.data[k]
def __iter__(s):
return iter(s.data)
def __len__(s):
return len(s.data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment