阅读studio_library代码笔记

动态加载包

字符串形式动态导入包
3.png

1
__import__(字符串)

getattr获取对象中的属性

这个found可以是模块,如果在模块中找不到n会引发异常
getattr(x, ‘foobar’) 等同于 x.foobar.如果指定的属性不存在,且提供了 default 值,则返回它.否则触发AttributeError.

1
found = getattr(found, n) 

创建有序字典

目前理解两个字典顺序不一致,==则为false

1
_itemClasses = collections.OrderedDict()

监听关闭maya事件,触发事件(比如保存程序设置)

点我展开看代码

_mayaCloseScriptJob = None
def enableMayaClosedEvent():
"""
Create a Maya script job to trigger on the event "quitApplication".
Enable the Maya closed event to save the library settings on close
:rtype: None
"""
global _mayaCloseScriptJob
if not _mayaCloseScriptJob:
    event = ['quitApplication', mayaClosedEvent]
    try:
        _mayaCloseScriptJob = mutils.ScriptJob(event=event)
        logger.debug("Maya close event enabled")
    except NameError as error:
        logging.exception(error)
def disableMayaClosedEvent():
"""Disable the maya closed event."""
global _mayaCloseScriptJob
if _mayaCloseScriptJob:
    _mayaCloseScriptJob.kill()
    _mayaCloseScriptJob = None
    logger.debug("Maya close event disabled")
def mayaClosedEvent():
"""
Create a Maya script job to trigger on the event "quitApplication".
:rtype: None
"""
for libraryWindow in librarywindow.LibraryWindow.instances():
    libraryWindow.saveSettings()