#Takes a ms3d ascii vertex list and puts it into a table meant for use with ActorMultiVertex. -Kid
import sys
import os

def main():
	gotfile = 0
	if len(sys.argv) > 1:
		if sys.argv[1] == '-h':
			print("Translates ms3d ascii vertex list into SM5 AMV vertex table.")
			print("USAGE: {} <INFILE>".format(sys.argv[0]))
			sys.exit()
		else:
			gotfile = 1
			filein = sys.argv[1]

	if not gotfile:
		#Grab the filename from the user.
		filein = raw_input("input file: ")

	#Does the file exist? If not, repeat until we have something valid.
	while not os.path.isfile(filein):
		print("File {} does not exist. Try again.".format(filein))
		filein = raw_input("input file: ")

	#open something for output.
	outputf = raw_input("Output file (Overwrites given file, default: \"output-vert.lua\"): ")
	if not outputf:
		outputf = "output-vert.lua"
	fileout = open(outputf,'w')

	arrayname = raw_input("input vertex array name (default: \"vertex\"): ")
	if not arrayname:
		arrayname = "vertex"

	#We're sure we have something that exists. Begin the task.
	with open(filein) as fp:
		fileout.write("--Made with ms3dascii-vert2amvtable.py! -Kid\n")
		fileout.write("local {}=\n".format(arrayname))#Meant to be put into a function.
		fileout.write("{\n")
		fileout.write("    --{{x,y,z},{r,g,b,a},{texturecoordx,texturecoordy}},\n")
		for line in fp:
			argument = line.split()
			#print("Flag: {}, x: {}, y: {}, z: {}, textcoordx: {}, textcoordy: {}, bone index: {}".format(argument[0],argument[1],argument[2],argument[3],argument[4],argument[5],argument[6]))
			fileout.write("    {{")
			fileout.write("{},{},{}".format(argument[1],argument[2],argument[3])+"},{1,1,1,1},{")
			fileout.write("{},{}".format(argument[4],argument[5])+"}}"+",--bone index {}\n".format(argument[6]))
		fp.close()#Close the file. It's common courtesy.
		fileout.write("}")#Close off the output.

	print("Results output to \"{}\"".format(outputf))
	fileout.close()#One last close.

if __name__ == '__main__':
	main()
