Hi, guys
I still new with python and have learn maya about few month, i have some problem with this code when i watch a tutorial and i cant tell how. Every time i run load function its seem my self[name] is empty. Its not suppose to be like that in the tutorial. So what did I do wrong? Thank a lot
Blockquote
import maya.cmds as cm
import os
import json
import pprint
USERAPPDIR = cm.internalVar(userAppDir=True)
DIRECTORY = os.path.join(USERAPPDIR , ‘controllerLibrary’)
def createDirectory(directory=DIRECTORY):
“”"
Creates the given directory if it’s does’t exist
Args:
directory (string) : The directory to create
"""
if not os.path.exists(directory):
os.mkdir(directory)
class ControllerLibrary(dict):
def save(self , name , directory=DIRECTORY , **info):
createDirectory(directory)
path = os.path.join(directory , '{}.ma'.format(name))
infoFile = os.path.join(directory , '{}.json'.format(name))
info['name'] = name
info['path'] = path
cm.file(rename=path)
if cm.ls(sl=True):
cm.file(force=True , type='mayaAscii' , exportSelected=True)
else:
cm.file(save=True , type='mayaAscii' , force=True)
with open(infoFile , 'w') as f:
json.dump(info , f , indent=4)
self[name] = info
def find(self , directory=DIRECTORY):
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 = '{}.json'.format(name)
if infoFile in files:
infoFile = os.path.join(directory , infoFile)
with open(infoFile , 'r') as f:
info = json.load(f)
else:
info = {}
info['name'] = name
info['path'] = path
self[name] = info
pprint.pprint(self)
def load(self , name):
print (self.get(name))
path = self[name]['path']
cm.file(path, i=True, usingNamespaces=False)