Python driven object creation. Newbie start script.

If you need to visualize data from somewhere, you can generate a python script for that data and open/run it in Blender.
I struggled for a while on how to do this, but now, thanks to the new python API, it’s finally possible.

I use it to dump out my geometric data from a C++ program, but if your environment can spit out a text file, you’re in business. With this method, you can now take your Quarterly sales and Revenue 3d clouds and make a video for your next presentation !

All you need is to generate a script that looks like this:

import Blender
from Blender import NMesh
import sys
scene=Blender.Scene.GetCurrent()

obj=Blender.Object.New(‘Mesh’,‘Cell0-a’)
obj.setLocation(0,0,0)
obj.Layer=5
obj.setDrawType(8)
me=obj.getData()
v=NMesh.Vert(-8.820767,+0.364396,-16.444286)
me.verts.append(v)
v=NMesh.Vert(-3.188777,+0.632404,-10.297288)
me.verts.append(v)
v=NMesh.Vert(-8.412910,-0.345921,+1.699220)
me.verts.append(v)
f=NMesh.Face()
f.append(me.verts[0])
f.append(me.verts[1])
f.append(me.verts[2])
me.faces.append(f)
me.update()

scene.link(obj)
Blender.Redraw()
sys.stdout.flush()

The mesh object is placed in the location specified (here 0,0,0) and the mesh’s vertices are appended one by one. You have to remember the order in which you added them so you can reference them later to create the mesh faces. Append the faces and you’re done.
This mesh shows up on the 1st & 3rd Layer ( because layers are binary flags ) and the setDrawMode(8) is to show the object name. DrawMode and DrawType are switched in the current version, it’s a bug that will go away in the next release, that’s why I use DrawType here.

I hope some of you will find this useful.
J.D.