From 52ac5cf5a6c857ff7d525faa0cfea08b1f8b33ee Mon Sep 17 00:00:00 2001 From: jeffreytsai1004 Date: Sun, 7 Dec 2025 22:50:43 +0800 Subject: [PATCH] UPDATE --- .gitignore | 2 + 2023/scripts/userSetup.py | 78 ++++++++++++++++++++++++++++----------- 2024/scripts/userSetup.py | 70 ++++++++++++++++++++++++++--------- 2025/scripts/userSetup.py | 70 ++++++++++++++++++++++++++--------- modules/ARTv2.mod | 29 +++++++++++++++ 5 files changed, 192 insertions(+), 57 deletions(-) create mode 100644 modules/ARTv2.mod diff --git a/.gitignore b/.gitignore index 7896b34..1639e9a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ # Python __pycache__/ +*/__pycache__/ +__pycache__/* *.py[cod] *$py.class *.so diff --git a/2023/scripts/userSetup.py b/2023/scripts/userSetup.py index dcae919..ae107f0 100644 --- a/2023/scripts/userSetup.py +++ b/2023/scripts/userSetup.py @@ -21,6 +21,30 @@ import os import sys import re +# Set MAYA_MODULE_PATH environment variable for external tools +try: + _current_file = os.path.abspath(__file__) + _script_dir = os.path.dirname(_current_file) + _project_root = os.path.dirname(os.path.dirname(_script_dir)) + _modules_dir = os.path.join(_project_root, 'modules') + + if os.path.exists(_modules_dir): + _modules_dir_normalized = os.path.normpath(_modules_dir) + _current_module_path = os.environ.get('MAYA_MODULE_PATH', '') + + # Check if already in path + _paths = [p.strip() for p in _current_module_path.split(os.pathsep) if p.strip()] + _normalized_paths = [os.path.normpath(p) for p in _paths] + + if _modules_dir_normalized not in _normalized_paths: + if _current_module_path: + os.environ['MAYA_MODULE_PATH'] = f"{_modules_dir_normalized}{os.pathsep}{_current_module_path}" + else: + os.environ['MAYA_MODULE_PATH'] = _modules_dir_normalized + print(f"[Tool] MAYA_MODULE_PATH set to: {_modules_dir_normalized}") +except Exception as e: + print(f"[Tool] Warning: Could not set MAYA_MODULE_PATH: {e}") + # Silently try to open default commandPort to avoid startup error if it's already in use try: mel.eval('catchQuiet("commandPort -securityWarning -name \\"commandportDefault\\";");') @@ -352,20 +376,20 @@ def load_tool_plugins(): _tool_log(f"[Tool] Processing plugin: {plugin_name}") - # 首先检查配置的 path 参数 + # First check the configured path parameter plugin_path = None if plugin_rel_path: - # 构建完整路径:script_dir/path/plugin_name + # Build the full path: script_dir/path/plugin_name full_path = _norm_path(os.path.join(script_dir, plugin_rel_path, plugin_name)) if os.path.exists(full_path): plugin_path = full_path _tool_log(f"[Tool] Found plugin at configured path: {plugin_path}") - # 如果配置路径没找到,搜索环境变量路径 + # If the configured path is not found, search the environment variable path. if not plugin_path: plugin_path = _find_file_in_paths(plugin_name, os.environ.get(env_var, '').split(os.pathsep)) - # 最后尝试脚本目录和父级 plug-ins 文件夹 + # Finally, try the script directory and the parent plug-ins folder. if not plugin_path: search_dirs = [ script_dir, @@ -454,10 +478,10 @@ def load_tool_scripts(): def load_project_modules(): """从 modules 目录加载 .mod 文件定义的插件""" try: - # 获取当前 Maya 版本 + # Get current Maya version maya_version = int(cmds.about(version=True).split()[0]) - # 获取项目根目录 + # Get project root directory script_dir = _get_script_dir() project_root = os.path.dirname(os.path.dirname(script_dir)) modules_dir = _norm_path(os.path.join(project_root, "modules")) @@ -466,7 +490,7 @@ def load_project_modules(): _tool_log(f"[Tool] Modules directory not found: {modules_dir}") return - # 查找所有 .mod 文件 + # Find all .mod files mod_files = [f for f in os.listdir(modules_dir) if f.endswith('.mod')] if not mod_files: @@ -475,7 +499,7 @@ def load_project_modules(): _tool_print(f"[Tool] Found {len(mod_files)} module(s): {', '.join(mod_files)}") - # 解析 .mod 文件并加载插件 + # Parse .mod files and load plugins. plugins_loaded = 0 for mod_file in mod_files: mod_path = os.path.join(modules_dir, mod_file) @@ -488,36 +512,48 @@ def load_project_modules(): with open(mod_path, 'r', encoding='utf-8') as f: for line in f: line = line.strip() - - # 跳过注释和空行 + + # Skip comments and empty lines if not line or line.startswith('#') or line.startswith('//'): continue - - # 解析模块定义行(以 + 开头) + + # Parse module definition lines (starting with +) if line.startswith('+'): parts = line.split() in_correct_version = False current_module_root = None - - if len(parts) >= 5: - # 检查版本匹配 + + if len(parts) >= 4: + # Check for version restrictions. + has_version_requirement = False for part in parts: if part.startswith('MAYAVERSION:'): + has_version_requirement = True required_version = int(part.split(':')[1]) if required_version == maya_version: in_correct_version = True - # 获取模块根路径(最后一个参数) + # Get the module root path (last parameter) current_module_root = parts[-1] if current_module_root.startswith('..'): current_module_root = _norm_path(os.path.join(modules_dir, current_module_root)) _tool_log(f"[Tool] Version matched for Maya {maya_version}: {current_module_root}") break - - # 只处理匹配版本的 MAYA_PLUG_IN_PATH + + # If there are no version restrictions, it works for all versions. + if not has_version_requirement: + in_correct_version = True + current_module_root = parts[-1] + if current_module_root.startswith('..'): + current_module_root = _norm_path(os.path.join(modules_dir, current_module_root)) + _tool_log(f"[Tool] Module without version restriction loaded: {current_module_root}") + + # Only process MAYA_PLUG_IN_PATH that matches the version elif line.startswith('MAYA_PLUG_IN_PATH') and in_correct_version and current_module_root: if '+:=' in line or '+=' in line: plugin_path_relative = line.split('=')[-1].strip() + plugins_dir = _norm_path(os.path.join(current_module_root, plugin_path_relative)) + if os.path.exists(plugins_dir): _tool_log(f"[Tool] Checking plugin dir: {plugins_dir}") @@ -527,7 +563,7 @@ def load_project_modules(): plugin_name = os.path.splitext(plugin_file)[0] plugin_full_path = os.path.join(plugins_dir, plugin_file) - # 检查是否已加载 + # Check if it has been loaded if plugin_name in _LOADED_PLUGINS: continue @@ -600,11 +636,11 @@ def initialize_tool(): if not _check_maya_version(): print("[Tool] Warning: Running on unsupported Maya version") - # 先设置图标路径,这样 shelf 加载时就能找到图标 + # Set icon paths first, so shelves can find icons setup_icon_paths() # Load project modules - load_project_modules() + load_project_modules() # Automatic loading using Maya standard module system # Setup command port setup_command_port() diff --git a/2024/scripts/userSetup.py b/2024/scripts/userSetup.py index 1c974d5..701278d 100644 --- a/2024/scripts/userSetup.py +++ b/2024/scripts/userSetup.py @@ -21,6 +21,30 @@ import os import sys import re +# Set MAYA_MODULE_PATH environment variable for external tools +try: + _current_file = os.path.abspath(__file__) + _script_dir = os.path.dirname(_current_file) + _project_root = os.path.dirname(os.path.dirname(_script_dir)) + _modules_dir = os.path.join(_project_root, 'modules') + + if os.path.exists(_modules_dir): + _modules_dir_normalized = os.path.normpath(_modules_dir) + _current_module_path = os.environ.get('MAYA_MODULE_PATH', '') + + # Check if already in path + _paths = [p.strip() for p in _current_module_path.split(os.pathsep) if p.strip()] + _normalized_paths = [os.path.normpath(p) for p in _paths] + + if _modules_dir_normalized not in _normalized_paths: + if _current_module_path: + os.environ['MAYA_MODULE_PATH'] = f"{_modules_dir_normalized}{os.pathsep}{_current_module_path}" + else: + os.environ['MAYA_MODULE_PATH'] = _modules_dir_normalized + print(f"[Tool] MAYA_MODULE_PATH set to: {_modules_dir_normalized}") +except Exception as e: + print(f"[Tool] Warning: Could not set MAYA_MODULE_PATH: {e}") + # Silently try to open default commandPort to avoid startup error if it's already in use try: mel.eval('catchQuiet("commandPort -securityWarning -name \\"commandportDefault\\";");') @@ -352,20 +376,20 @@ def load_tool_plugins(): _tool_log(f"[Tool] Processing plugin: {plugin_name}") - # 首先检查配置的 path 参数 + # First, check the configured path parameter. plugin_path = None if plugin_rel_path: - # 构建完整路径:script_dir/path/plugin_name + # Build full path: script_dir/path/plugin_name full_path = _norm_path(os.path.join(script_dir, plugin_rel_path, plugin_name)) if os.path.exists(full_path): plugin_path = full_path _tool_log(f"[Tool] Found plugin at configured path: {plugin_path}") - # 如果配置路径没找到,搜索环境变量路径 + # If the configuration path is not found, search the environment variable path. if not plugin_path: plugin_path = _find_file_in_paths(plugin_name, os.environ.get(env_var, '').split(os.pathsep)) - # 最后尝试脚本目录和父级 plug-ins 文件夹 + # Try the script directory and parent plug-ins folder last. if not plugin_path: search_dirs = [ script_dir, @@ -452,12 +476,12 @@ def load_tool_scripts(): _tool_print(f"[Tool] Error loading scripts: {e}") def load_project_modules(): - """从 modules 目录加载 .mod 文件定义的插件""" + """Load plugins defined in .mod files from the modules directory.""" try: - # 获取当前 Maya 版本 + # Get current Maya version maya_version = int(cmds.about(version=True).split()[0]) - # 获取项目根目录 + # Get project root directory script_dir = _get_script_dir() project_root = os.path.dirname(os.path.dirname(script_dir)) modules_dir = _norm_path(os.path.join(project_root, "modules")) @@ -466,7 +490,7 @@ def load_project_modules(): _tool_log(f"[Tool] Modules directory not found: {modules_dir}") return - # 查找所有 .mod 文件 + # Find all .mod files mod_files = [f for f in os.listdir(modules_dir) if f.endswith('.mod')] if not mod_files: @@ -475,7 +499,7 @@ def load_project_modules(): _tool_print(f"[Tool] Found {len(mod_files)} module(s): {', '.join(mod_files)}") - # 解析 .mod 文件并加载插件 + # Parse .mod files and load plugins. plugins_loaded = 0 for mod_file in mod_files: mod_path = os.path.join(modules_dir, mod_file) @@ -489,31 +513,41 @@ def load_project_modules(): for line in f: line = line.strip() - # 跳过注释和空行 + # Skip comments and empty lines. if not line or line.startswith('#') or line.startswith('//'): continue - # 解析模块定义行(以 + 开头) + # Parse module definition lines (starting with +) if line.startswith('+'): parts = line.split() in_correct_version = False current_module_root = None - if len(parts) >= 5: - # 检查版本匹配 + if len(parts) >= 4: + # Check for version requirements. + has_version_requirement = False for part in parts: if part.startswith('MAYAVERSION:'): + has_version_requirement = True required_version = int(part.split(':')[1]) if required_version == maya_version: in_correct_version = True - # 获取模块根路径(最后一个参数) + # Get the module root path (last parameter) current_module_root = parts[-1] if current_module_root.startswith('..'): current_module_root = _norm_path(os.path.join(modules_dir, current_module_root)) _tool_log(f"[Tool] Version matched for Maya {maya_version}: {current_module_root}") break + + # If there is no version requirement, it applies to all versions. + if not has_version_requirement: + in_correct_version = True + current_module_root = parts[-1] + if current_module_root.startswith('..'): + current_module_root = _norm_path(os.path.join(modules_dir, current_module_root)) + _tool_log(f"[Tool] Module without version restriction loaded: {current_module_root}") - # 只处理匹配版本的 MAYA_PLUG_IN_PATH + # Only process matching version MAYA_PLUG_IN_PATH elif line.startswith('MAYA_PLUG_IN_PATH') and in_correct_version and current_module_root: if '+:=' in line or '+=' in line: plugin_path_relative = line.split('=')[-1].strip() @@ -527,7 +561,7 @@ def load_project_modules(): plugin_name = os.path.splitext(plugin_file)[0] plugin_full_path = os.path.join(plugins_dir, plugin_file) - # 检查是否已加载 + # Check if already loaded. if plugin_name in _LOADED_PLUGINS: continue @@ -600,11 +634,11 @@ def initialize_tool(): if not _check_maya_version(): print("[Tool] Warning: Running on unsupported Maya version") - # 先设置图标路径,这样 shelf 加载时就能找到图标 + # Set icon paths first, so shelves can find icons setup_icon_paths() # Load project modules - load_project_modules() + load_project_modules() # Automatic loading using Maya standard module system # Setup command port setup_command_port() diff --git a/2025/scripts/userSetup.py b/2025/scripts/userSetup.py index cac1bf4..e0d7402 100644 --- a/2025/scripts/userSetup.py +++ b/2025/scripts/userSetup.py @@ -21,6 +21,30 @@ import os import sys import re +# Set MAYA_MODULE_PATH environment variable for external tools +try: + _current_file = os.path.abspath(__file__) + _script_dir = os.path.dirname(_current_file) + _project_root = os.path.dirname(os.path.dirname(_script_dir)) + _modules_dir = os.path.join(_project_root, 'modules') + + if os.path.exists(_modules_dir): + _modules_dir_normalized = os.path.normpath(_modules_dir) + _current_module_path = os.environ.get('MAYA_MODULE_PATH', '') + + # Check if already in path + _paths = [p.strip() for p in _current_module_path.split(os.pathsep) if p.strip()] + _normalized_paths = [os.path.normpath(p) for p in _paths] + + if _modules_dir_normalized not in _normalized_paths: + if _current_module_path: + os.environ['MAYA_MODULE_PATH'] = f"{_modules_dir_normalized}{os.pathsep}{_current_module_path}" + else: + os.environ['MAYA_MODULE_PATH'] = _modules_dir_normalized + print(f"[Tool] MAYA_MODULE_PATH set to: {_modules_dir_normalized}") +except Exception as e: + print(f"[Tool] Warning: Could not set MAYA_MODULE_PATH: {e}") + # Silently try to open default commandPort to avoid startup error if it's already in use try: mel.eval('catchQuiet("commandPort -securityWarning -name \\"commandportDefault\\";");') @@ -352,20 +376,20 @@ def load_tool_plugins(): _tool_log(f"[Tool] Processing plugin: {plugin_name}") - # 首先检查配置的 path 参数 + # First check the configured path parameter plugin_path = None if plugin_rel_path: - # 构建完整路径:script_dir/path/plugin_name + # Build full path: script_dir/path/plugin_name full_path = _norm_path(os.path.join(script_dir, plugin_rel_path, plugin_name)) if os.path.exists(full_path): plugin_path = full_path _tool_log(f"[Tool] Found plugin at configured path: {plugin_path}") - # 如果配置路径没找到,搜索环境变量路径 + # If the configuration path is not found, search the environment variable path. if not plugin_path: plugin_path = _find_file_in_paths(plugin_name, os.environ.get(env_var, '').split(os.pathsep)) - # 最后尝试脚本目录和父级 plug-ins 文件夹 + # Finally, try the script directory and the parent plug-ins folder. if not plugin_path: search_dirs = [ script_dir, @@ -451,12 +475,12 @@ def load_tool_scripts(): _tool_print(f"[Tool] Error loading scripts: {e}") def load_project_modules(): - """从 modules 目录加载 .mod 文件定义的插件""" + """Load plugins defined in .mod files from the modules directory.""" try: - # 获取当前 Maya 版本 + # Get current Maya version maya_version = int(cmds.about(version=True).split()[0]) - # 获取项目根目录 + # Get project root directory script_dir = _get_script_dir() project_root = os.path.dirname(os.path.dirname(script_dir)) modules_dir = _norm_path(os.path.join(project_root, "modules")) @@ -465,7 +489,7 @@ def load_project_modules(): _tool_log(f"[Tool] Modules directory not found: {modules_dir}") return - # 查找所有 .mod 文件 + # Find all .mod files mod_files = [f for f in os.listdir(modules_dir) if f.endswith('.mod')] if not mod_files: @@ -474,7 +498,7 @@ def load_project_modules(): _tool_print(f"[Tool] Found {len(mod_files)} module(s): {', '.join(mod_files)}") - # 解析 .mod 文件并加载插件 + # Parse .mod files and load plugins plugins_loaded = 0 for mod_file in mod_files: mod_path = os.path.join(modules_dir, mod_file) @@ -488,31 +512,41 @@ def load_project_modules(): for line in f: line = line.strip() - # 跳过注释和空行 + # Skip comments and empty lines if not line or line.startswith('#') or line.startswith('//'): continue - # 解析模块定义行(以 + 开头) + # Parse module definition lines (starting with +) if line.startswith('+'): parts = line.split() in_correct_version = False current_module_root = None - if len(parts) >= 5: - # 检查版本匹配 + if len(parts) >= 4: + # Check for version restrictions + has_version_requirement = False for part in parts: if part.startswith('MAYAVERSION:'): + has_version_requirement = True required_version = int(part.split(':')[1]) if required_version == maya_version: in_correct_version = True - # 获取模块根路径(最后一个参数) + # Get the module root path (last parameter) current_module_root = parts[-1] if current_module_root.startswith('..'): current_module_root = _norm_path(os.path.join(modules_dir, current_module_root)) _tool_log(f"[Tool] Version matched for Maya {maya_version}: {current_module_root}") break + + # If there are no version restrictions, it works for all versions. + if not has_version_requirement: + in_correct_version = True + current_module_root = parts[-1] + if current_module_root.startswith('..'): + current_module_root = _norm_path(os.path.join(modules_dir, current_module_root)) + _tool_log(f"[Tool] Module without version restriction loaded: {current_module_root}") - # 只处理匹配版本的 MAYA_PLUG_IN_PATH + # Only process MAYA_PLUG_IN_PATH that matches the version. elif line.startswith('MAYA_PLUG_IN_PATH') and in_correct_version and current_module_root: if '+:=' in line or '+=' in line: plugin_path_relative = line.split('=')[-1].strip() @@ -526,7 +560,7 @@ def load_project_modules(): plugin_name = os.path.splitext(plugin_file)[0] plugin_full_path = os.path.join(plugins_dir, plugin_file) - # 检查是否已加载 + # Check if already loaded if plugin_name in _LOADED_PLUGINS: continue @@ -599,11 +633,11 @@ def initialize_tool(): if not _check_maya_version(): print("[Tool] Warning: Running on unsupported Maya version") - # 先设置图标路径,这样 shelf 加载时就能找到图标 + # First, set the icon path so that the shelf can find the icon when it loads. setup_icon_paths() # Load project modules - load_project_modules() + load_project_modules() # Use Maya standard module systems for automatic loading. # Setup command port setup_command_port() diff --git a/modules/ARTv2.mod b/modules/ARTv2.mod new file mode 100644 index 0000000..e855d13 --- /dev/null +++ b/modules/ARTv2.mod @@ -0,0 +1,29 @@ ++ MAYAVERSION:2020 PLATFORM:win64 ARTv2 1.0 ../plug-ins/ARTv2 +MAYA_PLUG_IN_PATH+:=plug-ins +MAYA_SCRIPT_PATH+:=Core/Scripts +XBMLANGPATH+:=Core/Icons + ++ MAYAVERSION:2021 PLATFORM:win64 ARTv2 1.0 ../plug-ins/ARTv2 +MAYA_PLUG_IN_PATH+:=plug-ins +MAYA_SCRIPT_PATH+:=Core/Scripts +XBMLANGPATH+:=Core/Icons + ++ MAYAVERSION:2022 PLATFORM:win64 ARTv2 1.0 ../plug-ins/ARTv2 +MAYA_PLUG_IN_PATH+:=plug-ins +MAYA_SCRIPT_PATH+:=Core/Scripts +XBMLANGPATH+:=Core/Icons + ++ MAYAVERSION:2023 PLATFORM:win64 ARTv2 1.0 ../plug-ins/ARTv2 +MAYA_PLUG_IN_PATH+:=plug-ins +MAYA_SCRIPT_PATH+:=Core/Scripts +XBMLANGPATH+:=Core/Icons + ++ MAYAVERSION:2024 PLATFORM:win64 ARTv2 1.0 ../plug-ins/ARTv2 +MAYA_PLUG_IN_PATH+:=plug-ins +MAYA_SCRIPT_PATH+:=Core/Scripts +XBMLANGPATH+:=Core/Icons + ++ MAYAVERSION:2025 PLATFORM:win64 ARTv2 1.0 ../plug-ins/ARTv2 +MAYA_PLUG_IN_PATH+:=plug-ins +MAYA_SCRIPT_PATH+:=Core/Scripts +XBMLANGPATH+:=Core/Icons