317 lines
8.5 KiB
Python
317 lines
8.5 KiB
Python
|
#!/usr/bin/env python3
|
|||
|
# -*- coding: utf-8 -*-
|
|||
|
"""
|
|||
|
Blender中测试SUWImpl功能的脚本
|
|||
|
使用方式:在Blender的Python控制台中运行这个脚本
|
|||
|
"""
|
|||
|
|
|||
|
import sys
|
|||
|
import os
|
|||
|
|
|||
|
# 添加当前目录到Python路径
|
|||
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|||
|
if current_dir not in sys.path:
|
|||
|
sys.path.append(current_dir)
|
|||
|
|
|||
|
try:
|
|||
|
import bpy
|
|||
|
print("✅ Blender API 可用")
|
|||
|
BLENDER_AVAILABLE = True
|
|||
|
except ImportError:
|
|||
|
print("⚠️ Blender API 不可用,使用模拟模式")
|
|||
|
BLENDER_AVAILABLE = False
|
|||
|
|
|||
|
|
|||
|
def clear_scene():
|
|||
|
"""清空场景"""
|
|||
|
if BLENDER_AVAILABLE:
|
|||
|
# 删除所有网格对象
|
|||
|
bpy.ops.object.select_all(action='SELECT')
|
|||
|
bpy.ops.object.delete(use_global=False)
|
|||
|
|
|||
|
# 删除所有材质
|
|||
|
for material in bpy.data.materials:
|
|||
|
bpy.data.materials.remove(material)
|
|||
|
|
|||
|
# 删除所有集合
|
|||
|
for collection in bpy.data.collections:
|
|||
|
bpy.data.collections.remove(collection)
|
|||
|
|
|||
|
print("🧹 场景已清空")
|
|||
|
|
|||
|
|
|||
|
def test_basic_import():
|
|||
|
"""测试基本导入"""
|
|||
|
try:
|
|||
|
from suw_impl import SUWImpl, Point3d, Vector3d, Transformation
|
|||
|
print("✅ 成功导入 SUWImpl 和几何类")
|
|||
|
return True
|
|||
|
except Exception as e:
|
|||
|
print(f"❌ 导入失败: {e}")
|
|||
|
return False
|
|||
|
|
|||
|
|
|||
|
def test_suw_impl_initialization():
|
|||
|
"""测试SUWImpl初始化"""
|
|||
|
try:
|
|||
|
from suw_impl import SUWImpl
|
|||
|
|
|||
|
# 获取实例
|
|||
|
impl = SUWImpl.get_instance()
|
|||
|
print(f"✅ SUWImpl 实例创建成功: {type(impl)}")
|
|||
|
|
|||
|
# 初始化
|
|||
|
impl.startup()
|
|||
|
print("✅ SUWImpl 启动成功")
|
|||
|
|
|||
|
return impl
|
|||
|
except Exception as e:
|
|||
|
print(f"❌ SUWImpl 初始化失败: {e}")
|
|||
|
return None
|
|||
|
|
|||
|
|
|||
|
def test_geometry_classes():
|
|||
|
"""测试几何类"""
|
|||
|
try:
|
|||
|
from suw_impl import Point3d, Vector3d, Transformation
|
|||
|
|
|||
|
# 测试Point3d
|
|||
|
p1 = Point3d(1.0, 2.0, 3.0)
|
|||
|
p2 = Point3d.parse("4.5,5.6,6.7")
|
|||
|
print(f"✅ Point3d 测试: {p1}, {p2}")
|
|||
|
|
|||
|
# 测试Vector3d
|
|||
|
v1 = Vector3d(1.0, 0.0, 0.0)
|
|||
|
v2 = Vector3d.parse("0,1,0")
|
|||
|
v3 = v1.normalize()
|
|||
|
print(f"✅ Vector3d 测试: {v1}, {v2}, normalized: {v3}")
|
|||
|
|
|||
|
# 测试Transformation
|
|||
|
t1 = Transformation(p1, v1, v2, Vector3d(0, 0, 1))
|
|||
|
print(f"✅ Transformation 测试: origin={t1.origin}")
|
|||
|
|
|||
|
return True
|
|||
|
except Exception as e:
|
|||
|
print(f"❌ 几何类测试失败: {e}")
|
|||
|
return False
|
|||
|
|
|||
|
|
|||
|
def test_basic_commands():
|
|||
|
"""测试基本命令"""
|
|||
|
try:
|
|||
|
from suw_impl import SUWImpl
|
|||
|
|
|||
|
impl = SUWImpl.get_instance()
|
|||
|
|
|||
|
# 测试c00命令(清空选择)
|
|||
|
test_data = {"uid": "test_uid_001"}
|
|||
|
impl.c00(test_data)
|
|||
|
print("✅ c00 命令测试成功")
|
|||
|
|
|||
|
# 测试c01命令(单位设置)
|
|||
|
test_data = {
|
|||
|
"uid": "test_uid_001",
|
|||
|
"unit_drawing": "test_drawing.dwg",
|
|||
|
"drawing_name": "测试图纸"
|
|||
|
}
|
|||
|
impl.c01(test_data)
|
|||
|
print("✅ c01 命令测试成功")
|
|||
|
|
|||
|
return True
|
|||
|
except Exception as e:
|
|||
|
print(f"❌ 基本命令测试失败: {e}")
|
|||
|
return False
|
|||
|
|
|||
|
|
|||
|
def test_geometry_creation():
|
|||
|
"""测试几何创建"""
|
|||
|
try:
|
|||
|
from suw_impl import SUWImpl, Point3d
|
|||
|
|
|||
|
impl = SUWImpl.get_instance()
|
|||
|
|
|||
|
# 创建测试surface数据
|
|||
|
test_surface = {
|
|||
|
"segs": [
|
|||
|
["0,0,0", "100,0,0"], # 底边
|
|||
|
["100,0,0", "100,100,0"], # 右边
|
|||
|
["100,100,0", "0,100,0"], # 顶边
|
|||
|
["0,100,0", "0,0,0"] # 左边
|
|||
|
],
|
|||
|
"vx": "1,0,0",
|
|||
|
"vy": "0,1,0",
|
|||
|
"vz": "0,0,1"
|
|||
|
}
|
|||
|
|
|||
|
# 测试create_face方法
|
|||
|
if hasattr(impl, 'create_face'):
|
|||
|
face = impl.create_face(None, test_surface, "mat_normal")
|
|||
|
if face:
|
|||
|
print("✅ create_face 测试成功")
|
|||
|
else:
|
|||
|
print("⚠️ create_face 返回 None")
|
|||
|
else:
|
|||
|
print("⚠️ create_face 方法不存在")
|
|||
|
|
|||
|
return True
|
|||
|
except Exception as e:
|
|||
|
print(f"❌ 几何创建测试失败: {e}")
|
|||
|
return False
|
|||
|
|
|||
|
|
|||
|
def test_material_system():
|
|||
|
"""测试材质系统"""
|
|||
|
try:
|
|||
|
from suw_impl import SUWImpl
|
|||
|
|
|||
|
impl = SUWImpl.get_instance()
|
|||
|
|
|||
|
# 测试添加材质
|
|||
|
impl.add_mat_rgb("test_mat", 1.0, 255, 128, 64)
|
|||
|
print("✅ 材质添加测试成功")
|
|||
|
|
|||
|
# 测试获取纹理
|
|||
|
texture = impl.get_texture("mat_normal")
|
|||
|
print(f"✅ 纹理获取测试: {texture}")
|
|||
|
|
|||
|
return True
|
|||
|
except Exception as e:
|
|||
|
print(f"❌ 材质系统测试失败: {e}")
|
|||
|
return False
|
|||
|
|
|||
|
|
|||
|
def create_test_scene():
|
|||
|
"""创建测试场景"""
|
|||
|
if not BLENDER_AVAILABLE:
|
|||
|
print("⚠️ 非Blender环境,跳过场景创建")
|
|||
|
return
|
|||
|
|
|||
|
try:
|
|||
|
# 添加一些基本几何体作为测试
|
|||
|
bpy.ops.mesh.primitive_cube_add(location=(0, 0, 0))
|
|||
|
cube = bpy.context.active_object
|
|||
|
cube.name = "SUW_Test_Cube"
|
|||
|
|
|||
|
# 创建测试材质
|
|||
|
mat = bpy.data.materials.new(name="SUW_Test_Material")
|
|||
|
mat.use_nodes = True
|
|||
|
|
|||
|
# 安全设置材质颜色,兼容不同Blender版本
|
|||
|
try:
|
|||
|
# 尝试找到Principled BSDF节点
|
|||
|
bsdf = None
|
|||
|
for node in mat.node_tree.nodes:
|
|||
|
if "BSDF" in node.type:
|
|||
|
bsdf = node
|
|||
|
break
|
|||
|
|
|||
|
if bsdf and hasattr(bsdf, 'inputs'):
|
|||
|
# 尝试设置基础颜色
|
|||
|
if "Base Color" in bsdf.inputs:
|
|||
|
bsdf.inputs["Base Color"].default_value = (
|
|||
|
0.8, 0.2, 0.2, 1.0)
|
|||
|
elif len(bsdf.inputs) > 0:
|
|||
|
bsdf.inputs[0].default_value = (0.8, 0.2, 0.2, 1.0)
|
|||
|
except Exception as e:
|
|||
|
print(f"⚠️ 设置材质颜色失败: {e}")
|
|||
|
|
|||
|
cube.data.materials.append(mat)
|
|||
|
|
|||
|
print("✅ 测试场景创建成功")
|
|||
|
|
|||
|
except Exception as e:
|
|||
|
print(f"❌ 测试场景创建失败: {e}")
|
|||
|
|
|||
|
|
|||
|
def run_comprehensive_test():
|
|||
|
"""运行完整测试"""
|
|||
|
print("🚀 开始 SUWImpl Blender 测试")
|
|||
|
print("=" * 50)
|
|||
|
|
|||
|
# 清空场景
|
|||
|
clear_scene()
|
|||
|
|
|||
|
# 测试步骤
|
|||
|
tests = [
|
|||
|
("基本导入", test_basic_import),
|
|||
|
("几何类", test_geometry_classes),
|
|||
|
("SUWImpl初始化", test_suw_impl_initialization),
|
|||
|
("基本命令", test_basic_commands),
|
|||
|
("材质系统", test_material_system),
|
|||
|
("几何创建", test_geometry_creation),
|
|||
|
]
|
|||
|
|
|||
|
results = []
|
|||
|
for test_name, test_func in tests:
|
|||
|
print(f"\n📝 测试: {test_name}")
|
|||
|
print("-" * 30)
|
|||
|
try:
|
|||
|
result = test_func()
|
|||
|
results.append((test_name, result))
|
|||
|
if result:
|
|||
|
print(f"✅ {test_name} 测试通过")
|
|||
|
else:
|
|||
|
print(f"❌ {test_name} 测试失败")
|
|||
|
except Exception as e:
|
|||
|
print(f"💥 {test_name} 测试异常: {e}")
|
|||
|
results.append((test_name, False))
|
|||
|
|
|||
|
# 创建测试场景
|
|||
|
print(f"\n📝 测试: 场景创建")
|
|||
|
print("-" * 30)
|
|||
|
create_test_scene()
|
|||
|
|
|||
|
# 总结
|
|||
|
print("\n" + "=" * 50)
|
|||
|
print("📊 测试结果总结:")
|
|||
|
passed = sum(1 for _, result in results if result)
|
|||
|
total = len(results)
|
|||
|
|
|||
|
for test_name, result in results:
|
|||
|
status = "✅ 通过" if result else "❌ 失败"
|
|||
|
print(f" {test_name}: {status}")
|
|||
|
|
|||
|
print(f"\n🎯 总体结果: {passed}/{total} 测试通过")
|
|||
|
|
|||
|
if passed == total:
|
|||
|
print("🎉 所有测试通过!SUWImpl 在 Blender 中运行正常")
|
|||
|
else:
|
|||
|
print("⚠️ 部分测试失败,需要进一步调试")
|
|||
|
|
|||
|
|
|||
|
def quick_demo():
|
|||
|
"""快速演示"""
|
|||
|
print("🎭 SUWImpl 快速演示")
|
|||
|
print("=" * 30)
|
|||
|
|
|||
|
try:
|
|||
|
from suw_impl import SUWImpl, Point3d, Vector3d
|
|||
|
|
|||
|
# 创建实例
|
|||
|
impl = SUWImpl.get_instance()
|
|||
|
impl.startup()
|
|||
|
|
|||
|
# 演示几何类
|
|||
|
p1 = Point3d(0, 0, 0)
|
|||
|
p2 = Point3d(10, 10, 10)
|
|||
|
v1 = Vector3d(1, 0, 0)
|
|||
|
|
|||
|
print(f"📍 点1: {p1}")
|
|||
|
print(f"📍 点2: {p2}")
|
|||
|
print(f"📏 向量: {v1}")
|
|||
|
print(f"📏 归一化向量: {v1.normalize()}")
|
|||
|
|
|||
|
# 演示命令
|
|||
|
test_data = {"uid": "demo_001"}
|
|||
|
impl.c00(test_data)
|
|||
|
|
|||
|
print("✅ 快速演示完成")
|
|||
|
|
|||
|
except Exception as e:
|
|||
|
print(f"❌ 演示失败: {e}")
|
|||
|
|
|||
|
|
|||
|
if __name__ == "__main__":
|
|||
|
# 如果直接运行此脚本,执行完整测试
|
|||
|
run_comprehensive_test()
|