通过 Blender python 渲染和保存图像

rendering and saving images through Blender python(通过 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:

  1. 创建或加载您要拍摄的对象(主题)
  2. 缩放它并添加一些漂亮的照明(以便从您想要的方向看到对象);您可以通过渲染场景来检查照明(使用 F12 键)
  3. 创建一个Empty对象,并将其位置设置为主体中心,旋转为identity (0, 0, 0)
  4. 将您的相机视图设置为起始位置(再次检查渲染)
  5. 打开交互式 Python shell (Shift+F4)
  6. 粘贴&运行脚本
  1. create or load an object you are going to take pictures of (the subject)
  2. 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)
  3. create an Empty object and set its position to the center of the subject and rotation to identity (0, 0, 0)
  4. set your camera view to the starting position (check it with rendering, again)
  5. open interactive Python shell (Shift+F4)
  6. 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 渲染和保存图像

基础教程推荐