# # w3d2 exporter for Blender v2.11+ # Based on Flure's w3d script; exports raw data in easy to parse format # Press alt-p in this window and each mesh will be exported to a file # Comment out corresponding FILE.write lines for info you don't want/need # import Blender from math import sqrt version = "W3D24" def calcNormal(face, verts, n): # If it's a quad, calculate normal from diagonal instead # Assume verts are sorted in circular fasion (i.e. next to each other) m = (3 * n) | 1 # when n=0 (tri), m=1; when n=1 (quad), m=3 vectorU = [verts[face[0]][0] - verts[face[2]][0], \ verts[face[0]][1] - verts[face[2]][1], \ verts[face[0]][2] - verts[face[2]][2]] vectorV = [verts[face[n]][0] - verts[face[m]][0], \ verts[face[n]][1] - verts[face[m]][1], \ verts[face[n]][2] - verts[face[m]][2]] normal = [(vectorU[1] * vectorV[2]) - (vectorU[2] * vectorV[1]), \ (vectorU[2] * vectorV[0]) - (vectorU[0] * vectorV[2]), \ (vectorU[0] * vectorV[1]) - (vectorU[1] * vectorV[0])] nLength = sqrt((normal[0] ** 2) + (normal[1] ** 2) + (normal[2] ** 2)) return (normal[0]/nLength, normal[1]/nLength, normal[2]/nLength) scene = Blender.getCurrentScene() for name in scene.objects: if Blender.isMesh(name): print("Writing %s...\n" % name) fileName = "%s.w3d" % name FILE = open(fileName, "w") FILE.write("%s\n\n" % version) FILE.write("MESH\t%s\n\n" % name) object = Blender.getObject(name) mesh = Blender.getMesh(name) faces = mesh.faces materials = object.materials vertices = mesh.vertices vnormals = mesh.normals texcoords = mesh.texcoords vcolors = mesh.colors # Total vertices and faces; comment if not useful FILE.write("TOTALFACES\t%s\n" % len(faces)) FILE.write("TOTALVERTS\t%s\n\n" % len(vertices)) # the j is to keep track what iteration I'm on in the # "for face in faces" loop so I can make sure I'm on the # right set of UV coords j = 0 for face in faces: if face[3]: totalVerts = 4 else: totalVerts = 3 # 1 for quad, 0 for triangle FILE.write("QUAD\t%s\n" % (totalVerts - 3)) # 1 to smooth, 0 to not smooth FILE.write("SMOOTH\t%s\n" % face[4]) # print face's "natual" material info matIndex = face[5] if materials and materials[matIndex]: material = Blender.getMaterial(materials[matIndex]) FILE.write("COLOR\t%s\t%s\t%s\n" % (material.R, material.G, material.B)) try: FILE.write("SPECCOLOR\t%s\t%s\t%s\n" % (material.SpecR, material.SpecG, material.SpecB)) FILE.write("SPEC\t%s\n" % material.Spec) FILE.write("HARD\t%s\n" % material.Hard) FILE.write("ALPHA\t%s\n" % material.Alpha) FILE.write("DIFFUSE\t%s\n" % material.Ref) except: print "Blender Version < 2.12, skipping most material properties\n\n" # normal caculation normal = calcNormal(face, vertices, totalVerts - 3) FILE.write("NORMAL\t%s\t%s\t%s\n" % (normal[0], normal[1], normal[2])) # loop to keep uv, vertex, vertex normal, and vertex colors # grouped per set for i in range(totalVerts): if texcoords: FILE.write("UVCOORD\t%s\t%s\n" % (texcoords[j][i][0], texcoords[j][i][1])) FILE.write("VERTEX\t%s\t%s\t%s\n" % (vertices[face[i]][0], vertices[face[i]][1], vertices[face[i]][2])) FILE.write("VNORMAL\t%s\t%s\t%s\n" % (vnormals[face[i]][0], vnormals[face[i]][1], vnormals[face[i]][2])) if vcolors: FILE.write("VCOLOR\t%s\t%s\t%s\n" % (vcolors[face[i]][0], vcolors[face[i]][1], vcolors[face[i]][2])) # print first image map for uvcoords to use # to be updated when we get access to other textures if mesh.texture: FILE.write("UVTEXTURE\t%s\n" % mesh.texture) FILE.write("\n") j = j + 1 FILE.close() print scene