rendering and saving images through Blender python

这一生的挚爱 提交于 2019-12-03 02:20:38

问题


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.


回答1:


Here's what I've done in Blender 2.63:

bpy.data.scenes['Scene'].render.filepath = '/home/user/Documents/image.jpg'
bpy.ops.render.render( write_still=True ) 

What this is doing is creating a VR panorama (a series of object pictures around it). And I ended with this algorithm:

  1. create or load an object you are going to take pictures of
  2. scale it and add some nice lighting (so that the object is visible from directions you want); check the lighting by rendering the scene (use F12 key)
  3. create an Empty node and set its position and rotation to identity (position: 0, 0, 0, rotation: 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

You shall end up with a number of pictures (defined by step_count) around your object inside your /home/user/VR directory.

cam = bpy.data.objects['Camera']
origin = bpy.data.objects['Empty']

step_count = 32

for step in range(0, step_count):
    origin.rotation_euler[2] = radians(step * (360.0 / step_count))

    bpy.data.scenes["Scene"].render.filepath = '/home/user/VR/vr_shot_%d.jpg' % step
    bpy.ops.render.render( write_still=True )



回答2:


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



回答3:


You will have to do the following. The i in the second line after the for loop is the loop index of the file loop.

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)

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)


来源:https://stackoverflow.com/questions/14982836/rendering-and-saving-images-through-blender-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!