# This script exports the current Blender scene as a ID/Quake/Valve # 220 .map file. Each mesh is exported an "entity." Each mesh/entity # must be composed of convex shapes. Each of those convex shapes is # exported as a "brush". import Blender import sys import copy def diff(x, y): return x-y def mult(x, y): return x*y def add(x,y): return x+y def dot(x,y): return reduce(add, map(mult,x,y)) #dot product of 2 vectors, x,y def scalerotate(v, obj): # v - a point in space, obj - an object whose # scaling, rotation and translation will be applied to the point. # vt=[v[0],v[1],v[2],0] # vt=map(dot, obj.matrix, [vt,vt,vt,vt]) #matrix multiply vt=[0,0,0] for i in range(3): # matrix is transposed... grungy matrix multiply for j in range(3): vt[i]=vt[i]+ obj.matrix[j][i]*v[j] return map(add, obj.matrix[3][0:3],vt) # Translate the vector objs = Blender.Object.Get() # Get all objects in current(?) scene for obj in objs: # Loop over all objects if not obj or obj.getType() != "Mesh": # print "Skipping non mesh" continue mesh_data = obj.getData() ### mesh_data = Blender.NMesh.GetRaw(mesh.name) if not mesh_data: continue print "Exporting ",obj.name # For each face we have a list of vertices associated with that face. # To each element in the list of vertices we add a list of faces. faces=copy.copy(mesh_data.faces) #list of NMFaces while (len(faces)>0): face=faces.pop() # Grab a face from list of remaining faces setfaces=[face] # Start new list of connected faces verts={} for v2 in face.v: verts[v2]=1 # Stuff its vertices into our vertex list. found=True while found: found=False for othface in faces: #Loop thru list of NMFace data for v2 in othface.v: # Check if v2 is in our vert list if (verts.has_key(v2)): found=True # we found another connected face for v2 in othface.v: verts[v2]=1 # Stuff vertices into our vertex dict setfaces.append(othface) # put it in our sublist faces.remove(othface) # remove if from one list break # Now we have created a subset of connected faces. if len(setfaces) < 4: print "Fewer than 4 faces in set, skipping" continue # Check that the faces comprise a convex surface. # For each face we pick any vertex on that face. The dot product # between the normal vector of that face and the vector between that # vertex and any other vertex in that shap is 0 or negative convex=True for othface in setfaces: #Loop thru # Grab a vertex on this face and the normal v1= othface.v[0].co n1= othface.normal # Face's normal for v2 in verts.keys(): # for all verices in our brush v3= map(diff,v2.co,v1) # 'vector' from v1 to v2 dp=dot(v3,n1) # Dot product of v3 and n1 if (dp>1e-5): convex=False if (convex): # brush is convex. Write it out. print "{" for othface in setfaces: #Loop thru # apply the translations, rotations and scaling of the object for v2 in othface.v: vt=scalerotate(v2.co, obj); print " ( ", vt[0],vt[1],vt[2], " ) ", print # Print out UV mapping print "}"