1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
from maya import cmds import os import json import pprint
USERAPPDIR = cmds.internalVar(userAppDir = True)
DIRECTORY = os.path.join(USERAPPDIR,'controllerLibrary')
def creatDirectory(directory=DIRECTORY):
if not os.path.exists(directory): os.mkdir(directory)
class ControllerLibrary(dict): """ # ControllerLibrary 字典保存格式
[ (u'test', { 'name': u'test', 'path': u'C:/Users/Administrator/Documents/maya/controllerLibrary\\test.ma', u'screenshot': u'C:/Users/Administrator/Documents/maya/controllerLibrary\\test' }), (u'sphere', { 'name': u'sphere', 'path': u'C:/Users/Administrator/Documents/maya/controllerLibrary\\sphere.ma', u'screenshot': u'C:/Users/Administrator/Documents/maya/controllerLibrary\\sphere' }) ]
"""
def save(self,name,directory=DIRECTORY,screenshot=True,**info):
creatDirectory(directory)
path = os.path.join(directory,'%s.ma' % name)
infoFile = os.path.join(directory,'%s.json' % name)
cmds.file(rename = path)
if
cmds.ls(selection=True): cmds.file( save=True , type='mayaAscii',exportSelection=True) else: cmds.file(save=True,type='mayaAscii',force=True)
if screenshot: info['screenshot'] = self.saveScreenshot(name,directory=directory)
with open(infoFile,'w') as f: json.dump(info,f,indent=4)
self[name] = path return path
def find(self,directory=DIRECTORY):
self.clear() if not os.path.exists(directory): return
files = os.listdir(directory) mayaFiles = [f for f in files if f.endswith('.ma')]
for ma in mayaFiles: name , ext = os.path.splitext(ma) path = os.path.join(directory,ma)
infoFile = '%s.json' % name if infoFile in files: infoFile = os.path.join(directory,infoFile)
with open(infoFile,'r') as f: info = json.load(f)
else: info = {}
screenshot = '%s.jpg' % name
if screenshot in files: info['screenshot'] = os.path.join(directory,name)
info['name'] = name info['path'] = path
self[name] = info
def load(self,name):
path = self[name]['path']
cmds.file(path,i=True,usingNamespaces=False)
def saveScreenshot(self,name,directory=DIRECTORY):
path = os.path.join(directory,'%s.jpg' % name)
cmds.viewFit() cmds.setAttr('defaultRenderGlobals.imageFormat',8)
cmds.playblast(completeFilename=path,forceOverwrite=True,format='image',width=200,height=200,showOrnaments=False,startTime=1,endTime=1,viewer=False)
return path
|