The Complete Blender Pages
| NaN | BlenderStuff | UserLinks | NewsServer | Shop | Download | FAQ | NeoGeo ||
The Complete Blender pages


Python Scripting
These pages describe the integration between Blender and the Python programming language. They are not intended to teach the Python language. Programmers familiar with C/C++/Java or other high-level and/or object-oriented languages should be able to pick up the language fairly quickly with the help of a reference and examples. Further information on Python can be found at www.python.org

The Python Roadmap
  • IPO level control

    The first step in Blender/Python integration is providing the same capabilities that IPO's have to scripts. This means scripts can read and write the same properties that can be controlled by IPO's, allowing for procedural animation.

    Simple examples might included making a ball bounce along a sine curve, or adding random jitter to a camera movement.

  • Vertice-level control

    The second stage of integration is proving script access to the data found in Meshes, Curves, and Surfaces. Not only does this involve the reading/adding/deleting of vertices but also an interface for generation of fully procedural objects - allowing the user to manipulate a mesh, and a non-destructive transformation to be applied by the script.

  • And more...

    Other things in consideration or planning include more adding more Blender API calls, letting scripts execute commands just like the user would, scripts could then automate frequent or complex actions.

    We are also interested in providing the functionality necessary to write complete applications that run embedded inside a Blender window with access to the Blender UI elements.


Basic Python

Python scripts can be executed directly from the Text window by pressing Alt-P in the Text window. Scripts can also be attached to DataBlocks to be executed automatically, see the section on ScriptLinks below. Modules
The following modules for high-level object access are available:
  • Blender Main API module
    • Int: Blender.bylink Used to test if the script was executed by a scriptlink - see the ScriptLink section below

    • Method: Blender.Get(request) General data access function, used with the constants in Blender.Const

    • Module: Blender.Object Object object and functions
      • Method: Blender.Object.Get([name])

    • Module: Blender.Material Material object and functions
      • Method: Blender.Material.Get([name])

    • Module: Blender.Camera Camera object and functions
      • Method: Blender.Camera.Get([name])

    • Module: Blender.World World object and functions
      • Method: Blender.World.Get([name])
        Method: Blender.World.GetActive() Returns the active World

    • Module: Blender.Lamp Lamp object and functions
      • Method: Blender.Lamp.Get([name])

    • Module: Blender.Const Constant definitions for Blender.Get
      • Int: Blender.Const.BP_CURFRAME Request the current frame
      • Float: Blender.Const.BP_CURTIME Request the current time (includes field and motion blur calculations)

The Blender.____.Get([name]) methods all function in the same manner: If a name is specified the object with that name is returned (or PyNone), otherwise a list off all objects in that category is returned.

Object data

The objects delivered by the Blender.____.Get() functions contain data exactly matching the names found in the IPO window. For example, the energy of a Lamp is Lamp.Energ, and the Horizon Red component for a world is World.HorR.

One exception to this rule is that script access to the texture fields of IPOs (Off_, Dvar, etc.) is not currently available.

Objects delivered by the Blender.Object.Get() function also contain several special data members:
  • Object.parent The parent of the object
  • Object.data The object data (ie. mesh, lamp, camera)
  • Object.mat The 4x4 matrix for the object

  • Object.loc A 3 member vector for location
  • Object.dloc A 3 member vector for delta location

  • Object.rot A 3 member vector for rotation
  • Object.drot A 3 member vector for delta rotation

  • Object.size A 3 member vector for size
  • Object.dsize A 3 member vector for delta size

The NMesh Module

The vertex editing functionality is planned for access in two ways, low and high level. Low level access is intended for programmers familiar with mesh editing and the data structures involved (and links between them) and who intend to write intensive modules to work with Blender. High level access is for people who do not want to spend the time to handle the basic data structures themselves, or only need a quick effect. Low level editing is completely independent of Blender, while high level editing uses and builds on Blender's features.

