Problem with class(dict) and self

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)

it works for me

cl = ControllerLibrary()
cl.save('hello')
print cl['hello']

result

{'path': u'C:/Users/joey/Documents/maya/controllerLibrary\\hello.ma', 'name': 'hello'}

Personal note - sub classing dict is a strange choice. Maybe there’s some functionality he’s planning to add? For what it is currently, I would not sub class dict and would change self[name] to self.name.

self[name] >> self.name
print (self.get(name)) >> print self.name
path = self[name]['path'] >> path = self.name['path']
cl = ControllerLibrary()
cl.save('hello')
print cl.name
2 Likes

Subclassing from dict comes with all sorts of problems: The problem with inheriting from dict and list in Python

It is recommended to subclass from abc’s MutableMapping and implement the abstract methods yourself, if you would like to continue with the dict approach: collections.abc — Abstract Base Classes for Containers — Python 3.12.6 documentation

2 Likes

Thank a lots for answer, take me sometime to understand, this is kind of out of my league :slight_smile: , but i’m addicted