Writing a scene exporter addon for Blender

This tutorial will explain how to pull the scene data(meshes, lights, cameras, animations) from Blender and dump them into a desired file format for our use.  This is what every addon in the Blender does.  This article emphasize more on how to read the required data from blender scene, not how exactly the procedures to put them into an addon.

Blender Exporter Menu

First of all, Blender is a 3D modeling/animation platform is built using C++ and Python.  Python is used for extending its existing functionalities and is released as the Blender-Python API.  It can be found here.

Scene analysis using Python code

Before we can start writing the addon, it is recommended to work out some simple python scripts to examine the scene structure of a typical Blender scene. We can access the scene parameters such as the scene nodes, meshes, transformation matrices, materials, textures, uvmaps, all through tiny snippets of python code.  For the convenience I created a checklist of sample programs and started working out it one by one for achieving more clarity about the Blender’s scene data.

Print all the top level scene objects(scene roots)

import bpy

for scene in bpy.data.scenes:

Print the node names in the scenegraph

We’d consider the first scene, ie scene[0] to enumerate.

import bpy

for node in bpy.data.scenes[0].objects:

Blender Scene Objects

Print all the mesh names

import bpy

for mesh in bpy.data.meshes:

Print the number of vertices and vertex normals

import bpy

sel_mesh = bpy.context.object.data

print("Number of vertices: %d" %(len(sel_mesh.vertices)))
print("Number of normals: %d" %(len(sel_mesh.vertices)))
print("These are vertices...")
for v in sel_mesh.vertices:
    print("(%f, %f, %f)" %(v.co.x, v.co.y, v.co.z))

print("These are vertex normals...")
for v in sel_mesh.vertices:
    print("(%f, %f, %f)" %(v.normal.x, v.normal.y, v.normal.z))

Print the material names a given mesh use

import bpy

sel_mesh = bpy.context.object.data

print("Number of materials: %d" %len(sel_mesh.materials))

for mtl in sel_mesh.materials:
    print("Material name : %s" %mtl.name)

Mesh Materials View



Print the names of UV maps a mesh use

import bpy

sel_mesh = bpy.context.object.data

print("UVMaps of mesh \"%s\"" %sel_mesh.name)
for uvmap in sel_mesh.uv_layers:
    print("%s" %(uvmap.name))

Mesh UVMaps

Print the name of the texture maps associated with a material

In Blender, a material owns a number of texture slots which together decides the appearance of the material when rendering. A single texture slot consists of a texture object, and every texture object is associated an image as well as a UVMap. A number of blending parameters are also associated with the texture slot which is beyond the scope of this article.

import bpy

sel_mesh = bpy.context.object.data

for mtl in sel_mesh.materials:
    for texslot in mtl.texture_slots:
        if(texslot is not None):


Print all the images used by the scene

import bpy

for img in bpy.data.images:


Save all images to drive

import bpy

img_dir = "D:\\blend_images"
for img in bpy.data.images:
    img.save_render(img_dir + "\\" + img.name + ".png")


Print all the vertices and corresponding UV coordinates

Here we assume that the mesh has atleast one UVMap is associated with the mesh which is “unwrapped” already.   Then the following code snippet will list all the vertices and their UV coordinates.

import bpy

sel_mesh = bpy.context.object.data
uvmap = sel_mesh.uv_layers[0].data

for poly in sel_mesh.polygons:
  for loop_idx in poly.loop_indices:
    vert_idx = sel_mesh.loops[loop_idx].vertex_index
    print("Vertex: (%f, %f, %f)" %(sel_mesh.vertices[vert_idx].co.x, sel_mesh.vertices[vert_idx].co.y, sel_mesh.vertices[vert_idx].co.z))
    print("UV Coord: (%f, %f)" %(uvmap[loop_idx].uv.x, uvmap[loop_idx].uv.y))


I hope this information is enough for writing the basic version of a scene exporter addon.   In the next article, I’ll explain how to explore the animation data.

Leave a reply

Your email address will not be published.

You may use these HTML tags and attributes:
