Blender 3D: Noob to Pro/Advanced Tutorials/Python Scripting/Introduction
| Applicable Blender version: 2.49. |
Contents |
[edit] Introduction
Python is an incredible language, much akin to C, which also offers linking of C libraries, probably why it was chosen to be integrated into Blender. It was designed to incorporate the extremely quick and easy learning of ABC with the more powerful functionality of C, but at the same time keeping syntaxing and naming conventions simple.
Arguably its main function is in importing and exporting 3D models, an essential task for a 3D modeling application. With the adaptability of Python, it allows you to create custom import/export scripts easily and quickly for Blender, to service filetypes which Blender doesn't natively support.
Next comes its ability to allow automation, such as the Discombobulator, which is a fantastic (but not often used) tool, which turns simple meshes into futuristic panels.
It is also almost universally used in the BGE (Blender Game Engine), to bring life to games.
And yes, Python was named after Monty Python.
The version of Python used with Blender 2.49 is 2.6. If you are unfamiliar with Python, start with this tutorial at python.org.
[edit] First Steps In Blender Scripting
Open a new, default Blender document. Split the 3D View in two vertically. Change the type of one side to a Scripts Window. In the “Scripts” menu in the header of that window, find the “System” submenu, and select “Interactive Python Console”. The grey background should turn black, with some initial explanatory blue text; everything you type into this window will display in white, while output from the system will be in blue.
To start with, let’s find out what objects are present in the document. Type
Blender.Object.Get()
The response should be
[[Object "Camera"], [Object "Cube"], [Object "Lamp"]]
which shows you the names of the three default objects you can see in the 3D View window.
Let’s get a reference to the Cube object for more convenient access: type
Cube = Blender.Object.Get("Cube")
Now let’s try querying the value of one of its attributes: type
Cube.LocX
You should see the response
0.0
Now let’s try changing its location: type
Cube.LocX += 1
Nothing seems to happen. But now tell Blender to update its display:
Blender.Window.Redraw()
and you should see the cube jump to a new position in the 3D View. To make it move again, press UPARROW a couple of times to bring back the command that changed the LocX attribute, followed by ENTER to re-execute it; then do the same with the Redraw command. And that’s it—you’re scripting!
[edit] Multiple Running Scripts
After starting the console, you should see a popup menu in the Script Window header showing “Interactive Python C”, being the truncated name of the script you started running. You can have multiple scripts running at once, and switch between them by selecting from this menu; for example, try starting a second Python console by re-executing the “Interactive Python Console” script. To terminate the currently-running script, press CTRL + Q in this window.
[edit] The Blender Versus bpy Modules
The main functionality that Blender makes available to Python scripts is in the Blender module and its submodules. However, beginning with Blender 2.44, a new bpy module was introduced. This represents a completely revamped way of accessing Blender’s functionality, which maps in a more natural fashion onto Python language constructs.
Indeed, in the upcoming 2.5x version of Blender (still under development), the Blender module is abandoned completely, and what equivalent functionality there is must be accessed through bpy.
So, does this sound like a good reason to start using module bpy in preference to module Blender? Unfortunately, in the 2.4x series, the bpy module is still marked as “experimental”, and its functionality is far from complete.
In short, there is no way to write scripts for Blender 2.49 so that they will also work seamlessly with Blender 2.5x.