From 7993c0f23c4f18c65489a876d3862d9e21f2a010 Mon Sep 17 00:00:00 2001 From: jeffreytsai1004 Date: Mon, 24 Nov 2025 23:42:22 +0800 Subject: [PATCH] Update --- .../animation_tools/studiolibrary/__init__.py | 103 ++++++++++-------- .../animation_tools/studiolibrary/launcher.py | 45 ++++---- 2 files changed, 80 insertions(+), 68 deletions(-) diff --git a/2023/scripts/animation_tools/studiolibrary/__init__.py b/2023/scripts/animation_tools/studiolibrary/__init__.py index 25202c7..fcef2d3 100644 --- a/2023/scripts/animation_tools/studiolibrary/__init__.py +++ b/2023/scripts/animation_tools/studiolibrary/__init__.py @@ -5,6 +5,10 @@ Studio Library Wrapper Module 用于简化 Studio Library 的导入和使用 支持 Maya 2017-2026+ 所有版本 + +使用方法: + import animation_tools.studiolibrary + animation_tools.studiolibrary.show() """ import sys @@ -13,52 +17,40 @@ import os # 获取当前目录 _current_dir = os.path.dirname(os.path.abspath(__file__)) -# 确保所有子模块路径都在 sys.path 中 -_required_paths = [ - _current_dir, - os.path.join(_current_dir, 'studiolibrary'), - os.path.join(_current_dir, 'studiolibrarymaya'), - os.path.join(_current_dir, 'mutils'), - os.path.join(_current_dir, 'studioqt'), - os.path.join(_current_dir, 'studiovendor'), +# 全局变量用于存储导入的模块 +_studiolibrary_module = None +__version__ = "2.20.2" + +def _ensure_studiolibrary_loaded(): + """确保 studiolibrary 模块已加载""" + global _studiolibrary_module + + if _studiolibrary_module is not None: + return _studiolibrary_module + + # 添加所有必要的路径 + _inner_studiolibrary = os.path.join(_current_dir, 'studiolibrary') + for _subdir in ['studiolibrary', 'studiolibrarymaya', 'mutils', 'studioqt', 'studiovendor']: + _subdir_path = os.path.join(_current_dir, _subdir) + if _subdir_path not in sys.path: + sys.path.insert(0, _subdir_path) + + # 导入 studiolibrary + import studiolibrary + _studiolibrary_module = studiolibrary + return _studiolibrary_module + +def version(): + return __version__ + +# 导出所有公共接口 +__all__ = [ + '__version__', + 'version', + 'show', + 'main', ] -for _path in _required_paths: - if _path not in sys.path: - sys.path.insert(0, _path) - -# 从内层 studiolibrary 模块导入所有功能 -try: - from studiolibrary import ( - __version__, - version, - config, - resource, - Library, - LibraryItem, - main, - ) - - # 导入工具函数 - from studiolibrary.utils import * - - # 导出所有公共接口 - __all__ = [ - '__version__', - 'version', - 'config', - 'resource', - 'Library', - 'LibraryItem', - 'main', - ] - -except ImportError as e: - import traceback - print("Failed to import studiolibrary:") - print(traceback.format_exc()) - raise - def show(*args, **kwargs): """ @@ -70,8 +62,27 @@ def show(*args, **kwargs): Returns: LibraryWindow: Studio Library 窗口实例 + + Example: + >>> import animation_tools.studiolibrary + >>> animation_tools.studiolibrary.show() """ - return main(*args, **kwargs) + lib = _ensure_studiolibrary_loaded() + return lib.main(*args, **kwargs) + +def main(*args, **kwargs): + """ + 启动 Studio Library 主窗口 + + Args: + *args: 传递给 main() 的位置参数 + **kwargs: 传递给 main() 的关键字参数 + + Returns: + LibraryWindow: Studio Library 窗口实例 + """ + lib = _ensure_studiolibrary_loaded() + return lib.main(*args, **kwargs) def isMaya(): @@ -85,5 +96,5 @@ def isMaya(): import maya.cmds maya.cmds.about(batch=True) return True - except ImportError: + except (ImportError, AttributeError): return False diff --git a/2023/scripts/animation_tools/studiolibrary/launcher.py b/2023/scripts/animation_tools/studiolibrary/launcher.py index a4eeb22..10c33c4 100644 --- a/2023/scripts/animation_tools/studiolibrary/launcher.py +++ b/2023/scripts/animation_tools/studiolibrary/launcher.py @@ -23,31 +23,32 @@ def LaunchStudioLibrary(): # 获取 Studio Library 路径 current_dir = os.path.dirname(os.path.abspath(__file__)) - # 确保路径在 sys.path 中 - if current_dir not in sys.path: - sys.path.insert(0, current_dir) - - # 确保子模块路径也在 sys.path 中 + # 关键:直接将内层 studiolibrary 子目录放到 sys.path 最前面 + # 这样 import studiolibrary 会直接找到内层的,避免循环导入 studiolibrary_subdir = os.path.join(current_dir, 'studiolibrary') - if studiolibrary_subdir not in sys.path: - sys.path.insert(0, studiolibrary_subdir) - # 导入 Studio Library - try: - # 方式1:直接从外层包导入 - import studiolibrary - except ImportError: - # 方式2:从子目录导入 - sys.path.insert(0, studiolibrary_subdir) - import studiolibrary + # 临时保存并清理 sys.path,避免外层包干扰 + original_path = sys.path[:] + sys.path.insert(0, studiolibrary_subdir) + + # 添加其他必要的路径 + for subdir in ['studiolibrarymaya', 'mutils', 'studioqt', 'studiovendor']: + subdir_path = os.path.join(current_dir, subdir) + if subdir_path not in sys.path: + sys.path.insert(0, subdir_path) + + # 导入 Studio Library(现在会找到内层的) + import studiolibrary # 打印版本信息 - print(f"Studio Library version: {studiolibrary.version()}") + print("Studio Library version: " + str(studiolibrary.version())) # 检测 Maya 环境 - if studiolibrary.isMaya(): + try: + import maya.cmds + maya.cmds.about(batch=True) print("Studio Library: Running in Maya environment") - else: + except (ImportError, AttributeError): print("Studio Library: Running in standalone mode") # 启动主窗口 @@ -58,16 +59,16 @@ def LaunchStudioLibrary(): return window except Exception as e: - print(f"Failed to launch Studio Library: {e}") + print("Failed to launch Studio Library: " + str(e)) import traceback traceback.print_exc() # 提供详细的调试信息 print("\n=== Debug Information ===") - print(f"Current directory: {os.path.dirname(os.path.abspath(__file__))}") - print(f"sys.path entries:") + print("Current directory: " + os.path.dirname(os.path.abspath(__file__))) + print("sys.path entries:") for i, path in enumerate(sys.path[:10]): - print(f" [{i}] {path}") + print(" [" + str(i) + "] " + path) print("=========================\n") return None