blenderpython/suw_core/test/fix_missing_inits.py

118 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
修复缺失的初始化函数
位置: blenderpython/suw_core/test/fix_missing_inits.py
作者: SUWood Team
版本: 1.0.0
"""
import os
def fix_part_creator():
"""修复 part_creator.py 中缺失的初始化函数"""
# 要添加的代码
additional_code = '''
def get_part_creator_stats(self) -> Dict[str, Any]:
"""获取部件创建器统计信息"""
try:
stats = {
"manager_type": "PartCreator",
"parts_by_uid": {uid: len(parts) for uid, parts in self.parts.items()},
"total_parts": sum(len(parts) for parts in self.parts.values()),
"creation_stats": self.creation_stats.copy(),
"blender_available": BLENDER_AVAILABLE
}
return stats
except Exception as e:
logger.error(f"获取部件创建器统计失败: {e}")
return {"error": str(e)}
# ==================== 模块实例 ====================
# 全局实例将由SUWImpl初始化时设置
part_creator = None
def init_part_creator(suw_impl):
"""初始化部件创建器"""
global part_creator
part_creator = PartCreator(suw_impl)
return part_creator
'''
file_path = "../part_creator.py"
try:
# 读取现有文件
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# 检查是否已经有 init_part_creator 函数
if "def init_part_creator" in content:
print("✅ part_creator.py 已经有初始化函数")
return True
# 查找替换点
old_pattern = '''# ==================== 全局实例 ====================
# 创建全局部件创建器实例
part_creator = PartCreator()'''
if old_pattern in content:
# 替换旧代码
new_content = content.replace(old_pattern, additional_code.strip())
# 写回文件
with open(file_path, 'w', encoding='utf-8') as f:
f.write(new_content)
print("✅ part_creator.py 初始化函数已添加")
return True
else:
# 如果找不到替换点,直接追加
with open(file_path, 'a', encoding='utf-8') as f:
f.write(additional_code)
print("✅ part_creator.py 初始化函数已追加")
return True
except Exception as e:
print(f"❌ 修复 part_creator.py 失败: {e}")
return False
def test_imports():
"""测试导入"""
try:
import sys
sys.path.insert(0, "../..")
from suw_core.part_creator import init_part_creator
print("✅ init_part_creator 导入成功")
from suw_core.material_manager import init_material_manager
print("✅ init_material_manager 导入成功")
return True
except Exception as e:
print(f"❌ 导入测试失败: {e}")
return False
if __name__ == "__main__":
print("🔧 修复缺失的初始化函数")
print("=" * 40)
success1 = fix_part_creator()
success2 = test_imports()
if success1 and success2:
print("\n🎉 所有修复完成!")
else:
print("\n❌ 修复失败")