当前位置: 首页 > wzjs >正文

从网站开发到游戏编程网站模板服务商

从网站开发到游戏编程,网站模板服务商,长沙网络营销哪家好,苏州外贸网站建设公司排名Blender-MCP服务源码5-BlenderSocket插件安装 上一篇讲述了Blender是基于Socket进行本地和远程进行通讯,现在尝试将BlenderSocket插件安装到Blender中进行功能调试 1-核心知识点 将开发的BlenderSocket插件安装到Blender中 2-思路整理 1)将SocketServe…

Blender-MCP服务源码5-BlenderSocket插件安装

上一篇讲述了Blender是基于Socket进行本地和远程进行通讯,现在尝试将BlenderSocket插件安装到Blender中进行功能调试


1-核心知识点

  • 将开发的BlenderSocket插件安装到Blender中

2-思路整理

  • 1)将SocketServer部署到Blender启动SocketServer
  • 2)本地使用SocketClient连接SocketServer尝试发送指令
  • 3)验证交互结果->如果该逻辑通->后续就可以完善MCP业务指令

3-参考网址

  • Blender-MCP-Github地址:https://github.com/ahujasid/blender-mcp
  • B站大佬开源Blender开发框架:https://github.com/xzhuah/BlenderAddonPackageTool
  • B站大佬开源Blender开发框架教程
  • 个人实现代码仓库1:https://gitee.com/enzoism/python_blender_socket
  • 个人实现代码仓库2:https://gitee.com/enzoism/python_blender_mcp

4-上手实操

1-部署Socket到Blender

代码地址:https://gitee.com/enzoism/python_blender_mcp

  • 部署Socket到Blender
  • 本地SocketClient与SocketServer通讯验证

2-本地项目调试

运行test.py文件即可

  • SocketClient代码

代码地址:https://gitee.com/enzoism/python_blender_socket

import json
import socketdef send_command(command):"""发送命令到服务器并接收响应"""sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # 创建TCP套接字try:print("Connecting to server...")  # 连接到服务器sock.connect(('localhost', 9876))  # 连接到本地9876端口的服务器# 发送命令json_command = json.dumps(command).encode('utf-8')  # 将命令转换为JSON格式并编码为字节sock.sendall(json_command)  # 发送命令print(f"Sent command: {command}")  # 打印发送的命令# 接收响应sock.settimeout(10)  # 设置超时时间为10秒response_data = sock.recv(65536)  # 接收响应数据,最大长度为65536字节if response_data:  # 如果接收到数据response = json.loads(response_data.decode('utf-8'))  # 将响应数据解码为JSON格式return response  # 返回响应else:return {"status": "error", "result": "Empty response"}  # 如果没有接收到数据,返回错误信息except Exception as e:  # 捕获所有异常return {"status": "error", "result": str(e)}  # 返回异常信息finally:sock.close()  # 关闭套接字if __name__ == "__main__":# 测试ping命令ping_command = {"type": "ping","params": {}}response = send_command(ping_command)print("Server response:", response)  # 打印服务器响应

3-代码结构

  • 1)clazz类中业务类->比如创建一个Person类,有构造方法,有eat方法
  • 2)operator中放要clazz类中业务类的操作逻辑
  • 3)panels中放看板的点击Operator类->但是不要在这里也业务逻辑(解耦)
  • 4)panels中防止看板的判断属性->根据状态更换按钮文字和点击事件

  • 1)clazz代码示例
