通过 Blender python 渲染和保存图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14982836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:59:34  来源:igfitidea点击:

rendering and saving images through Blender python

pythonrendersaveblender

提问by user2047506

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.

我正在尝试通过 Blender 中的 python 脚本渲染和保存多个图像。我知道如何通过 Blender GUI 渲染和保存图像,但我想通过我的脚本来完成这一切,因为我使用了一组嵌套循环并且需要保存多个图像。我能够渲染图像,我想在输出成功的情况下保存图像。但我不确定它保存到哪里,当我尝试编辑文件路径时,它给了我上下文不正确的错误。

回答by Dirk

something like this:

像这样:

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()

回答by shybovycha

Here's what I've done in Blender 2.81a:

这是我所做的Blender 2.81a

bpy.context.scene.render.filepath = '/home/user/Documents/image.jpg'
bpy.ops.render.render(write_still = True)

This code creates a "VR panorama" (a series of pictures of an object, from different perspectives around it).

这段代码创建了一个“VR 全景图” (一个物体的一系列图片,从它周围的不同角度看)

I ended with this algorithm:

我以这个算法结束:

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

You shall end up with a number of pictures (defined by rotation_steps)around your object under /Users/myusername/Pictures/VRdirectory:

您将在目录下的对象周围得到许多图片(由 定义rotation_steps/Users/myusername/Pictures/VR

def rotate_and_render(output_dir, output_file_format, rotation_steps = 32, rotation_angle = 360.0):
  bpy.ops.object.add(type = 'EMPTY')
  origin = bpy.context.object

  for step in range(0, rotation_steps):
    origin.rotation_euler[2] = radians(step * (rotation_angle / rotation_steps))
    bpy.context.scene.render.filepath = output_dir + (output_file_format % step)
    bpy.ops.render.render(write_still = True)

  bpy.ops.object.delete(confirm = False)

rotate_and_render('/Users/myusername/Pictures/VR', 'render%d.jpg')

回答by Mahesh Natarajan

You will have to do the following. The iin the second line after the forloop is the loop index of the file loop.

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

I have verified that this works while running in the console and also from the command line. Don't forget to remove the objects after you render one file. (The remove command is not given here since it is not generic. Some specific arguments will be needed in that command if that object has links)

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

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)