From BlenderWiki

Jump to: navigation, search
Note: This is an archived version of the Blender Developer Wiki. The current and active wiki is available on wiki.blender.org.

2.5 Python I/O API Tutorial

Starter Tips

  • use outliner -> datablocks to discover API data, properties and functions
  • use source/blender/makesrna/intern/rna_*_api.c code to examine API functions in detail, what parameters they take, what is the return type, and more. For example rna_mesh_api.c
  • read code! There are "live" examples of I/O API usage under release/scripts/io.

Input

Create a mesh object:

ob = bpy.data.add_object("MESH", "mesh-name-here")

Allocate verts, faces, edges:

me = ob.data
me.add_geometry(num_verts, num_edges, num_faces) # any parameter can be 0

Fill in vertex data:

me.verts.foreach_set("co", coords)

coords must be a "flat" sequence of floats, e.g. if vertices are ((0, 0, 0), (1, 2, 3), ...), then coords should be (0, 0, 0, 1, 2, 3)

unpack_list() from import_obj.py can be used to "flatten" a list of verts.

Fill in face data:

me.faces.foreach_set("verts_raw", indices)

indices is a "flat" list of vertex indices. Again, unpack_face_list() from import_obj.py can be used to "flatten" a list of faces (triangles and quads).

Output