Skip to content

Python driven Blender

Nicolas Cottaris edited this page Oct 30, 2013 · 66 revisions

This is a guide on procedural scene generation using the Blender-Python API that ships with Blender.

Getting setup

To craft a scene using Python scripts, it is imperative to start Blender from within a terminal. This is the only way that you can see error messages generated by Blender's engine and any output generated by your print statements. In Mac OSX, generate a file called startBlender.command and enter the following into it.

#!/bin/sh
/Applications/Blender/blender.app/Contents/MacOS/blender

Next, go to Finder and double click on the startBlender.command icon. Blender should launch together with a Terminal.app.

In Blender, click on the Screen Layout button (button to the right of Help) in the Information viewport, and select the Scripting mode. This will open the following viewports: Text Editor, Python Console and 3D View.

In the Text Editor viewport, click on Text -> Create Text Block (or alternatively, press Control+N). This will create an empty document. Enter the following print command,

print('Hello from the Blender-Python API !');

then hit the Run Script button. You should see Hello from the Blender-Python API ! in the Terminal that launched with Blender. Great!

We will not proceed with generating a scene procedurally by calling methods of the Blender-Python API (bpy module). To ease this process, we have created a Python module (called SceneUtilsV1.py) for generating and positioning various scene components. Using this module the user can experience the power of procedurally-generated scenes without having to first learn the intricacies of the bpy module. More experienced users are welcome to modify and extend the supplied module to fit their needs.

Download the SceneUtilsV1.py' module. In the following example, it is assumed that the SceneUtilsV1.pymodule exists at/Users/Shared/PythonDrivenBlender/Toolboxes. It is also assumed that an empty directory named ColladaExportsexists at/Users/Shared/PythonDrivenBlender`.

Clone this wiki locally