搅拌机遥控器
概述
目的:通过LLM辅助的Python开发实现复杂的Blender自动化,弥合人工智能生成的Blender脚本和外部Python工具之间的差距。
预期用户:
- 开发人员需要复杂的Blender自动化,但没有时间掌握Blender的Python API
- 用户在Blender的基本文本编辑器中编写Blender端Python代码时感到不舒服
- 严重依赖LLM进行代码生成并希望自动化Blender任务的开发人员
我们的解决方案:
- 允许LLM生成Blender端的Python代码,并帮助将其封装到外部Python API中
- 为全自动化和批处理提供后台模式执行
- 我们不试图将所有Blender Python API映射到Python或MCP -相反,我们为用户提供基础设施,让他们开发自己的Python工具,在LLM的帮助下与Blender进行交互
- 最终结果:使用VSCode和自己的Python来控制Blender,不受Blender的基本Python环境和编辑器的限制,轻松编写复杂的Blender自动化项目
系统架构:
BLD_Remote_MCP-Blender插件使用JSON-RPC与外部调用者通信- MCP服务器 -将MCP命令从LLM IDE(VSCode、Claude、Cursor)转发到Blender插件
- Python客户端 -直接控制Blender插件,绕过MCP服务器进行自动化脚本
主要特点:
- LLM生成的Blender端代码和外部Python API之间的无缝桥梁
- LLM和Python客户端同时访问,代码转换工作流程顺畅
- LLM辅助的包装器代码生成,用于将Blender脚本转换为Python API
- 后台模式支持自动化和批处理
- 跨平台支持:Windows、Linux、macOS
小心:这段代码主要是在人工智能的帮助下编写的。使用风险自负。
用法
安装
安装软件包:
pip install blender-remote安装uv(MCP服务器需要):
# Windows (PowerShell)
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
# Linux/macOS
curl -LsSf https://astral.sh/uv/install.sh | sh基本用法
CLI方法
使用 blender-remote-cli 设置和管理Blender集成:
1.初始化并安装插件:
# Auto-detect Blender (Windows/macOS) or specify path
blender-remote-cli init
# Install the addon automatically (recommended)
blender-remote-cli install手动安装注意事项:如果您更喜欢手动安装插件或检查其源代码,可以先导出它: ``bash blender-remote-cli export --content=addon -o ./exported_addon`这创建了一个bld_remote_mcp目录内./exported_addon。然后,您可以压缩此目录并通过Blender的Edit > Preferences > Add-ons`.
2.在Blender GUI中验证安装:
- 打开搅拌机→ Edit → 偏好设置→ 附加组件
- 搜索“BLD远程MCP”-应启用
3.配置服务设置(可选,启动前):
# Configure custom port (default is 6688)
blender-remote-cli config set mcp_service.default_port=7777
# Configure logging level
blender-remote-cli config set mcp_service.log_level=DEBUG
# View current configuration
blender-remote-cli config get mcp_service.default_port4.使用服务启动Blender:
# GUI mode with service
blender-remote-cli start
# Background mode for automation
blender-remote-cli start --background
# Load specific scene
blender-remote-cli start --scene=my_project.blend5.在运行Blender时执行命令:
# Execute Python code directly
blender-remote-cli execute -c "import bpy; bpy.ops.mesh.primitive_cube_add(location=(2, 0, 0))"
# Execute with custom port
blender-remote-cli execute -c 'import bpy; print(f"Blender {bpy.app.version_string}")' --port 7888
# Execute Python file
blender-remote-cli execute my_script.py
# Complex code with base64 encoding (recommended for multiline code)
blender-remote-cli execute -c "import bpy; [bpy.ops.mesh.primitive_cube_add(location=(i*2, 0, 0)) for i in range(3)]" --use-base646.导出插件或脚本以供手动使用:
# Export addon source code for inspection or manual installation
blender-remote-cli export --content=addon -o ./exported_addon
# Export the keep-alive script for custom background startup
blender-remote-cli export --content=keep-alive.py -o .MCP服务器方法
对于VSCode、Claude Desktop或Cursor等LLM IDE:
1.先安装Blender插件 (见上面的CLI方法)
2.使用服务启动Blender:
blender-remote-cli start3.配置LLM IDE:
VSCode设置.json:
{
"mcpServers": {
"blender-remote": {
"command": "uvx",
"args": ["blender-remote"]
}
}
}自定义主机/端口配置:
{
"mcpServers": {
"blender-remote": {
"command": "uvx",
"args": ["blender-remote", "--host", "127.0.0.1", "--port", "6688"]
}
}
}4.与LLM一起使用:
- “当前Blender场景中有哪些对象?”
- 在(2,0,0)位置创建一个金属蓝色立方体
- “将当前场景导出为GLB格式”
- “帮助我创建一个Python函数来生成立方体网格”
Python客户端方法
对于直接的Python自动化脚本:
import blender_remote
# Connect to running Blender service
client = blender_remote.connect_to_blender(port=6688)
# Execute Blender Python code directly
result = client.execute_python("bpy.ops.mesh.primitive_cube_add(location=(2, 0, 0))")
# Use scene manager for higher-level operations
scene_manager = blender_remote.create_scene_manager(client)
scene_manager.set_camera_location(location=(7, -7, 5), target=(0, 0, 0))
# Get scene information
scene_info = client.get_scene_info()
print(f"Scene has {len(scene_info['objects'])} objects")高级用法
使用LLM为Blender开发Python工具
工作流程示例:
- 问LLM:“创建Blender代码以生成立方体螺旋”
- LLM生成:Blender端Python代码使用
bpy运营 - 搅拌机测试:使用MCP工具执行和改进代码
- 问LLM:“将其包装成我可以从外部脚本调用的Python函数”
- LLM创建包装器:
def create_cube_spiral(client, count=10, radius=3):
code = f"""
import bpy
import math
for i in range({count}):
angle = i * (2 * math.pi / {count})
x = {radius} * math.cos(angle)
y = {radius} * math.sin(angle)
z = i * 0.5
bpy.ops.mesh.primitive_cube_add(location=(x, y, z))
"""
return client.execute_python(code)
# Use the wrapper
client = blender_remote.connect_to_blender()
create_cube_spiral(client, count=15, radius=5)使用后台模式进行批处理
为了完全控制后台进程,您可以导出keep-alive脚本,根据需要进行修改,并直接使用Blender运行它。这对于自定义启动逻辑或集成到更大的自动化框架中非常有用。
1.导出保活脚本
blender-remote-cli export --content=keep-alive.py -o .2.使用脚本和所需的环境变量启动Blender
该插件要求环境变量知道要使用哪个端口并启用自动启动。
在Linux/macOS上:
# Set environment variables and start Blender in the background
export BLD_REMOTE_MCP_PORT=7788
export BLD_REMOTE_MCP_START_NOW=1
export BLD_REMOTE_LOG_LEVEL=DEBUG # Optional: for detailed logs
blender --background --python keep-alive.py &在Windows上(命令提示符):
C:\> set BLD_REMOTE_MCP_PORT=7788
C:\> set BLD_REMOTE_MCP_START_NOW=1
C:\> set BLD_REMOTE_LOG_LEVEL=DEBUG
C:\> start /b blender --background --python keep-alive.py在Windows(PowerShell)上:
PS C:\> $env:BLD_REMOTE_MCP_PORT="7788"
PS C:\> $env:BLD_REMOTE_MCP_START_NOW="1"
PS C:\> $env:BLD_REMOTE_LOG_LEVEL="DEBUG"
PS C:\> Start-Process blender -ArgumentList "--background", "--python", "keep-alive.py" -NoNewWindow然后,Python客户端可以在指定端口上连接到此手动启动的实例。
自动批处理工作流(使用CLI启动):
import blender_remote
import subprocess
import time
import os
# Start background Blender process using CLI (avoids path issues)
port = 7888
process = subprocess.Popen([
"python", "-m", "blender_remote.cli", "start", "--background", "--port", str(port)
])
# Wait for service to start up
time.sleep(3)
# Connect to the background instance
client = blender_remote.connect_to_blender(port=port)
# Process multiple scene files
scene_dir = "tmp/test-scenes"
input_files = ["scene1.blend", "scene2.blend", "scene3.blend"]
for scene_file in input_files:
scene_path = os.path.join(scene_dir, scene_file)
scene_path_abs = os.path.abspath(scene_path)
print(f"Processing {scene_file}...")
# Load scene
client.execute_python(f'bpy.ops.wm.open_mainfile(filepath="{scene_path_abs.replace(os.sep, "/")}")')
# Process scene (your custom operations)
client.execute_python("bpy.ops.mesh.primitive_cube_add(location=(0, 0, 2))")
client.execute_python("bpy.ops.mesh.primitive_uv_sphere_add(location=(2, 0, 0))")
# Export result
output_file = scene_file.replace('.blend', '.glb')
output_path = os.path.abspath(f"tmp/{output_file}")
client.execute_python(f'bpy.ops.export_scene.gltf(filepath="{output_path.replace(os.sep, "/")}")')
print(f"Exported {output_file}")
# Gracefully exit Blender
client.execute_python("bpy.ops.wm.quit_blender()")
process.wait() # Wait for process to fully exit文档
鸣谢
建在 搅拌机mcp 该项目具有增强的后台模式支持、线程安全操作和生产部署功能。
