The Allegro Wiki is migrating to github at https://github.com/liballeg/allegro_wiki/wiki

Blender mesh exporter

From Allegro Wiki
Jump to: navigation, search

Here is an old script for exporting meshes from blender . You need to edit the name of the mesh and the range (this was designed for exporting vector-keyed animations)


<highlightSyntax language="python">

import Blender210 as Blender

import math

import string

import sys


exportAnimations = 1



def printModuleInfo(module, example):

exec("import %s" % module)

print

print "#" * 79

exec("print %s.__doc__" % module)

print "#" * 79

exec("names = dir(%s)" % module)

for name in names:

exec('ifClause = "if type(%s.%s) == type(%s.%s): "' %

(module, name, module, example))

exec('statement = "print %s.%s.__doc__"' % (module, name))

exec(ifClause + statement)



class AllegroExport:


def __init__(self, filename):

self.filename = filename

self.file = None

self.scene = None

self.display = None

self.meshname = None


def export(self, meshname):

self.meshname = meshname


print "exporting ..."

self.file = open(self.filename, "w")

self.writeHeader()

self.writeVertexes(meshname)

self.writeTris(meshname)

self.writeEnd()


self.file.close()



def writeVertexes(self, name):

#if Blender.isMesh(name):

self.writeIdentifier(name)

object = Blender.getObject(name)

mesh = Blender.getMesh(object.data)

cname = name

cname = string.replace(cname, ".", "_")

cname = string.replace(cname, " ", "_")

cname = string.lower(cname)

self.file.write("V3D %s_v3d[]= {\n" % (cname))


index = 0

for vertex in mesh.vertices:

self.file.write(" {%2.6f, %2.6f, %2.6f},\n" %

(vertex[0],

vertex[1],

vertex[2]))

index = index + 1

self.file.write(" };\n")



def writeTris(self, name):


if Blender.isMesh(name):

meshobj = Blender.getObject(name)

mesh = Blender.getMesh(meshobj.data)


self.writeIdentifier(name)

cname = name

cname = string.replace(cname, ".", "_")

cname = string.replace(cname, " ", "_")

cname = string.lower(cname)


self.file.write("TRIS %s_tris[]= {\n" % (cname))

index = 0

for face in mesh.faces:

self.file.write(" {%s, %s, %s},\n" %

(face[0],

face[1],

face[2]))


self.file.write(" }\n")

index = index + 1

self.file.write("}\n")


else:

print "Sorry can export meshes only ..."


def writeEnd(self):

print "... finished"


def writeHeader(self):

self.file.write("/* file exported by blender mesh_exp.py\n")

self.file.write(" script by Matt Smith 2003 */\n")


def writeIdentifier(self, name):

self.file.write("// %s\n" % name)



  1. if __name__ == "__main__":
  1. printModuleInfo("Blender", "getCurrentScene")


v3d_export = AllegroExport("\\Devprojects\\speedhack\\test.c")

scene = Blender.getCurrentScene()

v3d_export.export("Plane.021")


</highlightSyntax>