# Blender 3D: Blending Into Python/2.5 quickstart

In Blender 2.5 bpy replaces the old Blender module, introducing with it a completely new Python API. This page is designed to give an overview of how to perform key scripting tasks with bpy, and should be of interest both to newcomers and those transitioning from the 2.4x releases.

## The console

The built-in Python console has had a major overhaul in Blender 2.5, and now supports autocompletion (press Ctrl+Space). This is great for exploring all the new functions, though inline documentation is sparse.
Latest version (14-7-2010) activate console: Shift+F4.

## Context + Operation = Result

The 2.5 series introduces the bpy.ops module. Once you register your script as an "operation" it will integrate directly into the UI and can appear as a standard button identical to the built-in tools (which also use ops). Usefully for programmers, the function call of each operation appears in the tooltip of its button.
There is also a big conceptual change in ops: instead of being explicitly passed a target, operations deduce one from Blender's current "context". The context includes an array of what is currently selected, the current active object or bone, the active editing mode, the state of the Blender window, user preferences, and more besides.
Operations take no arguments and return no values. For this reason calling them from within complex scripts is considered sloppy, since you must indirectly set the operation's target, then (unless you already have a pointer) hunt indirectly for its results. Calling objects' functions directly is still supported, of course; unfortunately, as of 2.5 alpha 0 there are still a lot of ops that can't be replicated with direct calls.

## Performing tasks in 2.5

Remember that ops functions do not return a value. After calling one to create an object, you will need to get a handle to the result yourself!

### Creation and destruction
#### bpy.ops.object.add(type='EMPTY')

    Creates an object and empty datablock, and links it to the current scene. See also #Manual object creation.

#### bpy.ops.scene.new(type='EMPTY')

    Creates a new scene. Can be a clone or full duplicate of the current scene.

#### mesh_object.data.add_geometry(verts, edges, faces)

    Adds verts, edges and faces to a mesh. Object mode only.

#### object.modifiers.new(type="ARMATURE",name="Armature")

    Adds a modifier to an object.

#### bpy.ops.mesh.edge_face_add

    If two verts are selected, creates an edge. If three or more verts are selected, creates face(s) as well.

#### bpy.ops.mesh.primitive_cube_add

    Adds a cube to the current mesh object. Other primitive_*_add functions exist.

#### bpy.ops.armature.bone_primitive_add(name="Bone")

    Adds a bone to the active armature.

#### bpy.ops.*.delete

    Deletes the selected objects, mesh, etc. Many versions exist for different types.

#### Manual object creation

If you need finer control than ops.object.add provides, or just return values, follow this example:
There is also add_armature, add_image, add_lamp, add_material and add_texture.

### Getting objects and data
#### bpy.data.*

    A group of collections of Python objects. There are many - one for each type. As with normal dictionaries you can pick objects out by name or by index, to add new datablocks use new or load for images, sounds and fonts.

#### object.data

    An object's datablock.

#### mesh.data.vertices

    A mesh's vertex collection. Read-only.

#### armature.data.edit_bones

    An armature's bone dict, in object and edit mode respectively. If you access the wrong object for the current context, it will appear empty.

### Context management
#### bpy.ops.object.mode_set(mode='EDIT')

    Set the current mode. Note that the mode may not end up exactly as you set it here: for instance, EDIT can become EDIT_ARMATURE.

#### bpy.context.active_base

    These functions return the user's currently active item, which is generally the last one of its type selected. active_base returns a generic Python type.
    To set the active item, assign a Python object to one of the following values:
   * scene.objects.active
   * armature.data.bones.active or armature.data.edit_bones.active
    The active object is highlighted white in the outliner.

#### bpy.context.selected_bases

    Returns an array of the currently-selected objects. Other functions exist for editbones, "editable objects" and pchans.

#### object.selected

    Boolean value defining whether the object is selected.

## Registering an operation

See .blender\scripts\templates\operator.py. With your script registered, you will be able to add it to the Tool shelf and launch it from the spacebar menu.