# Blender 渲染

在 CLORE.AI GPU 上使用 Blender 渲染 3D 场景和动画。

{% hint style="success" %}
所有示例都可以在通过以下方式租用的 GPU 服务器上运行： [CLORE.AI 市场](https://clore.ai/marketplace).
{% endhint %}

## 在 CLORE.AI 上租用

1. 访问 [CLORE.AI 市场](https://clore.ai/marketplace)
2. 按 GPU 类型、显存和价格筛选
3. 选择 **按需** （固定费率）或 **竞价** （出价价格）
4. 配置您的订单：
   * 选择 Docker 镜像
   * 设置端口（用于 SSH 的 TCP，Web 界面的 HTTP）
   * 如有需要，添加环境变量
   * 输入启动命令
5. 选择支付方式： **CLORE**, **BTC**，或 **USDT/USDC**
6. 创建订单并等待部署

### 访问您的服务器

* 在以下位置查找连接详情： **我的订单**
* Web 界面：使用 HTTP 端口的 URL
* SSH： `ssh -p <port> root@<proxy-address>`

## 为什么为 Blender 租用 GPU？

* 比 CPU 渲染复杂场景快 10-50 倍
* 使用多 GPU 实现更快渲染
* 无需投资昂贵硬件
* 仅为渲染时间付费

## 要求

| 场景复杂度 | 推荐 GPU   | 显存      |
| ----- | -------- | ------- |
| 简单    | RTX 3070 | 8GB     |
| 中等    | 速度       | 24GB    |
| 复杂度   | 512x512  | 24GB    |
| 生产环境  | 2s       | 40-80GB |

## 快速部署

**Docker 镜像：**

```
linuxserver/blender
```

或无头（headless）渲染：

```
nytimes/blender:3.6-gpu-ubuntu22.04
```

**端口：**

```
22/tcp
3000/http
```

## 无头渲染设置

**镜像：**

```
nvidia/cuda:12.1.0-devel-ubuntu22.04
```

**命令：**

```bash
apt-get update && \
apt-get install -y wget libxi6 libxxf86vm1 libxfixes3 libxrender1 libgl1 && \
wget https://download.blender.org/release/Blender4.0/blender-4.0.2-linux-x64.tar.xz && \
tar -xf blender-4.0.2-linux-x64.tar.xz && \
mv blender-4.0.2-linux-x64 /opt/blender
```

## 访问您的服务

部署后，在以下位置查找您的 `http_pub` URL： **我的订单**:

1. 前往 **我的订单** 页面
2. 单击您的订单
3. 查找 `http_pub` URL（例如， `abc123.clorecloud.net`)

使用 `https://YOUR_HTTP_PUB_URL` 而不是 `localhost` 在下面的示例中。

## 上传您的项目

### 通过 SCP

```bash

# 上传 .blend 文件
scp -P <port> myproject.blend root@<proxy>:/workspace/

# 上传项目文件夹
scp -P <port> -r ./project/ root@<proxy>:/workspace/
```

### 通过 rsync（大项目）

```bash
rsync -avz --progress -e "ssh -p <port>" ./project/ root@<proxy>:/workspace/project/
```

## 渲染命令

### 单帧

```bash
/opt/blender/blender -b /workspace/myproject.blend -o /workspace/output/frame_### -f 1 -- --cycles-device CUDA
```

### 动画（帧范围）

```bash
/opt/blender/blender -b /workspace/myproject.blend -o /workspace/output/frame_### -s 1 -e 250 -a -- --cycles-device CUDA
```

### 指定帧

```bash
/opt/blender/blender -b /workspace/myproject.blend -o /workspace/output/frame_### -f 1,50,100,150,200 -- --cycles-device CUDA
```

## 渲染选项

### 分辨率

```bash

# 覆盖分辨率
blender -b file.blend -o //output/frame_### -x 1920 -y 1080 -a -- --cycles-device CUDA
```

### 使用 Python 脚本

```bash
blender -b file.blend --python render_setup.py -a
```

**render\_setup.py：**

```python
import bpy

# 设置渲染引擎
bpy.context.scene.render.engine = 'CYCLES'

# 设置设备
bpy.context.preferences.addons['cycles'].preferences.compute_device_type = 'CUDA'

# 启用所有 GPU
for device in bpy.context.preferences.addons['cycles'].preferences.devices:
    device.use = True

# 设置采样数
bpy.context.scene.cycles.samples = 128

# 设置分辨率
bpy.context.scene.render.resolution_x = 1920
bpy.context.scene.render.resolution_y = 1080

# 输出设置
bpy.context.scene.render.image_settings.file_format = 'PNG'
```

## 多 GPU 渲染

对于配备多个 GPU 的服务器：

```python
import bpy

# 启用 CUDA
prefs = bpy.context.preferences.addons['cycles'].preferences
prefs.compute_device_type = 'CUDA'

# 刷新设备
prefs.get_devices()

# 启用所有 GPU
for device in prefs.devices:
    if device.type == 'CUDA':
        device.use = True
        print(f"Enabled: {device.name}")
```

## 渲染农场风格（多台服务器）

租用多台服务器并分割帧：

**服务器 1：**

```bash
blender -b project.blend -o //output/frame_### -s 1 -e 100 -a
```

**服务器 2：**

```bash
blender -b project.blend -o //output/frame_### -s 101 -e 200 -a
```

**服务器 3：**

```bash
blender -b project.blend -o //output/frame_### -s 201 -e 300 -a
```

然后在本地合并渲染结果。

## Eevee 渲染（更快）

用于实时质量：

```bash
blender -b file.blend -E BLENDER_EEVEE -o //output/frame_### -a
```

## OptiX 支持（RTX GPU）

用于 RTX 光线追踪：

```python
import bpy
prefs = bpy.context.preferences.addons['cycles'].preferences
prefs.compute_device_type = 'OPTIX'  # 替代 CUDA
```

## 自动化渲染脚本

**render.sh：**

```bash
#!/bin/bash
BLEND_FILE=$1
START_FRAME=$2
END_FRAME=$3
OUTPUT_DIR=/workspace/output

mkdir -p $OUTPUT_DIR

/opt/blender/blender -b $BLEND_FILE \
    -o ${OUTPUT_DIR}/frame_### \
    -s $START_FRAME \
    -e $END_FRAME \
    -a \
    -- --cycles-device CUDA

echo "渲染完成！"
ls -la $OUTPUT_DIR
```

用法：

```bash
chmod +x render.sh
./render.sh /workspace/myproject.blend 1 250
```

## 监控渲染进度

### 查看输出文件夹

```bash
watch -n 5 'ls -la /workspace/output/ | tail -20'
```

### Blender 输出

Blender 将帧进度打印到标准输出：

```
Fra:1 Mem:1234.56M (Peak 1500.00M) | Time:00:01.23 | Remaining:04:32.10 | Mem:567.89M, Peak:890.12M | Scene, View Layer | Sample 64/128
```

## 下载渲染帧

```bash

# 下载所有帧
scp -P <port> -r root@<proxy>:/workspace/output/ ./renders/

# 下载特定帧
scp -P <port> root@<proxy>:/workspace/output/frame_001.png ./

# 使用 rsync 同步
rsync -avz --progress -e "ssh -p <port>" root@<proxy>:/workspace/output/ ./renders/
```

## 视频编码

渲染完帧后，编码为视频：

```bash

# 安装 ffmpeg
apt-get install -y ffmpeg

# 编码为 MP4
ffmpeg -framerate 24 -i /workspace/output/frame_%03d.png -c:v libx264 -pix_fmt yuv420p output.mp4

# 编码为 ProRes（高质量）
ffmpeg -framerate 24 -i /workspace/output/frame_%03d.png -c:v prores_ks -profile:v 3 output.mov
```

## 性能优化建议

### 为速度进行优化

```python

# 为预览减少采样数
bpy.context.scene.cycles.samples = 64

# 使用自适应采样
bpy.context.scene.cycles.use_adaptive_sampling = True
bpy.context.scene.cycles.adaptive_threshold = 0.01

# 限制反弹次数
bpy.context.scene.cycles.max_bounces = 8
```

### 内存优化

```python

# 对高分辨率使用平铺渲染
bpy.context.scene.render.use_persistent_data = True

# 设置平铺大小
bpy.context.scene.cycles.tile_size = 256  # 针对 GPU
```

## 渲染时间估计

| 场景   | GPU     | 分辨率   | 采样   | 每帧时间    |
| ---- | ------- | ----- | ---- | ------- |
| 简单   | 速度      | 1080p | 128  | \~30s   |
| 中等   | 速度      | 1080p | 256  | \~2min  |
| 复杂度  | 512x512 | 4K    | 512  | \~10min |
| 生产环境 | 2s      | 4K    | 1024 | \~20min |

## 成本计算

**示例：250 帧动画**

```
GPU：RTX 4090
每帧时间：2 分钟
总渲染时间：500 分钟 = 8.3 小时
小时费率：$0.04
总成本：~$0.33
```

## # 使用固定种子以获得一致结果

### "未找到 CUDA 设备"

```python

# 检查可用设备
import bpy
prefs = bpy.context.preferences.addons['cycles'].preferences
prefs.compute_device_type = 'CUDA'
prefs.get_devices()
for d in prefs.devices:
    print(d.name, d.type)
```

{% hint style="danger" %}
**内存不足**
{% endhint %}

* 降低贴图分辨率
* 使用更小的平铺大小
* 启用“persistent data”
* 使用更简单的着色器

### 渲染缓慢

* 检查 GPU 是否正在使用（nvidia-smi）
* 优化场景几何体
* 使用降噪并减少采样数

## 使用以下方式支付

* 运行 Jupyter 进行后期处理
* [AI 视频生成](https://docs.clore.ai/guides/guides_v2-zh/shi-pin-sheng-cheng/ai-video-generation)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-zh/qi-ta-gong-zuo-fu-zai/blender-rendering.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
