36

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.

4

3 回答 3

48

下面的代码创建了一个“VR 全景图” (一个对象的一系列图片,从它周围的不同角度)

我最终得到了这个算法:

  1. 创建或加载您要拍摄的对象(主题
  2. 缩放它并添加一些漂亮的照明(以便从您想要的方向看到对象);您可以通过渲染场景来检查照明(使用F12键)
  3. 创建一个Empty对象并将其位置设置为主体的中心,并将旋转设置为标识 ( 0, 0, 0)
  4. 将您的相机视图设置为起始位置(再次使用渲染检查)
  5. 打开交互式 Python shell ( Shift+F4)
  6. 粘贴并运行脚本

您最终会在目录下的对象周围获得许多图片(由 定义rotation_steps) :/Users/myusername/Pictures/VR

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参数传递给函数:

rotate_and_render('/Users/myusername/Pictures/VR', 'render%d.jpg', subject = bpy.data.objects["Cube"])
于 2013-07-11T21:52:09.570 回答
11

像这样的东西:

import bpy

bpy.context.scene.render.filepath = 'pathToOutputImage'
bpy.context.scene.render.resolution_x = w #perhaps set resolution in code
bpy.context.scene.render.resolution_y = h
bpy.ops.render.render()
于 2013-04-09T15:37:23.263 回答
1

您将必须执行以下操作。循环后第二i行的for是文件循环的循环索引。

我已经验证这在控制台和命令行中运行时有效。渲染一个文件后不要忘记删除对象。(此处未给出删除命令,因为它不是通用的。如果该对象具有链接,则该命令中将需要一些特定参数)

for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
        area.spaces[0].viewport_shade = 'RENDERED'

bpy.context.scene.render.image_settings.file_format='JPEG'
bpy.context.scene.render.filepath = ".pic%0.2d.jpg"%i
bpy.ops.render.render(use_viewport = True, write_still=True)
于 2018-11-05T08:28:39.983 回答