-- Author   "Drigien"
-- Version: "0.1 alpha"
-- About:   "This script exports the selected mesh to the Lithtech ASCII format"


fn Formatted  num  output =
(
	sz = num as string
	
	pos = findString sz "e"
	if pos != undefined then
	(
		szArray = filterString sz ". e"
		count = szArray[3] as integer
		print szArray
		print count
		
		if szArray[1] as integer < 0 then
		(
			pos = szArray[1] as integer
			szArray[1] = abs pos as string
			sz  = "-"
		)
		else
			sz = ""
		
		if count < 0 then
		(
			sz += "0."
			for i=1 to abs count do
				sz += "0"
			sz += szArray[1] + szArray[2]
		)
		else
		(
			sz += szArray[1] + szArray[2]
			sz += "0"
			for i=1 to abs count do
				sz += "0"
			sz += "."
		)
		print sz
	)
	
	sz += "000000"
	
	inum = num as integer
	sziNum = inum as string
	size = sziNum.count + 7
	
	szOut = substring sz 1 size
	format "% " szOut to:output	
)			

	tmesh = snapshotAsMesh selection[1]		
	if tmesh != undefined then 
	(

	out_name = getSaveFileName caption:"Export to Lta" initialDir:"$scripts" types:"LithTech Model(*.lta)|*.lta|All|*.*|"
	if out_name != undefined then
	(
		out_file = createfile out_name		
		if out_file != undefined then
		(	
			num_verts = tmesh.numverts 
			num_faces = tmesh.numfaces
			
			format "(lt-model-0 \n" to:out_file
			format "\t(on-load-cmds \n" to:out_file
			format "\t\t( \n" to:out_file
			format "\t\t\t(anim-bindings  \n" to:out_file
			format "\t\t\t\t( \n" to:out_file
			format "\t\t\t\t\t(anim-binding \n" to:out_file
			format "\t\t\t\t\t\t(name \"none\" ) \n" to:out_file
			format "\t\t\t\t\t\t(translation  \n" to:out_file
			format "\t\t\t\t\t\t\t(0.000000 0.000000 0.000000 ) \n" to:out_file
			format "\t\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t\t\t(interp-time 200 ) \n" to:out_file
			format "\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t) \n" to:out_file
			format "\t\t\t) \n" to:out_file
			format "\t\t\t(set-node-flags \n" to:out_file
			format "\t\t\t\t( \n" to:out_file
			format "\t\t\t\t\t(\"Scene Root\" 0 ) \n" to:out_file
			format "\t\t\t\t\t(\"Bone01\" 0 ) \n" to:out_file
			format "\t\t\t\t) \n" to:out_file
			format "\t\t\t) \n" to:out_file
			format "\t\t\t(set-command-string \"\" ) \n" to:out_file
			format "\t\t\t(lod-groups \n" to:out_file
			format "\t\t\t\t( \n" to:out_file
			format "\t\t\t\t\t(create-lod-group \"Object\" \n" to:out_file
			format "\t\t\t\t\t\t(lod-dists \n" to:out_file
			format "\t\t\t\t\t\t\t(0.000000 ) \n" to:out_file
			format "\t\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t\t\t(shapes \n" to:out_file
			format "\t\t\t\t\t\t\t(\"Object\" ) \n" to:out_file
			format "\t\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t) \n" to:out_file
			format "\t\t\t) \n" to:out_file
			format "\t\t\t(set-global-radius 96.000000 ) \n" to:out_file
			format "\t\t\t(add-node-obb-list  \n" to:out_file
			format "\t\t\t\t() \n" to:out_file
			format "\t\t\t) \n" to:out_file
			format "\t\t\t(add-deformer \n" to:out_file
			format "\t\t\t\t(skel-deformer \n" to:out_file
			format "\t\t\t\t\t(target \"Object\" ) \n" to:out_file
			format "\t\t\t\t\t(influences \n" to:out_file
			format "\t\t\t\t\t\t(\"Scene Root\" \"Bone01\" ) \n" to:out_file
			format "\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t\t(weightsets \n" to:out_file
			format "\t\t\t\t\t\t( \n" to:out_file
			    
			for v = 1 to num_verts do
			(
				format "\t\t\t\t\t\t\t(1 1.000000 )\n" to:out_file
			)
			
			format "\t\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t) \n" to:out_file
			format "\t\t\t) \n" to:out_file
			format "\t\t) \n" to:out_file
			format "\t) \n" to:out_file
			format "\t(hierarchy  \n" to:out_file
			format "\t\t(children  \n" to:out_file
			format "\t\t\t( \n" to:out_file
			format "\t\t\t\t(transform \"Scene Root\"  \n" to:out_file
			format "\t\t\t\t\t(matrix  \n" to:out_file
			format "\t\t\t\t\t\t( \n" to:out_file
			format "\t\t\t\t\t\t\t(-1.000000 0.000000 0.000000 0.000000 ) \n" to:out_file
			format "\t\t\t\t\t\t\t(0.000000 1.000000 0.000000 0.000000 ) \n" to:out_file
			format "\t\t\t\t\t\t\t(0.000000 0.000000 -1.000000 0.000000 ) \n" to:out_file
			format "\t\t\t\t\t\t\t(0.000000 0.000000 0.000000 1.000000 ) \n" to:out_file
			format "\t\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t\t(children  \n" to:out_file
			format "\t\t\t\t\t\t( \n" to:out_file
			format "\t\t\t\t\t\t\t(transform \"Bone01\"  \n" to:out_file
			format "\t\t\t\t\t\t\t\t(matrix \n" to:out_file
			format "\t\t\t\t\t\t\t\t\t( \n" to:out_file
			format "\t\t\t\t\t\t\t\t\t\t(1.000000 0.000000 0.000000 0.000000 ) \n" to:out_file
			format "\t\t\t\t\t\t\t\t\t\t(0.000000 1.000000 0.000000 0.000000 )  \n" to:out_file
			format "\t\t\t\t\t\t\t\t\t\t(0.000000 0.000000 1.000000 0.000000 ) \n" to:out_file
			format "\t\t\t\t\t\t\t\t\t\t(0.000000 0.000000 0.000000 1.000000 )  \n" to:out_file
			format "\t\t\t\t\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t) \n" to:out_file
			format "\t\t\t) \n" to:out_file
			format "\t\t) \n" to:out_file
			format "\t) \n" to:out_file
			format "\t(shape \"Object\" \n" to:out_file
			format "\t\t(geometry \n" to:out_file
			format "\t\t\t(mesh \"Object\" \n" to:out_file
			
			--VERTICES
			format "\t\t\t\t(vertex \n"  to:out_file
			format "\t\t\t\t\t( \n" to:out_file			    
			for v = 1 to num_verts do
			(
				vert = getVert tmesh v
				--print vert
				
				format "\t\t\t\t\t\t(" to:out_file
				Formatted vert.x out_file
				Formatted vert.z out_file
				Formatted vert.y out_file
				format ") \n" to:out_file 
			)			
			format "\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t) \n" to:out_file
			
			--NORMALS
			format "\t\t\t\t(normals \n"  to:out_file
			format "\t\t\t\t\t( \n" to:out_file			    
			for v = 1 to num_verts do
			(
				vert = getNormal tmesh v
				print vert
				format "\t\t\t\t\t\t(" to:out_file
				
				Formatted vert.x out_file
				Formatted vert.z out_file
				Formatted vert.y out_file
				
				format ") \n" to:out_file 
			)			
		 	format "\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t) \n" to:out_file
			
			--UV COORDS
			format "\t\t\t\t(uvs \n" to:out_file
			format "\t\t\t\t\t( \n" to:out_file			
			for f = 1 to num_faces do
			(
				face = getFace tmesh f
				print face
				
				uv = getTVert tmesh face.x
				format "\t\t\t\t\t\t(" to:out_file
				Formatted uv.x out_file
				Formatted uv.y out_file
				format ")\n" to:out_file
				
				uv = getTVert tmesh face.y
				format "\t\t\t\t\t\t(" to:out_file
				Formatted uv.x out_file
				Formatted uv.y out_file
				format ")\n" to:out_file
				
				uv = getTVert tmesh face.z
				format "\t\t\t\t\t\t(" to:out_file
				Formatted uv.x out_file
				Formatted uv.y out_file
				format ")\n" to:out_file
			)			
			format "\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t) \n" to:out_file
			
			--FACE COUNT
			format "\t\t\t\t(tex-fs \n" to:out_file
			format "\t\t\t\t\t(" to:out_file			
			for f = 0 to num_faces-1 do
			(
				face = f*3
				a = face
				b = face+1
				c = face+2
				format "% % % " a b c to:out_file
			)			
			format ") \n" to:out_file
			format "\t\t\t\t) \n" to:out_file
			
			--FACES
			format "\t\t\t\t(tri-fs \n" to:out_file
			format "\t\t\t\t\t(" to:out_file			
			for f = 1 to num_faces do
			(
				face = getFace tmesh f
				--print face
								
				a = face.x-1
				b = face.y-1
				c = face.z-1
				a = a as integer
				b = b as integer
				c = c as integer
				format "% % % " c b a to:out_file
			)
			
			format ") \n" to:out_file
			format "\t\t\t\t) \n" to:out_file
			format "\t\t\t) \n" to:out_file
			format "\t\t) \n" to:out_file
			format "\t) \n" to:out_file
			format "\t(animset \"base\"  \n" to:out_file
			format "\t\t(keyframe  \n" to:out_file
			format "\t\t\t(keyframe  \n" to:out_file
			format "\t\t\t\t(times  \n" to:out_file
			format "\t\t\t\t\t(0 ) \n" to:out_file
			format "\t\t\t\t) \n" to:out_file
			format "\t\t\t\t(values  \n" to:out_file
			format "\t\t\t\t\t(\"\" ) \n" to:out_file
			format "\t\t\t\t) \n" to:out_file
			format "\t\t\t) \n" to:out_file
			format "\t\t) \n" to:out_file
			format "\t\t(anims  \n" to:out_file
			format "\t\t\t( \n" to:out_file
			format "\t\t\t\t(anim  \n" to:out_file
			format "\t\t\t\t\t(parent \"Scene Root\" ) \n" to:out_file
			format "\t\t\t\t\t(frames  \n" to:out_file
			format "\t\t\t\t\t\t(posquat  \n" to:out_file
			format "\t\t\t\t\t\t\t( \n" to:out_file
			format "\t\t\t\t\t\t\t\t( \n" to:out_file
			format "\t\t\t\t\t\t\t\t\t(0.000000 0.000000 0.000000 ) \n" to:out_file
			format "\t\t\t\t\t\t\t\t\t(0.000000 1.000000 0.000000 0.000000 ) \n" to:out_file
			format "\t\t\t\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t\t\t) \n" to:out_file
		    format "\t\t\t\t\t) \n" to:out_file
		    format "\t\t\t\t) \n" to:out_file
			format "\t\t\t\t(anim  \n" to:out_file
			format "\t\t\t\t\t(parent \"Bone01\" ) \n" to:out_file
			format "\t\t\t\t\t(frames  \n" to:out_file
			format "\t\t\t\t\t\t(posquat  \n" to:out_file
			format "\t\t\t\t\t\t\t( \n" to:out_file
			format "\t\t\t\t\t\t\t\t( \n" to:out_file
			format "\t\t\t\t\t\t\t\t\t(0.000000 0.000000 0.000000 ) \n" to:out_file
			format "\t\t\t\t\t\t\t\t\t(0.000000 1.000000 0.000000 0.000000 ) \n" to:out_file
			format "\t\t\t\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t\t) \n" to:out_file
			format "\t\t\t\t) \n" to:out_file
			format "\t\t\t) \n" to:out_file
			format "\t\t) \n" to:out_file
			format "\t) \n" to:out_file
			format ") \n" to:out_file
			
			close out_file
		)
			edit out_name
	)
)
	
	