The NMesh module represents the first step towards providing vertex level access from Python.
  • Blender.NMesh Low-level mesh editing module
    • Method: NMesh.GetRaw([name]) Get a NMesh from Blender
      • If name is not specified a new empty mesh is returned, otherwise Blender returns an existing mesh.
      • On reading Blender sets the NMesh has_col and has_uvco flags based on what data was filled

    • Method: NMesh.PutRaw(mesh, [name, renormal]) Return a NMesh to Blender
      • (mesh) The NMesh object to store
      • [name] The mesh to replace
      • [renormal] Flag to control vertex normal recalculation (True==recalculate)
      • If the name of a mesh to replace is not given a new object is created and returned.
      • On writing Blender uses the NMesh has_col and has_uvco flags to determine what data should be allocated and set. You need to manual set these to 1 if you want colors of UV coordinates to be set.

    • Method: NMesh.Vert([x, y, z]) Create a new vertice
      • [x=0.0, y=0.0, z=0.0] Specify new coordinates

    • Method: NMesh.Face() Create a new face

    • Method: NMesh.Col([r,g,b,a]) Create a new mesh color
      • [r=255, g=255, b=255, a=255] Specify the color components


  • NMesh object
    • verts List of vertices
    • faces List of faces
    • has_col Flag for whether mesh has mesh colors
    • has_uvco Flag for whether mesh has UV coordinates

    • Face object
      • v List of Vert's that form face
      • col List of vertex Col's (always size 4), matches to verts in v
      • mat Material index number for face

    • Vert object
      • co Vertex coordinate vector
      • uvco UV coordinate vector (element [2] (Z) is unused)
      • no Normal vector

    • Col object
      • r Red component
      • g Green component
      • b Blue component
      • a Alpha component
ScriptLinks



IPO level control allows Python scripts to access the same data controllable through the IPO system. Python scripts are attached to DataBlocks through the ScriptButtons window, and assigned events on which they are supposed to run.



The ScriptButtons are accessed via a new button on the ButtonsWindow header. This window has no shortcut and can only be reached by the icon. When the ScriptButtons have been selected the headerbuttons change to display the available choices for ScriptLinks.

ScriptLinks can be added for the following DataBlocks
  • Worlds - Available when the current scene contains a World
  • Lamps - Available when the active Object is a Lamp
  • Materials - Available when the active Object has a Material
  • Cameras - Available when the active Object is a Camera
  • Objects - Available when an Object is active
When you are able to add a ScriptLink an icon appears on the header, similar to the IPO Window. Selecting one of the icons brings up the ScriptLink buttons group on the left of the ScriptButtons window.



DataBlocks can have an arbitrary number of ScriptLinks attached to them - additional links can be added and deleted with the New and Del buttons, similar to Material Indexes (see manual page 274). Scripts are executed in order, beginning with the script linked at index 1.

When you have at least 1 scriptlink the Event type and link buttons are displayed. The link button should be filled in with the name of the Text object to be executed. The Event type controls at what point the text will be executed,
  • FrameChanged - This event is executed everytime the user changes frames, and during rendering and animation playback. To provide greater user interaction this script is also executed continuously during editing for Objects.
  • Thats all? - Only for now! In the future we will provide more events as integration progresses.
Scripts that are executed because of events being triggered recieve additional input through two objects in the Blender module.

The Blender.bylink (PyInt) object is set to 1 to indicate that the script has been called through a ScriptLink (as opposed to the user pressing Alt-P in the Text window).

The Blender.link (variable) object contains the DataBlock which referenced the script, this may be a Material, Lamp, Object, etc.

If attached by a ScriptLink the following script will make sure an object does not go beneath the floor.
# Keeps objects above the floor!

import Blender

if (Blender.bylink):
	ob= Blender.link
	
	if (ob.LocZ < 0.0): ob.LocZ= 0.0


Scene ScriptLinks
The ScriptLink buttons for Scenes are always available in the right of the ScriptButtons window, and function exactly in the manner described above. Scene scripts are global and recieve no input through the Blender module. Events available for Scenes are,
  • FrameChanged - This event is executed everytime the user changes frames, and during rendering and animation playback.
  • Thats all? - See "Thats all?" above!