Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 203
user2047506 Avatar asked Feb 20 '13 14:02

user2047506


People also ask

Can you use Blender with Python?

Python in BlenderBlender provides its Python modules, such as bpy and mathutils , to the embedded interpreter so they can be imported into a script and give access to Blender's data, classes, and functions. Scripts that deal with Blender data will need to import the modules to work.

How do I save a Python script in Blender?

In the Text Editor, find the “Save As” option under the “Text” menu, and choose a filename for saving your script—perhaps call it TetrahedronMaker.py. That's it. This saved text file is now ready for installation into your Blender user settings, or distribution to others for installation in their settings.

Can you render photos in Blender?

Blender can render your images and animations in a variety of different formats, such as jpeg, png, and mp4. There are key differences between each of these file formats in terms of how information is stored but for now, use the PNG format as it is the traditional file format for most renders.


1 Answers

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

I ended up 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_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') 

You will have to select the object you want to render. Alternatively, you can use Blender's Python API to find the object in the scene and pass it as a subject param to the function:

rotate_and_render('/Users/myusername/Pictures/VR', 'render%d.jpg', subject = bpy.data.objects["Cube"]) 
like image 79
shybovycha Avatar answered Sep 21 '22 08:09

shybovycha