suwoodblender/test/blender_simple_test.py

165 lines
4.6 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 -*-
"""
Blender SUWood 简单测试脚本
在Blender Text Editor中运行此脚本
"""
# 首先导入Blender模块
import bpy
import sys
from pathlib import Path
print("🚀 Blender SUWood 简单测试")
print("=" * 40)
# 清除场景中的默认对象
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)
# 添加SUWood模块路径
project_path = r"D:\XL\code\blender\blenderpython"
if project_path not in sys.path:
sys.path.insert(0, project_path)
print(f"✅ 添加模块路径: {project_path}")
try:
# 导入SUWood模块
from suw_impl import SUWImpl, Point3d, Vector3d
print("✅ SUWood模块导入成功")
# 初始化SUWImpl
suw = SUWImpl.get_instance()
suw.startup()
print("✅ SUWImpl 初始化完成")
# 创建测试集合
if "SUWood_Simple_Test" not in bpy.data.collections:
test_collection = bpy.data.collections.new("SUWood_Simple_Test")
bpy.context.scene.collection.children.link(test_collection)
else:
test_collection = bpy.data.collections["SUWood_Simple_Test"]
print(f"✅ 测试集合准备: {test_collection.name}")
# 测试1: 创建简单矩形面
print("\n🧪 测试创建面...")
surface_data = {
"segs": [
["(0,0,0)", "(500,0,0)"], # 500mm × 300mm
["(500,0,0)", "(500,300,0)"],
["(500,300,0)", "(0,300,0)"],
["(0,300,0)", "(0,0,0)"]
]
}
face_obj = suw.create_face(
container=test_collection,
surface=surface_data,
color="wood_color"
)
if face_obj:
print(f"✅ 面创建成功: {face_obj.name}")
face_obj.location = (0, 0, 0)
else:
print("❌ 面创建失败")
# 测试2: 创建边线
print("\n🧪 测试创建边线...")
edges_data = [
["(0,0,100)", "(500,0,100)"], # 水平线
["(250,0,0)", "(250,300,0)"] # 垂直线
]
edges = suw.create_edges(test_collection, edges_data)
if edges and len(edges) > 0:
print(f"✅ 边线创建成功: {len(edges)}")
else:
print("❌ 边线创建失败")
# 测试3: 创建材质
print("\n🧪 测试材质创建...")
if "TestWood" not in bpy.data.materials:
wood_material = bpy.data.materials.new(name="TestWood")
wood_material.use_nodes = True
# 设置木材颜色
bsdf = wood_material.node_tree.nodes.get("Principled BSDF")
if bsdf:
bsdf.inputs[0].default_value = (0.8, 0.6, 0.4, 1.0) # 木材色
bsdf.inputs[7].default_value = 0.3 # 粗糙度
print("✅ 木材材质创建成功")
else:
wood_material = bpy.data.materials["TestWood"]
print("✅ 使用现有木材材质")
# 应用材质到面
if face_obj and wood_material:
suw.textured_surf(
face=face_obj,
material=wood_material,
current="wood_finish",
color="wood_color"
)
print("✅ 材质应用成功")
# 测试4: 几何类测试
print("\n🧪 测试几何类...")
# Point3d 测试
point = Point3d.parse("(100,200,50)")
if point:
print(f"✅ Point3d: ({point.x:.3f}, {point.y:.3f}, {point.z:.3f})")
# Vector3d 测试
vector = Vector3d.parse("(1,0,0)")
if vector:
print(f"✅ Vector3d: ({vector.x:.3f}, {vector.y:.3f}, {vector.z:.3f})")
# 设置视图
print("\n🎯 设置视图...")
# 切换到材质着色模式
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
for space in area.spaces:
if space.type == 'VIEW_3D':
space.shading.type = 'MATERIAL'
break
# 调整视图
bpy.ops.object.select_all(action='SELECT')
bpy.ops.view3d.view_selected()
print("✅ 视图设置完成")
print("\n🎉 测试完成!")
print("=" * 40)
print("检查结果:")
print("1. 场景中应该有SUWood_Simple_Test集合")
print("2. 包含一个矩形面和两条边线")
print("3. 面应该有木材纹理")
print("4. 视图应该居中显示对象")
# 统计创建的对象
objects_count = len(test_collection.objects)
print(f"\n📊 创建了 {objects_count} 个对象")
if objects_count > 0:
print("🏆 SUWood工具在Blender中工作正常!")
else:
print("⚠️ 可能存在问题请检查Console输出")
except Exception as e:
print(f"❌ 测试失败: {e}")
import traceback
traceback.print_exc()
print("\n" + "=" * 40)