import sys import importlib import os from maya import cmds def get_all_py_files(start_path): """ Recursively get all .py files in the specified directory Returns: A list containing paths of all .py files """ py_files = [] for root, dirs, files in os.walk(start_path): # Skip __pycache__ directory if '__pycache__' in dirs: dirs.remove('__pycache__') for file in files: if file.endswith('.py') and not file.startswith('__'): full_path = os.path.join(root, file) # Convert to module path format rel_path = os.path.relpath(full_path, start_path) module_path = os.path.splitext(rel_path)[0].replace(os.sep, '.') module_path = module_path.replace("Scripts.", "") py_files.append(module_path) return py_files def reload_metabox(): """ Reload MetaBox and all its related modules """ try: # Close existing MetaBox window if cmds.workspaceControl("ToolBoxWorkSpaceControl", exists=True): cmds.deleteUI("ToolBoxWorkSpaceControl", control=True) # Get Scripts root directory SCRIPTS_PATH = os.path.dirname(os.path.dirname(__file__)) print(SCRIPTS_PATH) # Get module paths for all .py files modules_to_reload = get_all_py_files(SCRIPTS_PATH) # Sort by module hierarchy to ensure lower-level modules are reloaded first modules_to_reload.sort(key=lambda x: len(x.split('.'))) print(modules_to_reload) print("Starting to reload modules...") # Reload all modules reloaded_modules = set() for module_name in modules_to_reload: try: if module_name in sys.modules: module = sys.modules[module_name] importlib.reload(module) reloaded_modules.add(module_name) print(f"Module reloaded: {module_name}") except Exception as e: print(f"Error reloading module {module_name}: {str(e)}") print(f"\nSuccessfully reloaded {len(reloaded_modules)} modules") # Finally reload and display MetaBox if 'MetaBox' in sys.modules: importlib.reload(sys.modules['MetaBox']) from MetaBox import show show() print("\nMetaBox has been successfully reloaded!") except Exception as e: error_msg = f"Error occurred while reloading MetaBox: {str(e)}" print(error_msg) cmds.warning(error_msg) def reload_all(): """ Execute reload and print separator lines """ print("\n" + "="*50) print("Starting to reload MetaBox and all its modules") print("="*50 + "\n") reload_metabox() print("\n" + "="*50) print("Reload completed") print("="*50 + "\n") # Execute reload # if __name__ == "__main__": # reload_all()