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

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://www.mzph.cn/diannao/73892.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【MySQL数据库】存储过程与自定义函数(含: SQL变量、分支语句、循环语句 和 游标、异常处理 等内容)

存储过程:一组预编译的SQL语句和流程控制语句,被命名并存储在数据库中。存储过程可以用来封装复杂的数据库操作逻辑,并在需要时进行调用。 类似的操作还有:自定义函数、.sql文件导入。 我们先从熟悉的函数开始说起: …

ASP3605抗辐照加固同步降压调节器——商业航天电源芯片解决方案新选择

ASP3605企业宇航级型号ASP3605S2U通过SEU≥75 MeVcm/mg与SEL≥75 MeVcm/mg抗辐射测试。其输入电压4V至15V,输出电流5A,支持多相级联与冗余设计,适用于卫星、航天器电源系统。 面向航天场景的核心功能设计 1. 抗辐射与可靠性保障 单粒子效应…

使用fastapi部署stable diffusion模型

使用vscode运行stable diffusion模型,每次加载模型都需要10分钟,为算法及prompt调试带来了极大麻烦。使用jupyter解决自然是一个比较好的方案,但如果jupyter由于种种原因不能使用时,fastapi无疑成为了一个很好的选择。 参考github…

2025-03-16 学习记录--C/C++-PTA 习题4-4 特殊a串数列求和

合抱之木,生于毫末;九层之台,起于累土;千里之行,始于足下。💪🏻 一、题目描述 ⭐️ 习题4-4 特殊a串数列求和 给定两个均不超过9的正整数a和n,要求编写程序求aaaaaa⋯aa⋯a&#x…

ffmpeg库视频硬编码使用流程

‌一、硬件编码核心流程‌ ‌硬件设备初始化 // 创建CUDA硬件设备上下文‌ AVBufferRef *hw_device_ctx NULL; av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_CUDA, NULL, NULL, 0);// 绑定硬件设备到编码器上下文‌ codec_ctx->hw_device_ctx av_buffer_…

【设计模式】3W 学习法全面解析 7 大结构型模式:Java 实战 + 开源框架应用

3W 学习法总结结构型模式(附 Java 代码实战及开源框架应用) 结构型模式 主要关注 类与对象的组合,确保不同组件之间能够高效协作,提高系统的灵活性和可维护性。本文采用 3W 学习法(What、Why、How)&#x…

在大数据开发中ETL是指什么?

hello宝子们...我们是艾斯视觉擅长ui设计和前端数字孪生、大数据、三维建模、三维动画10年经验!希望我的分享能帮助到您!如需帮助可以评论关注私信我们一起探讨!致敬感谢感恩! 在数字经济时代,数据已成为企业最核心的资产。然而,分散在业务系统、日志文件…

前端面试项目拷打

Axios相关 1.在Axios二次封装时,具体封装了哪些内容,如何处理请求拦截和响应拦截? axios二次封装的目的:为了统一处理请求和响应拦截器、错误处理、请求超时、请求头配置等,提高代码可维护性和复用性。 首先创建axios…

「JavaScript深入」Server-Sent Events (SSE):轻量级实时通信技术

Server-Sent Events(SSE) SSE 的特点1. 单向通信2. 简单易用,浏览器原生支持3. 持久连接4. 纯文本传输5. 自动重连机制6. 轻量级协议 SSE 的实现服务器端实现(Node.js 示例)1. HTTP 响应头设置2. 数据推送模式3. 服务器…

蓝桥杯2023年第十四届省赛真题-阶乘的和

蓝桥杯2023年第十四届省赛真题-阶乘的和 时间限制: 2s 内存限制: 320MB 提交: 3519 解决: 697 题目描述 给定 n 个数 Ai,问能满足 m! 为∑ni1(Ai!) 的因数的最大的 m 是多少。其中 m! 表示 m 的阶乘,即 1 2 3 m。 输入格式 输入的第一行包含一个整…

影刀RPA拓展-Python变量类型转换

1. Python变量类型转换概述 1.1 类型转换的必要性 Python作为一种动态类型语言,在编程过程中经常需要进行变量类型转换。这主要是因为不同数据类型在存储结构、运算规则和使用场景上存在差异,而在实际开发中,我们常常需要对不同类型的数据进…

Python pyqt+flask做一个简单实用的自动排班系统

这是一个基于Flask和PyQt的排班系统,可以将Web界面嵌入到桌面应用程序中。 系统界面: 功能特点: - 读取员工信息和现有排班表 - 自动生成排班表 - 美观的Web界面 - 独立的桌面应用程序 整体架构: 系统采用前后端分离的架构…

Pycharm接入DeepSeek,提升自动化脚本的写作效率

一.效果展示: 二.实施步骤: 1.DeepSeek官网创建API key: 创建成功后,会生成一个API key: 2. PyCharm工具,打开文件->设置->插件,搜索“Continue”,点击安装 3.安装完成后&…

Java:Arrays类:操作数组的工具类

文章目录 Arrays类常见方法SetAll(); 代码排序如果数组中存储的是自定义对象 Arrays类 常见方法 SetAll(); 注意: 不能用新的数组接是因为修改的是原数组,所以完了要输出原数组发现会产生变化参数是数组下标变成灰色是因为还能简化(Lambda…

2025-gazebo配置on vmware,wsl

ros2安装 # 安装ros2, 推荐鱼香ros一键式安装 wget http://fishros.com/install -O fishros && . fishros安装版本:ubuntu24.04 ros2 jazzy gazebo Getting Started with Gazebo? — Gazebo ionic documentation ros与gz的版本对应关系: ​…

格力地产更名“珠免集团“ 全面转型免税赛道

大湾区经济网品牌观察讯,3月18日,格力地产股份有限公司公告宣布,拟将公司名称变更为"珠海珠免集团股份有限公司",证券简称同步变更为"珠免集团"。此次更名并非简单的品牌焕新,而是标志着这家曾以房…

网络编程--服务器双客户端聊天

写一个服务器和客户端 运行服务器和2个客户端,实现聊天功能 客户端1和客户端2进行聊天,客户端1将聊天数据发送给服务器,服务器将聊天数据转发给客户端2 要求: 服务器使用 select 模型实现 ,客户端1使用 poll 模型实现…

k8s主要控制器简述(一)ReplicaSet与Deployment

目录 一、ReplicaSet 关键特性 示例 解释 支持的 Operator 二、Deployment 1. 声明式更新 示例 2. 滚动更新 示例 3. 回滚 示例 4. ReplicaSet 管理 示例 5. 自动恢复 示例 6. 扩展和缩容 示例 示例 一、ReplicaSet ReplicaSet 是 Kubernetes 中的一个核心控…

python中redis操作整理

下载redis命令 pip install redis 连接redis import redis # host是redis主机,需要redis服务端和客户端都起着 redis默认端口是6379 pool redis.ConnectionPool(hostlocalhost, port6379,decode_responsesTrue) r redis.Redis(connection_poolpool)操作字符串 …

自然语言处理入门4——RNN

一般来说,提到自然语言处理,我们都会涉及到循环神经网络(RNN),这是因为自然语言可以被看作是一个时间序列,这个时间序列中的元素是一个个的token。传统的前馈神经网络结构简单,但是不能很好的处…