import json
import socket
import threading
import time# 添加自己的业务Operator
class BlenderMCPServer:def __init__(self, host='localhost', port=9876):self.host = hostself.port = portself.running = Falseself.socket = Noneself.client = Noneself.server_thread = Nonedef start(self):self.running = Trueself.server_thread = threading.Thread(target=self._run_server)self.server_thread.daemon = Trueself.server_thread.start()print(f"BlenderMCP server started on {self.host}:{self.port}")def stop(self):self.running = Falseif self.socket:self.socket.close()if self.client:self.client.close()print("BlenderMCP server stopped")def _run_server(self):self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)try:self.socket.bind((self.host, self.port))self.socket.listen(1)self.socket.settimeout(1.0)  # Add a timeout for acceptwhile self.running:try:self.client, address = self.socket.accept()print(f"Connected to client: {address}")while self.running:try:# Set a timeout for receiving dataself.client.settimeout(15.0)data = self.client.recv(4096)if not data:print("Empty data received, client may have disconnected")breaktry:print(f"Received data: {data.decode('utf-8')}")command = json.loads(data.decode('utf-8'))response = self.execute_command(command)print(f"Sending response: {json.dumps(response)[:100]}...")  # Truncate long responses in logself.client.sendall(json.dumps(response).encode('utf-8'))except json.JSONDecodeError:print(f"Invalid JSON received: {data.decode('utf-8')}")self.client.sendall(json.dumps({"status": "error","message": "Invalid JSON format"}).encode('utf-8'))except Exception as e:print(f"Error executing command: {str(e)}")import tracebacktraceback.print_exc()self.client.sendall(json.dumps({"status": "error","message": str(e)}).encode('utf-8'))except socket.timeout:print("Socket timeout while waiting for data")continueexcept Exception as e:print(f"Error receiving data: {str(e)}")breakself.client.close()self.client = Noneexcept socket.timeout:# This is normal - just continue the loopcontinueexcept Exception as e:print(f"Connection error: {str(e)}")if self.client:self.client.close()self.client = Nonetime.sleep(1)  # Prevent busy waitingexcept Exception as e:print(f"Server error: {str(e)}")finally:if self.socket:self.socket.close()def execute_command(self, command):"""Execute a Blender command received from the MCP server"""cmd_type = command.get("type")params = command.get("params", {})# Add a simple ping handlerif cmd_type == "ping":print("Handling ping command")return {"status": "success", "result": {"pong": True}}else:return {"status": "error","result": f"Unknown command type: {command.get('type')}"}

  • 2)operator代码示例
class BlenderMCPOperatorStart(bpy.types.Operator):'''BlenderMCPOperatorStart'''bl_idname = "object.blender_mcp_operator_start"bl_label = "Now_Stop_Click_Start"# 确保在操作之前备份数据,用户撤销操作时可以恢复bl_options = {'REGISTER', 'UNDO'}@classmethoddef poll(cls, context: bpy.types.Context):return context.active_object is not Nonedef execute(self, context: bpy.types.Context):scene = context.scene# 创建socket实例if not hasattr(bpy.types, "blender_mcp_server") or not bpy.types.blender_mcp_server:# Start the serverbpy.types.blender_mcp_server = BlenderMCPServer(port=9876)bpy.types.blender_mcp_server.start()print("---------------------Start MCP Server后重置状态")scene.blender_mcp_server_running = Truereturn {'FINISHED'}class BlenderMCPOperatorStop(bpy.types.Operator):'''BlenderMCPOperatorStop'''bl_idname = "object.blender_mcp_operator_stop"bl_label = "Now_Start_Click_Stop"@classmethoddef poll(cls, context: bpy.types.Context):return context.active_object is not Nonedef execute(self, context: bpy.types.Context):scene = context.scene# 销毁socket实例if hasattr(bpy.types, "blender_mcp_server") or bpy.types.blender_mcp_server:bpy.types.blender_mcp_server.stop()del bpy.types.blender_mcp_serverprint("---------------------Stop MCP Server后重置状态")scene.blender_mcp_server_running = Falsereturn {'FINISHED'}

  • 3)panels中Operator代码示例
@reg_order(0)
class ExampleAddonPanel2(BasePanel, bpy.types.Panel):bl_label = "Example Addon Side Bar Panel"bl_idname = "SCENE_PT_sample2"def draw(self, context: bpy.types.Context):layout = self.layoutlayout.label(text="BlenderMCP Panel")# 当前只是为了测试常驻按钮的点击测试-点击对图形缩小0.8layout.operator(StaticButtonOperator.bl_idname)# 测试服务器的装scene = context.sceneif not scene.blender_mcp_server_running:layout.operator(BlenderMCPOperatorStart.bl_idname)print("Start MCP Server")else:layout.operator(BlenderMCPOperatorStop.bl_idname)print("Stop MCP Server")

  • 4)panels中属性代码示例
