We create a basic scene with Camera and Lights.
In order to have a place for our new objects, we need a scene The Scene does not have an ObData but it does have a list of children.
Once we have a scene, we can add objects. They all follow the same pattern.
- create the Object
- create the ObData
- link the ObData to the Object
- link the Object to a Scene.
- move and rotate the object if necessary
Add a Scene
First we do our scene like this:
scene = B.Scene.New('MyScene') # a new scene called MyScene scene.makeCurrent() # make this the current scene
Add a Lamp
# create object, obdata and link lamp_obj = B.Object.New('Lamp') lamp_data = B.Lamp.New('Lamp') lamp_obj.link( lamp_data) scene.link(lamp_obj) # position object lamp_obj.loc = 0, 0, 10 # position at x,y,z
That last line 'lamp_obj.loc = ' might seem a little funny since it looks like we are assigning three values to a single attribute. However, Python knows that lamp_obj.loc is tuple of three numbers so it tries to help you out. We could assign to the LocX, LocY and LocZ attributes individually, but that is more work! Sometimes it is good to be lazy, if you can be smart about it.
Add a Camera
# create object, obdata and link cam_obj = B.Object.New('Camera') cam_data= B.Camera.New('ortho') cam_obj.link(cam_data) scene.link(cam_obj) # position object cam_obj.loc = (0, -15, 2) # rotate camera to look towards origin cam_obj.rot = math.radians(90), 0, 0
The Whole Scene
Here is the whole thing together. As an added bonus, at the end, we print the names of all the objects in our new scene:
import Blender as B import math print '\n***' # create a new scene scene = B.Scene.New('MyScene') scene.makeCurrent() print 'current scene is', scene.name cam_obj = B.Object.New('Camera') cam_data= B.Camera.New('ortho') cam_obj.link(cam_data) scene.link(cam_obj) scene.objects.camera = cam_obj cam_obj.loc = (0, -15, 2) # rotate camera to look towards origin cam_obj.rot = math.radians(90), 0, 0 lamp_obj = B.Object.New('Lamp') lamp_data = B.Lamp.New('Lamp') lamp_obj.link( lamp_data) scene.link(lamp_obj) lamp_obj.loc = 0, 0, 10 B.Redraw(-1) #update the User Interface # print the names of all objects in our scene for i in scene.getChildren(): print i.name
Now that we can create a new scene with a camera and a lamp, let's add some more interesting objects.
Blender has two mesh modules - NMesh and Mesh. We are going to use Mesh. This one is newer and more actively supported. Unlike Mesh, the older NMesh module makes copies of any data it uses. This can be slow and use up lots of memory when dealing with large meshes.
note: the plural of vertex is vertices.
From editing Meshes as a Blender user, you know a Mesh is made up of vertices, edges and faces. Each vertex is a 3 dimensional point with x,y,z coordinates. Internally, a Mesh holds 3 lists, one for vertices, one for edges and one for faces.
The vertex list is simply all the mesh vertices in no particular order. The other lists reference the vertices by their position in the list, starting with zero.
Since an edge has a vertex at each end, the edge list stores pairs of vertices using the index of each vertex in the vertices list.
Similarly, a face consists of 3 or 4 vertices so we store the face information as triplets or quads of vertices again using the index number from the vertex list.
This script creates a triangular mesh with a single face
import Blender as B # vertices p1 = [0,0,0] p2 = [1,0,0] p3 = [0.5, 1, 1] verts = [p1,p2,p3] faces = [0,1,2] # create Object ob = B.Object.New('Mesh', 'MeshOb') # create ObData me = B.Mesh.New('myMesh') # add vertices me.verts.extend( verts ) # create faces me.faces.extend( faces ) ob.link( me ) scene = B.Scene.GetCurrent() scene.link( ob ) B.Redraw(-1) # print out the verts, edges and faces of our mesh for v in me.verts: print v for e in me.edges: print e for f in me.faces: print f