#!BPY

"""
Name: 'OBJ Wavefront Object'
Blender: 232
Group: 'Export'
Tooltip: 'Save a Wavefront Object''
"""

# --------------------------------------------------------------------------
# OBJ Export v0.9 by Campbell Barton (AKA Ideasman)
# --------------------------------------------------------------------------
# ***** BEGIN GPL LICENSE BLOCK *****
#
# Copyright (C) 2004: Willian P. Germano, wgermano@ig.com.br
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
# --------------------------------------------------------------------------

#================================================#
# Gets the world matrix of an object             #
# by multiplying by parents mat's recursively    # 
# This only works in some simple situations,     #
# needs work....                                 #
#================================================#
def getWorldMat(ob):
	mat = ob.getMatrix()
	p = ob.getParent()
	if p != None:
		mat = mat + getWorldMat(p)
	return mat
	
#==================#
# Apply Transform  #
#==================#
def apply_transform(verts, matrix):
	
	x, y, z = verts
	xloc, yloc, zloc = matrix[3][0], matrix[3][1], matrix[3][2]
	xcomponent = x*matrix[0][0] + y*matrix[1][0] + z*matrix[2][0] + xloc
	ycomponent = x*matrix[0][1] + y*matrix[1][1] + z*matrix[2][1] + yloc
	zcomponent = x*matrix[0][2] + y*matrix[1][2] + z*matrix[2][2] + zloc
	
	return [xcomponent, ycomponent, zcomponent]


#=====================================#
# Apply Transform for vertex normals  #
# ignore the translation              #
#=====================================#
def apply_normal_transform(verts, matrix):
	x, y, z = verts
	xcomponent = x*matrix[0][0] + y*matrix[1][0] + z*matrix[2][0]
	ycomponent = x*matrix[0][1] + y*matrix[1][1] + z*matrix[2][1]
	zcomponent = x*matrix[0][2] + y*matrix[1][2] + z*matrix[2][2]
	return Mathutils.Vector([xcomponent, ycomponent, zcomponent])

#====================================================#
# Return a 6 deciaml point floating point value      #
# as a string that dosent have any python chars      #
#====================================================#  
def saneFloat(float):
  #return '%(float)b' % vars()  # 6 fp as house.hqx
  return str('%f' % float) + ' '


from Blender import *

def save_obj(filename):
	
	file = open(filename, "w")
	
	# Write Header
	file.write('# Blender OBJ File: ' + Get('filename') + ' \n')
	file.write('# www.blender.org\n')
	
	# Get all meshs
	for ob in Object.Get():
		if ob.getType() == 'Mesh':
			m = ob.getData()
			if len(m.verts) > 0: # Make sure there is somthing to write.
				
				# Set the default mat
				currentMatName = ''
				
				file.write('o ' + ob.getName() + '_' + m.name + '\n') # Write Object name
				
				# Dosent work properly,
				matrix = getWorldMat(ob)
				
				# Vert
				for v in m.verts:
					# Transform the vert 
					vTx = apply_transform(v.co, matrix)
					
					file.write('v ')
					file.write(saneFloat(vTx[0]))
					file.write(saneFloat(vTx[1]))
					file.write(saneFloat(vTx[2]) + '\n')
				
				# UV
				for f in m.faces:
					for uvIdx in range(len(f.v)):
						file.write('vt ')
						if f.uv:
							file.write(saneFloat(f.uv[uvIdx][0]))
							file.write(saneFloat(f.uv[uvIdx][1]))
						else:
							file.write('0.0 ')
							file.write('0.0 ')
							
						file.write('0.0' + '\n')
				
				# NORMAL
				for f in m.faces:
					for v in f.v:
						# Transform the normal
						noTx = apply_normal_transform(v.no, matrix)
						noTx.normalize()
						file.write('vn ')
						file.write(saneFloat(noTx[0]))
						file.write(saneFloat(noTx[1]))
						file.write(saneFloat(noTx[2]) + '\n')
										
				uvIdx = 0
				for f in m.faces:
					# Check material and change if needed.
					if currentMatName != m.materials[f.mat].getName():
						currentMatName = m.materials[f.mat].getName()
						file.write('usemtl ' + currentMatName + '\n')
					
					file.write('f ')
					for v in f.v:
						file.write( str(m.verts.index(v) +1) + '/') # Vert IDX
						file.write( str(uvIdx +1) + '/') # UV IDX
						file.write( str(uvIdx +1) + ' ') # NORMAL IDX
						uvIdx+=1
					file.write('\n')
					
	file.close()

Window.FileSelector(save_obj, 'SELECT OBJ FILE')
