#!BPY

"""
Name: 'Textport save'
Blender: 234
Group: 'Export'
Tooltip: 'Textport exporter for libgender'
"""

# Blender Text Export Module
# Version 0.0
# Colin Peart
# Released under GPL (insert license text here)

import Blender
from Blender import Types, Object, NMesh, Camera, Lamp, Mathutils
from Blender.Mathutils import *

import sys

# dump -- the object dumper f: File, o: object
def dump(f, o, od):
	if o.block_type == 'NMesh' :
		mesh = Blender.NMesh.GetRaw(o.name)
		f.write("M " + o.name + "\n")
                loc = od.getLocation ()
                f.write("L L\n" + `loc[0]` + " " + `loc[1]` + " " + `loc[2]` + "\n")
                f.write("T Material\n");

                if len (mesh.materials) > 0:
                   material = mesh.materials[0]
                   c0 = material.rgbCol[0]
                   c1 = material.rgbCol[1]
                   c2 = material.rgbCol[2]
                   f.write(`c0` + " " + `c1` + " " + `c2` + " " + `material.alpha` + "\n");
                   s0 = material.specCol[0]
                   s1 = material.specCol[1]
                   s2 = material.specCol[2]
                   f.write(`s0` + " " + `s1` + " " + `s2` + " " + `material.specTransp` + "\n");
                   s0 *= material.emit
                   s1 *= material.emit
                   s2 *= material.emit
                   f.write(`s0` + " " + `s1` + " " + `s2` + " 1\n");
                   f.write(`material.spec` + "\n");
                else:
                   f.write("1 1 1 1\n");
                   f.write("0 0 0 1\n");
                   f.write("0 0 0 1\n");
                   f.write("0\n");

		
		# Vertex Coordinate header
		f.write("V " + `len(mesh.verts)` + "\n")
		
		# Verticies
		for vert in mesh.verts:
			f.write(`vert.co[0]` + " " + `vert.co[1]` + " " + `vert.co[2]` + "\n")
		
		# Vertex Normal Header
		f.write("N " + `len(mesh.verts)` + "\n")
		# Vertex Normals
		for vert in mesh.verts:
			f.write(`vert.no[0]` + " " + `vert.no[1]` + " " + `vert.no[2]` + "\n")

		# Faces:  Take two passes:  First, count the total number of faces,
		# second: output them.  Tessalate quads to triangles. Discard edges (which
		# have only two vertices)
		# Modified to use only one pass, by building a variable with the text
		faces = len(mesh.faces)
		data = ""
		colordata = ""
		for face in mesh.faces:
			if len (face.v) < 3:
                                faces = faces - 1
                        elif len (face.v) == 3:
				# triangle
				data = data + `face.v[0].index` + " " + `face.v[1].index` + " " + `face.v[2].index` + "\n"
                                data = data + `face.smooth` + "\n"
				data = data + `face.uv[0][0]` + " " + `face.uv[0][1]` + "\n"
				data = data + `face.uv[1][0]` + " " + `face.uv[1][1]` + "\n"
				data = data + `face.uv[2][0]` + " " + `face.uv[2][1]` + "\n"
			else:
				# this one is a quad
				# Break it up into two triangles
				# Hence one additional face
				faces = faces + 1

				data = data + `face.v[0].index` + " " + `face.v[1].index` + " " + `face.v[3].index` + "\n"
                                data = data + `face.smooth` + "\n"
				data = data + `face.uv[0][0]` + " " + `face.uv[0][1]` + "\n"
				data = data + `face.uv[1][0]` + " " + `face.uv[1][1]` + "\n"
				data = data + `face.uv[3][0]` + " " + `face.uv[3][1]` + "\n"

				data = data + `face.v[1].index` + " " + `face.v[2].index` + " " + `face.v[3].index` + "\n"
                                data = data + `face.smooth` + "\n"
			        data = data + `face.uv[1][0]` + " " + `face.uv[1][1]` + "\n"
				data = data + `face.uv[2][0]` + " " + `face.uv[2][1]` + "\n"
				data = data + `face.uv[3][0]` + " " + `face.uv[3][1]` + "\n"
		# Now I can write the header with the correct face count, and then the data
		f.write("A " + `faces` + "\n")
		f.write(data)
		
################################################################################


objectsToExport = Blender.Object.GetSelected()

startFrame = Blender.Get('staframe')
endFrame = Blender.Get('endframe')
frameset = range(startFrame, endFrame + 1)


filename = Blender.Get('filename') + '.blasc'
print('Exporting ' + filename + ' frames:')
print(frameset)

fout = open(filename, 'w')

ob = objectsToExport[0]
objs = len (objectsToExport)

print('Exporting Object ' + ob.name)
fout.write("O " + `objs` + "\n")

for i in objectsToExport:
        dump (fout, i.data, i)

fout.close();

print('Done!')