import bpy# 添加属性到 Scene 类型
bpy.types.Scene.blender_mcp_server = bpy.props.BoolProperty(name="Blender MCP Server Running",default=False,description="Indicates whether the Blender MCP server is running."
)
bpy.types.Scene.blender_mcp_server_running = bpy.props.BoolProperty(name="Blender MCP Server Running",default=False,description="Indicates whether the Blender MCP server is running."
)

文章转载自:

http://muFZD1eq.kkwgg.cn
http://AhAmCFTh.kkwgg.cn
http://91bG6E9z.kkwgg.cn
http://T4ZeU2xg.kkwgg.cn
http://7VNa09O5.kkwgg.cn
http://Ba7DfbnG.kkwgg.cn
http://1c1c099U.kkwgg.cn
http://yIjLPpzg.kkwgg.cn
http://88l0nRQC.kkwgg.cn
http://rukqyB8L.kkwgg.cn
http://XwKrtXSg.kkwgg.cn
http://poOiPaU7.kkwgg.cn
http://mgosUt8T.kkwgg.cn
http://ThE7zcl6.kkwgg.cn
http://HY9wKS15.kkwgg.cn
http://gZ89cbhJ.kkwgg.cn
http://EeJ2WGnB.kkwgg.cn
http://fDxuA92p.kkwgg.cn
http://vnGG3zQa.kkwgg.cn
http://V1n81YcF.kkwgg.cn
http://PRIsTOfq.kkwgg.cn
http://CVGLG5JX.kkwgg.cn
http://8XE2eFLH.kkwgg.cn
http://8xe4Dx0T.kkwgg.cn
http://MXDRent6.kkwgg.cn
http://laa9PQhL.kkwgg.cn
http://EISDWIjV.kkwgg.cn
http://yxrTp2y6.kkwgg.cn
http://njElW3Xc.kkwgg.cn
http://KR4uLDjL.kkwgg.cn
http://www.dtcms.com/wzjs/681274.html

相关文章:

  • 网站开发需要的知识现在开什么网站
  • 沈阳网站哪家公司做的好万网域名注册
  • 网站开发存在的风险百度集团总部在哪里
  • wordpress网站更改主题信息电商网站建设用php
  • 企业网站seo报价app网站平台搭建
  • 定制做网站费用creative建网站平台
  • 可以做微商的网站安阳门户网站
  • 泉州做网站价格宁波建设集团几个分公司
  • 广州市海珠区建设局网站wordpress升级文章编辑器
  • 做垂直平台网站天津网页设计教程
  • 电商网站如何设计内容frontpage官方下载
  • 免费领手机 网站saas 做网站
  • 山东移动网站建设ui设计网页设计培训
  • 建设企业网站官网下载东营网站建设运营公司
  • 制作网站团队知己知彼网站
  • 关于网站建设论文的结束语网易企业邮箱怎么修改绑定手机
  • 哈尔滨 网站建设公司wordpress评轮审核
  • 重庆网站开发企业html5网站app开发
  • 川畅科技联系 网站设计站长之家网站建设
  • html5 js全屏滑动网站源码网站建设方案策划书ppt
  • 有哪些网站可以做全景效果图建设摩托车是名牌吗
  • 苏州城乡建设局的网站首页自学商城网站建设
  • 网站后台权限管理怎么做的个人网站尺寸
  • 网站内部资源推广怎么做网站建设专家价格
  • 福州整站优化做杂志的网站有哪些
  • 微信网站开发公司电话本地手机网站建设服务
  • 手机商城网站案例北京高端企业网站建设
  • 微博网站开发平台篡改 网站 支付接口
  • 重庆市官方网站大连seo快速排名
  • 做电影网站选择什么配置的服务器做爰全国网站