实战 Blender CLI:从 clone 到渲染的真实操作演示
为什么选 Blender
Blender 是 CLI-Anything 里测试覆盖最完整的软件——208 个测试,100% 通过。而且 Blender 是开源软件里 API 文档相对完善的,有官方的 Python 脚本接口 bpy,这让 CLI 生成的质量有保障。
不过 Blender 的命令行使用比想象中麻烦一点。Blender 官方并没有提供一个友好的 CLI 工具,大多数人用 Blender 还是在 GUI 里面操作。cli-anything-blender 做的事就是把 Blender 的 Python 脚本接口封装成人类和 Agent 都能用的命令。
环境准备
开始之前,确保系统里有这些:
# Blender 已安装且在 PATH
blender --version
# Blender 4.0.0 (hash abc123)
# Python 3.10+
python --version
# Claude Code / OpenCode / OpenClaw 其一已安装
克隆项目并构建 Blender CLI:
git clone https://github.com/HKUDS/CLI-Anything.git
cd CLI-Anything
# 用 Claude Code 构建
# /cli-anything:cli-anything ./blender
# 或者直接使用已生成好的(如果你不想自己构建)
cd blender/agent-harness
pip install -e .
命令行入口
安装完成后,cli-anything-blender 命令就全局可用了。
$ cli-anything-blender --help
Usage: cli-anything-blender [OPTIONS] COMMAND [ARGS]...
Blender CLI for AI Agents. Control Blender through structured commands.
Commands:
scene Scene management
object Object operations
material Material system
render Render control
mesh Mesh data operations
进入 REPL 模式:
$ cli-anything-blender
╔══════════════════════════════════════════╗
║ cli-anything-blender v1.0.0 ║
║ Blender CLI for AI Agents ║
╚══════════════════════════════════════════╝
[blender]>
创建 3D 场景
REPL 方式:
[blender]> scene new --name "ProductShot" --width 1920 --height 1080
✓ Created scene: ProductShot (1920×1080)
✓ State saved to: productshot.json
[blender]> object add -t cube -n "ProductBox" --size 2
✓ Added cube: ProductBox (size: 2m)
[blender]> object add -t plane -n "Floor" --size 20
✓ Added plane: Floor (size: 20m)
[blender]> material create -n "MatRed" --color "#cc3333" --metallic 0.8
✓ Created material: MatRed (metallic: 0.8)
[blender]> object set-material -n "ProductBox" -m "MatRed"
✓ Applied material MatRed to ProductBox
命令方式(适合脚本和 CI):
cli-anything-blender scene new --name "ProductShot" --width 1920 --height 1080
cli-anything-blender object add -t cube -n "ProductBox" --size 2
cli-anything-blender material create -n "MatRed" --color "#cc3333" --metallic 0.8
JSON 模式下的结构化输出
Agent 使用时,用 --json 拿到结构化数据:
$ cli-anything-blender --json scene info --project productshot.json
{
"scene": "ProductShot",
"resolution": {"x": 1920, "y": 1080},
"objects": [
{"name": "ProductBox", "type": "cube", "size": 2.0, "material": "MatRed"},
{"name": "Floor", "type": "plane", "size": 20.0, "material": null}
],
"materials": [
{"name": "MatRed", "color": "#cc3333", "metallic": 0.8, "roughness": 0.5}
]
}
触发真实渲染
这是最关键的部分——CLI-Anything 不是生成一个假的 3D 场景文件然后假装渲染了,而是真的调用 Blender 来渲染。
$ cli-anything-blender render start \
--project productshot.json \
--output ./product_render.png \
--samples 128 \
--engine cycles
Blender 的实际运行过程:
Starting Blender 4.0.0 (background mode)...
Loading scene from productshot.json...
Configuring Cycles render engine...
Setting sample count to 128...
Rendering frame 1...
Progress: 100%
Render complete.
Output: ./product_render.png (1920×1080, 2.3MB)
✓ Saved: ./product_render.png
验证渲染结果:
$ file product_render.png
product_render.png: PNG image data, 1920 x 1080, 8-bit/color RGBA
$ identify product_render.png
product_render.png PNG 1920x1080 1920x1080+0+0 8-bit sRGB
批量渲染动画帧
如果你有一个多帧动画场景,可以用批处理模式:
# 先生成动画关键帧
for i in {1..30}; do
cli-anything-blender --json object rotate \
--project productshot.json \
--name "ProductBox" \
--axis Z \
--degrees $((i * 12)) \
--output-frame $i
done
# 然后批量渲染
cli-anything-blender render batch \
--project productshot.json \
--start 1 \
--end 30 \
--output-dir ./frames/ \
--samples 64
Blender CLI 的后端实现思路
cli-anything-blender 的后端调的是 Blender 的 bpy 模块,这是 Blender 内置的 Python 脚本接口。核心思路是把命令转换成 bpy.ops 调用:
# 简化版的 Blender 后端
import bpy
def add_cube(name, size):
bpy.ops.mesh.primitive_cube_add(size=size, location=(0, 0, 0))
obj = bpy.context.active_object
obj.name = name
return {"name": name, "type": "cube", "size": size}
def apply_material(object_name, material_name):
obj = bpy.data.objects.get(object_name)
mat = bpy.data.materials.get(material_name)
if obj and mat:
if obj.data.materials:
obj.data.materials[0] = mat
else:
obj.data.materials.append(mat)
def render_to_file(output_path, samples=128, engine="CYCLES"):
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.cycles.samples = samples
bpy.ops.render.render(write_still=True, filepath=output_path)
然后 CLI 层做的是参数解析和文件持久化,真正做事的还是 Blender 自己的引擎。这保证了渲染质量和在 GUI 里手动渲染是一致的。
Blender 场景作为 Agent 工作流的一部分
CLI-Anything 设计的精妙之处在于:Blender CLI 不需要单独存在,它可以被流水线串联起来。
比如一个产品宣传片的工作流:
[数据] → [Blender] → [Shotcut] → [FFmpeg] → [最终视频]
Agent 可以这样规划:
# 1. Blender 生成产品图
cli-anything-blender scene new --name "ProductAd" --width 3840 --height 2160
cli-anything-blender object add -t model -n "Product" --import-obj ./product.obj
cli-anything-blender render start --output ./product_shot.png
# 2. Shotcut 剪辑合成
cli-anything-shotcut project new -o ad_project.json
cli-anything-shotcut clip add -n product_shot.png -o ad_project.json
cli-anything-shotcut export render -o ./ad_clip.mp4
# 3. FFmpeg 最终压制
ffmpeg -i ./ad_clip.mp4 -c:v libx264 -crf 20 ./final_ad.mp4
三款不同的软件,通过 JSON 项目文件作为数据流转的中间格式,被 Agent 用命令行串联成了一个完整的自动化工作流。

浙公网安备 33010602011771号