Workflow for using Blender in Python Console Mode

Arrange the Workflow Windows

A workflow to create a test scene in Blender, export to a scene description for the renderer, and finally render an image via rs-pbrt might look like this:

Arrange the workflow windows

On the left you see Blender, where the scene will get created.

On the upper right there is a shell from where Blender got started. All the (e.g. error) messages from Blender go there.

On the lower right you will render an exported scene description in .pbrt file format via the executable rs_pbrt.

Still on the right, but in the middle of both shells, there is another shell which runs Blender without a graphical user interface (GUI). Just a Python console which got started like this:

# your path to the blender executable might differ!
/usr/local/blender/blender -b --python-console

I will refer to this shell as python-console from now on.

After creating a simple triangle (see previous blog post) within Blender the GUI of Blender might look like this:

A triangle created in Blender

Now simply save the file by pressing F2 and naming the Blender scene file e.g. triangle.blend before pressing the Save as Blender File button.

In the python-console first import the bpy (Blender Python) module (has to be done only once) and make sure that pbrt is a know file format:

>>> import bpy
>>> dir(bpy.ops.export_scene)
['autodesk_3ds', 'fbx', 'obj', 'pbrt', 'x3d']

If pbrt is not in the list printed out above you have to make sure the io_scene_pbrt add-on is installed (see previous blog post) and that you pressed Save User Settings in the Blender User Preferences (press Ctrl-Alt-U). This will override the startup file preferences and make sure that even the python-console loads the add-on during startup.

Then type this command to load the scene you just exported:

>>> bpy.ops.wm.open_mainfile(filepath="/home/jan/tmp/rs_pbrt/triangle.blend")
Read blend: /home/jan/tmp/rs_pbrt/triangle.blend

Remember that we don't have a camera yet and even if we would have one, we couldn't see anything without a light. Exporting the scene in .pbrt file format adds both to the scene and also adds a material. So let's do that within the Blender GUI version using the Python Console area there:

>>> bpy.ops.export_scene.pbrt(filepath = "/home/jan/tmp/rs_pbrt/triangle.pbrt")
bpy.ops.export_scene.pbrt(filepath = "/home/jan/tmp/rs_pbrt/triangle.pbrt")
WARNING: no camera, let me create one for you ;)
WARNING: no light emitters, let me create a point light for you ;)
INFO: /home/jan/tmp/rs_pbrt/triangle.pbrt
WARNING: create new material Triangle_mat

Now the GUI of Blender might look like this:

Simplest scene to render

Notice that the light is at the same position as the camera and that I raised the energy of the light to 10 (instead of 1).


Now the workflow is like this:

  1. Change something within the Blender GUI to modify the scene and save the .blend file.

  2. Use the python-console shell to read the .blend file and export the .pbrt file:

    bpy.ops.export_scene.pbrt(filepath = "/home/jan/tmp/rs_pbrt/triangle.pbrt")
  3. Render the exported .pbrt scene from the command line of the render shell:

    # you might have to specify the full path to rs_pbrt
    rs_pbrt -i triangle.pbrt
    # output
    pbrt version 0.4.4 [Detected 4 cores]
    Copyright (c)2016-2018 Jan Douglas Bert Walter.
    Rust code based on C++ code by Matt Pharr, Greg Humphreys, and Wenzel Jakob.
    Film "image"
      "integer xresolution" [960]
      "integer yresolution" [540]
    Sampler "sobol"
      "integer pixelsamples" [8]
    Integrator "directlighting"
    BVHAccel::recursive_build(..., 1, ...)
    PT0.000146983S seconds for building BVH ...
    PT0.000000162S seconds for flattening BVH ...
    Rendering with 4 thread(s) ...
    2040 / 2040 [========================================================================] 100.00 % 622.21/s
    Writing image "pbrt.png" with bounds Bounds2 { p_min: Point2 { x: 0, y: 0 }, p_max: Point2 { x: 960, y: 540 } }
  4. You can look at the resulting image either with another program or even within Blender:

Resulting image within Blender

Repeat this workflow to modify the scene and render until you either break something with the exporter add-on (report a bug) or you run into some unsupported features (open a feature request) or you are done with your scene.