210 lines
5.2 KiB
Markdown
210 lines
5.2 KiB
Markdown
# 3D 光学系统建模工具
|
||
|
||
这个项目可以将 JSON 格式的光学系统数据转换为 3D 模型,使用 Blender 生成高质量的可视化结果。
|
||
|
||
## 🚀 功能特色
|
||
|
||
- **精确几何建模**: 支持平面、抛物面、双曲面等光学面型
|
||
- **智能材质系统**: 自动应用金属、透明、发光材质
|
||
- **多格式输出**: 生成 OBJ、GLB、Blender 文件和渲染图像
|
||
- **自动场景设置**: 包含相机、灯光和渲染配置
|
||
- **错误处理**: 完善的异常处理和日志输出
|
||
|
||
## 📋 系统要求
|
||
|
||
- **Blender 3.6+** (推荐 4.0+)
|
||
- **Python 3.7+**
|
||
- **操作系统**: Windows, macOS, Linux
|
||
|
||
## 🛠️ 安装
|
||
|
||
### 1. 安装 Blender
|
||
|
||
从官网下载并安装: https://www.blender.org/download/
|
||
|
||
### 2. 验证安装
|
||
|
||
```bash
|
||
# 确保可以从命令行访问Blender
|
||
blender --version
|
||
```
|
||
|
||
### 3. 准备数据文件
|
||
|
||
确保您的 JSON 数据文件格式正确,包含以下结构:
|
||
|
||
```json
|
||
{
|
||
"p": [0.0, 0.0, 0.0],
|
||
"q": [1.0, 0.0, 0.0, 0.0],
|
||
"name": "",
|
||
"children": [
|
||
{
|
||
"p": [x, y, z],
|
||
"q": [w, x, y, z],
|
||
"name": "对象名称",
|
||
"face_type": "symm",
|
||
"face_geometry": "plane|parabola|hyperbola",
|
||
"face_f": 1.0,
|
||
"face_g": 1.0,
|
||
"draw_material": "metal|nothing"
|
||
}
|
||
]
|
||
}
|
||
```
|
||
|
||
## 🎯 使用方法
|
||
|
||
### 方法 1: 使用运行脚本(推荐)
|
||
|
||
```bash
|
||
python run_blender.py -i jsonfile.json
|
||
```
|
||
|
||
### 方法 2: 直接运行 Blender 脚本
|
||
|
||
```bash
|
||
blender --background --python toModel.py
|
||
```
|
||
|
||
### 方法 3: 在 Blender GUI 中运行
|
||
|
||
1. 打开 Blender
|
||
2. 切换到 Scripting 工作区
|
||
3. 打开 `toModel.py` 文件
|
||
4. 点击运行按钮
|
||
|
||
### 综合
|
||
|
||
python3 run_fused_system.py
|
||
(.venv) ~/Desktop/BL/ [main*] python3 run_fused_system.py
|
||
🎨 融合光学系统脚本运行器
|
||
========================================
|
||
✅ 找到 Blender: /Applications/Blender.app/Contents/MacOS/Blender
|
||
🚀 开始运行融合光学系统脚本...
|
||
光学系统数据:./e8caffb4622e03b1495bbc1ed13fce13.json
|
||
光路数据:./miao_light_path_tsingtao.json
|
||
Blender 命令:/Applications/Blender.app/Contents/MacOS/Blender
|
||
执行命令:/Applications/Blender.app/Contents/MacOS/Blender --background --python ./fused_optical_system.py -- ./e8caffb4622e03b1495bbc1ed13fce13.json ./miao_light_path_tsingtao.json
|
||
✅ 融合光学系统脚本执行成功!
|
||
|
||
## 📁 输出文件
|
||
|
||
运行成功后,将生成以下文件:
|
||
|
||
| 文件名 | 格式 | 用途 |
|
||
| --------------------------- | ------- | ------------------------------------------- |
|
||
| `optical_system.obj` | OBJ | 通用 3D 模型格式,可在大多数 3D 软件中打开 |
|
||
| `optical_system.glb` | GLB | Web 友好格式,适合在线展示 |
|
||
| `optical_system.blend` | Blender | 完整的 Blender 工程文件,包含所有材质和设置 |
|
||
| `optical_system_render.png` | PNG | 高质量渲染图像 |
|
||
|
||
## ⚙️ 配置选项
|
||
|
||
### 几何体类型支持
|
||
|
||
- **plane**: 平面镜/透镜
|
||
- **parabola**: 抛物面镜,使用 `face_f` 参数控制焦距
|
||
- **hyperbola**: 双曲面镜,使用 `face_f` 和 `face_g` 参数
|
||
|
||
### 材质类型
|
||
|
||
- **metal**: 金属反射材质(淡蓝色)
|
||
- **nothing**: 透明材质(10%不透明度)
|
||
- **其他**: 发光材质(橙色光)
|
||
|
||
### 渲染设置
|
||
|
||
- 分辨率: 1920x1080
|
||
- 引擎: Cycles(支持高质量材质)
|
||
- 格式: PNG
|
||
|
||
## 🔧 自定义
|
||
|
||
### 修改材质
|
||
|
||
编辑 `toModel.py` 中的材质创建函数:
|
||
|
||
```python
|
||
def create_metal_material():
|
||
# 修改金属材质属性
|
||
bsdf.inputs["Base Color"].default_value = (R, G, B, A)
|
||
bsdf.inputs["Metallic"].default_value = 0.0-1.0
|
||
bsdf.inputs["Roughness"].default_value = 0.0-1.0
|
||
```
|
||
|
||
### 调整几何体精度
|
||
|
||
修改分辨率参数:
|
||
|
||
```python
|
||
mesh = create_parabolic_surface(face_f, size=2.0, resolution=64) # 更高精度
|
||
```
|
||
|
||
### 修改渲染设置
|
||
|
||
```python
|
||
scene.render.resolution_x = 3840 # 4K分辨率
|
||
scene.render.resolution_y = 2160
|
||
```
|
||
|
||
## 🐛 故障排除
|
||
|
||
### 常见问题
|
||
|
||
**Q: 找不到 Blender**
|
||
|
||
```
|
||
❌ 错误:未找到Blender安装
|
||
```
|
||
|
||
A: 确保 Blender 已正确安装并添加到系统 PATH 中
|
||
|
||
**Q: 几何体创建失败**
|
||
|
||
```
|
||
创建几何体时出错: ...
|
||
```
|
||
|
||
A: 检查 JSON 数据中的 `face_f` 和 `face_g` 参数是否为有效数值
|
||
|
||
**Q: 导出失败**
|
||
|
||
```
|
||
模型导出失败
|
||
```
|
||
|
||
A: 确保有写入权限,检查磁盘空间
|
||
|
||
### 调试技巧
|
||
|
||
1. **查看详细日志**: 脚本会输出详细的创建过程
|
||
2. **检查 JSON 数据**: 确保所有必需字段都存在
|
||
3. **逐步运行**: 在 Blender GUI 中逐步执行脚本
|
||
|
||
## 📈 性能优化
|
||
|
||
### 大型数据集
|
||
|
||
- 对于超过 100 个对象的数据集,考虑分批处理
|
||
- 降低几何体分辨率以提高性能
|
||
- 使用后台模式避免 GUI 开销
|
||
|
||
### 内存使用
|
||
|
||
- 大型模型可能需要 8GB+内存
|
||
- 监控内存使用,必要时重启 Blender
|
||
|
||
## 🤝 贡献
|
||
|
||
欢迎提交 Issue 和 Pull Request 来改进这个工具!
|
||
|
||
## 📄 许可证
|
||
|
||
本项目采用 MIT 许可证。
|
||
|
||
## 🙏 致谢
|
||
|
||
- Blender Foundation 提供的优秀 3D 建模软件
|
||
- 光学系统设计社区的支持和反馈
|