• 7

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

rendering and saving images through Blender python

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.

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)

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

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

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')
  • 38
Reply Report
      • 2
    • @SibbsGambling I think it's because Blender also has an option to render preview images (much like when you press F12). So actually saving the render as a still image is somewhat optional.

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
  • 9
Reply Report
      • 2
    • Without 'write_still=True' as parameter for the 'render()' function blender did not create an image in my case.

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.filepath = ".pic%0.2d.jpg"%i
bpy.ops.render.render(use_viewport = True, write_still=True)
  • 0
Reply Report

Trending Tags