#!BPY
# """
# Name: 'OpenGL Exporter'
# Blender: 244
# Group: 'Export'
# Tooltip: 'Simple OpenGl C/C++ Code exporter.'
# """
__author__ = 'Rogerio Mazakina'
__version__ = '2.44 2007/08/03'
__url__ = ["Developers3Soft, http://developers3.webcindario.com"]
__email__ = ["Rogerio Mazakina", "gpr2k@hotmail.com", "opengl exporter"]
__bpydoc__ = """\
Usage:
Use Blender version 2.44 or higher
Other versions may not be compatible
Just press Alt+P to exec
Choose the name of the file
Done!
Support:
Only Meshes at the moment
Known issues:
Cannot export texture information, just raw mesh
"""
import Blender
from Blender import Mesh
print "####################################"
print " GL file Export Script"
def GLexport(filename):
filename = filename
file = open(filename,"w")
file.write("// File Exported by GLexport Script.\n")
# get the scene
scene = Blender.Scene.GetCurrent()
# get all meshes
meshList = [ob for ob in scene.objects if ob.type == 'Mesh']
# FOREACH MESH
for object in meshList:
modelName = object.getName()
mesh = object.getData(0,1)
print " Exporting %s now." %modelName
file.write("void %s()\n{\n" %modelName)
file.write("\t// Object name: %s\n" %modelName)
faceCount = 0
lastShadeType = -1
lastNumVertices = -1
# FOREACH FACE
for face in mesh.faces:
if lastShadeType == -1 or lastShadeType != face.smooth:
lastShadeType = face.smooth
# The face must be smooth if there are vertex colors!
# Otherwise all vertices will be single color
if face.smooth or mesh.vertexColors:
file.write("\tglShadeModel(GL_SMOOTH);\n")
else:
file.write("\tglShadeModel(GL_FLAT);\n")
numVertices = len(face.v)
if lastNumVertices == -1 or lastNumVertices != numVertices:
if lastNumVertices != -1:
file.write("\tglEnd();\n\n")
lastNumVertices = numVertices
if numVertices ==3:
file.write("\tglBegin(GL_TRIANGLES);\n")
else:
file.write("\tglBegin(GL_QUADS);\n")
file.write("\t\t// Face %d\n" % faceCount );
# FOREACH VERTICE
for vIndex in range( numVertices ):
vertex = face.verts[vIndex]
# VERTEX COLOR IF TRUE
if mesh.vertexColors:
file.write("\t\tglColor3f(%ff, %ff, %ff);\n" % (face.col[vIndex].r/255.0, face.col[vIndex].g/255.0, face.col[vIndex].b/255.0) )
# VERTEX NORMAL
file.write("\t\tglNormal3f(%ff, %ff, %ff);\n" % (vertex.no.x, vertex.no.y, vertex.no.z ));
# VERTEX UV COORDS
# Note: I really dont know if this works...
#if mesh.faceUV: # if the mesh have texture (only UV coords)
# file.write("\t\tglTexCoord2f(%ff,%ff);" % (face.uv[vIndex].u, face.uv[vIndex].v))
# VERTEX POSITION
file.write("\t\tglVertex3f(%ff, %ff, %ff);\n" % (vertex.co.x, vertex.co.y, vertex.co.z ))
# END FOR
faceCount = faceCount + 1
# END FOREACH FACE
file.write("\tglEnd();\n\n")
file.write("}\n")
# END FOREACH MESH
file.close()
print " Export done!"
print "------- END GL EXPORT.PY ----------"
Blender.Window.FileSelector(GLexport, "Export")