rendering and saving images through Blender python(通过 Blender python 渲染和保存图像)
问题描述
我正在尝试通过搅拌机中的 python 脚本渲染和保存多个图像.我知道如何通过 Blender GUI 渲染和保存图像,但我想通过我的脚本来完成这一切,因为我使用了一组嵌套循环并且需要保存多个图像.我能够渲染图像,我猜想在输出成功的情况下保存图像.但我不确定它保存到哪里,当我尝试编辑文件路径时,它会给我上下文不正确的错误.
I am trying to render and save multiple images through python script in blender. I know how to render and save the image through the Blender GUI but I want to do it all through my script since I am using a set of nested loops and need to save multiple images. I am able to render the image and I guess save the image with the output being successful. But I am not sure where it saves to and when I try to edit the filepath it gives me the error of the context being incorrect.
推荐答案
下面的代码创建了一个VR全景";(一个物体的一系列图片,从它周围的不同角度).
The code below creates a "VR panorama" (a series of pictures of an object, from different perspectives around it).
我最终得到了这个算法:
I ended up with this algorithm:
- 创建或加载您要拍摄的对象(主题)
- 缩放它并添加一些漂亮的照明(以便从您想要的方向看到对象);您可以通过渲染场景来检查照明(使用 F12 键)
创建一个Empty
对象,并将其位置设置为主体中心,旋转为identity (0, 0, 0
)- 将您的相机视图设置为起始位置(再次检查渲染)
- 打开交互式 Python shell (Shift+F4)
- 粘贴&运行脚本
- create or load an object you are going to take pictures of (the subject)
- scale it and add some nice lighting (so that the object is visible from directions you want); you can check the lighting by rendering the scene (using the F12 key)
create anEmpty
object and set its position to the center of the subject and rotation to identity (0, 0, 0
)- set your camera view to the starting position (check it with rendering, again)
- open interactive Python shell (Shift+F4)
- paste & run the script
您最终会在 /Users/myusername/Pictures/VR
下的对象周围获得许多图片(由 rotation_steps
定义)目录:
You shall end up with a number of pictures (defined by rotation_steps
) around your object under /Users/myusername/Pictures/VR
directory:
def rotate_and_render(output_dir, output_file_pattern_string = 'render%d.jpg', rotation_steps = 32, rotation_angle = 360.0, subject = bpy.context.object):
import os
original_rotation = subject.rotation_euler
for step in range(0, rotation_steps):
subject.rotation_euler[2] = radians(step * (rotation_angle / rotation_steps))
bpy.context.scene.render.filepath = os.path.join(output_dir, (output_file_pattern_string % step))
bpy.ops.render.render(write_still = True)
subject.rotation_euler = original_rotation
rotate_and_render('/Users/myusername/Pictures/VR', 'render%d.jpg')
您必须选择要渲染的对象.或者,您可以使用 Blender 的 Python API 在场景并将其作为 subject
参数传递给函数:
You will have to select the object you want to render. Alternatively, you can use Blender's Python API to find the object in the scene and pass it as a subject
param to the function:
rotate_and_render('/Users/myusername/Pictures/VR', 'render%d.jpg', subject = bpy.data.objects["Cube"])
这篇关于通过 Blender python 渲染和保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:通过 Blender python 渲染和保存图像
基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01