通过Blender python渲染和保存图像 [英] rendering and saving images through Blender python

查看:692
本文介绍了通过Blender python渲染和保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Blender中的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.

推荐答案

这是我在Blender 2.81a中所做的事情:

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

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

此代码创建一个"VR全景图"(一个对象的一系列图片,从对象周围的不同角度来看)..

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

我以这种算法结束了:

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

这篇关于通过Blender python渲染和保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