suwoodblender/test/blender_suw_client.py

220 lines
7.3 KiB
Python
Raw Permalink 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.

# 超级简化的SUWood客户端启动脚本
import sys
import os
import threading
import time
print("启动SUWood客户端...")
# 设置路径
suwood_path = r"D:\XL\code\blender\blenderpython"
sys.path.insert(0, suwood_path)
# 检查基础模块
try:
from suw_impl import SUWImpl
from suw_client import SUWClient, get_client
print("基础模块导入成功")
except Exception as e:
print(f"基础模块导入失败: {e}")
exit()
# 检查suw_core
use_modular = False
try:
import suw_core
if hasattr(suw_core, 'command_dispatcher') and suw_core.command_dispatcher:
cmd_dispatcher = suw_core.command_dispatcher
if hasattr(cmd_dispatcher, 'part_creator_ref'):
use_modular = True
print("检测到模块化架构")
else:
print("未检测到终极修复,尝试应用...")
try:
exec(open(r'D:\XL\code\blender\test\auto_init_suwood.py').read())
if hasattr(suw_core.command_dispatcher, 'part_creator_ref'):
use_modular = True
print("终极修复应用成功")
else:
print("使用原始方法")
except:
print("使用原始方法")
else:
print("使用原始方法")
except:
print("suw_core导入失败使用原始方法")
# 初始化SUWImpl
suw_impl = SUWImpl.get_instance()
suw_impl.startup()
print(f"架构模式: {'模块化' if use_modular else '原始'}")
# 命令处理函数
def handle_command(cmd_type, data):
import datetime
timestamp = datetime.datetime.now().strftime("%H:%M:%S")
print("=" * 50)
print(f"[{timestamp}] 命令: {cmd_type}")
print(f"数据: {data}")
# 处理嵌套命令
actual_cmd = cmd_type
actual_data = data
if cmd_type == 'set_cmd' and isinstance(data, dict) and 'cmd' in data:
actual_cmd = data.get('cmd')
actual_data = {k: v for k, v in data.items() if k != 'cmd'}
print(f"提取真实命令: {actual_cmd}")
try:
if use_modular:
print(f"使用模块化分发器处理: {actual_cmd}")
result = suw_core.command_dispatcher.dispatch_command(
actual_cmd, actual_data)
if result is not None:
print(f"分发器执行成功: {result}")
return True
else:
print("分发器返回None回退到原始方法")
# 回退
if hasattr(suw_impl, actual_cmd):
method = getattr(suw_impl, actual_cmd)
result = method(actual_data)
print(f"回退执行成功: {result}")
return True
else:
print(f"未找到方法: {actual_cmd}")
return False
else:
print(f"使用原始SUWImpl处理: {actual_cmd}")
if hasattr(suw_impl, actual_cmd):
method = getattr(suw_impl, actual_cmd)
result = method(actual_data)
print(f"原始方法执行成功: {result}")
return True
else:
print(f"未找到方法: {actual_cmd}")
return False
except Exception as e:
print(f"执行异常: {e}")
return False
# 客户端循环
def run_client():
import threading
import time
def client_thread():
print("启动客户端线程...")
try:
client = get_client()
if not client or not client.sock:
print("无法连接到服务器")
return
print("已连接到SUWood服务器")
print("开始监听命令...")
total = 0
success = 0
while True:
try:
from suw_client import get_cmds
commands = get_cmds()
if commands and len(commands) > 0:
print(f"\n收到 {len(commands)} 个命令")
for cmd in commands:
cmd_type = None
cmd_data = {}
# 解析命令
if 'type' in cmd:
cmd_type = cmd.get('type')
cmd_data = cmd.get('data', {})
elif 'cmd' in cmd and 'data' in cmd:
cmd_type = cmd.get('cmd')
cmd_data = cmd.get('data', {})
elif isinstance(cmd, dict):
for key in ['cmd', 'command', 'action']:
if key in cmd:
cmd_type = cmd.get(key)
cmd_data = {
k: v for k, v in cmd.items() if k != key}
break
if cmd_type:
total += 1
if handle_command(cmd_type, cmd_data):
success += 1
print(f"成功率: {success}/{total}")
time.sleep(0.1)
except KeyboardInterrupt:
break
except Exception as e:
print(f"循环异常: {e}")
time.sleep(1)
except Exception as e:
print(f"客户端异常: {e}")
thread = threading.Thread(target=client_thread, daemon=True)
thread.start()
return thread
# 修改 start_suwood_blender_client() 函数
def start_suwood_blender_client():
"""启动SUWood Blender客户端 - 修复版本,参考原始脚本"""
print("🎬 开始启动SUWood Blender客户端...")
# 1. 快速初始化SUWood系统不测试连接
initializer = SUWoodInitializer()
if not initializer.initialize():
print("💀 初始化失败,无法启动客户端")
return None
# 2. 创建客户端管理器
client_manager = SUWoodClientManager(initializer)
# 3. 【修复】直接启动后台线程,不在主线程测试连接
print("🌐 启动SUWood客户端...")
client_manager.running = True
client_manager.client_thread = threading.Thread(
target=client_manager._client_loop_with_connection_test, # 新方法
daemon=True,
name="SUWoodClient"
)
client_manager.client_thread.start()
print("🎉 SUWood Blender客户端启动成功!")
print("📋 系统信息:")
print(f" 🏗️ 架构: {'模块化' if initializer.use_modular else '原始'}")
print(f" 🧵 客户端线程: 运行中")
print("\n💡 现在可以从SUWood服务器发送命令来在Blender中绘制模型了!")
return client_manager
# 启动
client_thread = run_client()
print("SUWood客户端已启动!")
print("现在可以发送命令测试了")
if use_modular:
print("期待看到: '使用模块化分发器处理'")
else:
print("将看到: '使用原始SUWImpl处理'")