	
--- script to export max models to MDL's 
--- this generates a .MCR file for use in the interface

Macroscript Max2MDL\
	category:"Tools"\
	tooltip:"Export current Max file to MDL" (	
	
	ExpFlag = false
	debug_out = false -- set this to true to debug
	EscapeEnable = false
	showQC = keyboard.shiftpressed -- hold down shift to see generated QC
	global VRMoutput, SMDoutput
	
	--- save QC file with same name as this max file
	
	ErrorMsg = while ExpFlag == false do
		(
		if debug_out == true do (DebugWindow= newscript())
		
		ExportTo = getSaveFileName\
			caption:"Save QC File As..."\
			filename:(MaxFilePath + (substring MaxFileName 1 (MaxfileName.count - 4) )+ ".qc")\
			types:"QC file|*.qc|"
		
		if ExportTo == undefined do (exit with "Must choose a save name and location!")
		
		--- path variables to direct subsequent ops	
		ExpPath = GetFileNamePath ExportTo
		ExpRoot = GetFileNameFile ExportTo
		
		--- compare would-be values to the current Dev Tree
		MemData = openfile (scriptspath + "\\ValveConfig.dat")
			if Memdata == undefined then 
				(
				exit with "tree"
				)
			else -- read saved values
				(
				dummy = readline MemData
				DevTreePath = readline MemData
				ModelSourcePath = readline MemData
				Qroot  = getfilenamepath (readline Memdata)
				QStudioMDL = Readline MemData
				QHLMV = readline MemData
				QMdls = readline Memdata
		  		close Memdata
				) 
		
		--- is the current model in the Dev Tree?		
		
		DevTreePathArray = filterstring DevTreePath "\\"
		ExpPathArray = filterstring ExpPath "\\"
		MismatchFlag = false
		for I = 1 to  DevTreePathArray.count do 
			(
			if DevTreePathArray[I] != ExpPathArray[I] do (MisMatchFlag = true; exit)
			)
		If MismatchFlag == true do (exit with "tree")
	
		
		if debug_out == true do format " Check for too many meshes; select the mesh to export\n" to:DebugWindow
		SceneMeshes = #()
		for item in objects do
				(if classof item == Editable_mesh do append SceneMeshes item)
		if SceneMeshes.count != 1 do (exit with "Can only export one mesh at a time")
		ExMesh =  SceneMeshes[1]
			
		if debug_out == true do format " check for multisub texturel; save texure location(s) into UniqueTexList for later use\n" to:DebugWindow
		if ExMesh.material == undefined do (exit with "Mesh has no material applied")
		if classof ExMesh.material != multimaterial do (exit with "Mesh needs a multi-sub material")
		
		TexList = for item in Exmesh.material.materiallist where \
			classof item.diffusemap == BitmapTexture \
			collect (getfilenamepath Item.diffusemap.BitMap.filename)		
		UniqueTexList = #()
		
		for counter = Texlist.count to 1 by -1 do
			(
			temp = TexList[counter]
			deleteitem TexList counter
			if finditem Texlist temp == 0 do (append UniqueTexList temp)
			)

		if debug_out == true do format" get (or create) MRM values from mesh's custom properties\n" to:DebugWindow
		mrmMax = GetUserProp ExMesh "mrmMax"
		mrmMin = GetUserProp Exmesh "mrmMin"
		mrmFlip = GetUserProp Exmesh "mrmFlip"
		if mrmMax == undefined do (mrmMax = Exmesh.numfaces; SetUserProp Exmesh "mrmMax" mrmMax)
		if mrmMin == undefined do (mrmMin = ( Exmesh.numfaces /  4); SetUserProp Exmesh "mrmMin" mrmMin)
		if mrmFlip == undefined do (mrmFlip = ""; SetUserProp Exmesh "mrmFlip" false)

		if debug_out == true do format " Export VRMfile (using dxffile as a dummy)\n" to:DebugWindow
		Select ExMesh
		VRMoutput = (ExpPath + ExpRoot +".vrm")
		ExportFile VRMoutput #noprompt
		filecheck = getfiles VRMoutput
		if filecheck.count == 0 do (exit with "VRM export failed")
		
		if debug_out == true do format " Export idleSMD\n" to:DebugWindow
		SMDOutput = (ExpPath + ExpRoot + ".smd") 
		ExportFile SMDOutput #noprompt
		filecheck = getfiles smdoutput
		if filecheck.count == 0  do ( exit with "SMD export failed")
	
		if debug_out == true do format " Assemble QC file for export\n" to:DebugWindow
		QC_OUT = createfile Exportto
		if QC_Out == undefined do (exit with "Could not create .qc file")
		
		if debug_out == true do format " Header Information\n" to:DebugWindow
		format "// .qc file version 1.0\n" to:QC_OUT
		format "// generated by auto QC exporter v0.9\n" to:QC_OUT
		format"// file generated %" localtime to:QC_OUT
		format "\n\n"   to:QC_out
		
		-- relative model path
		RModelPath = substring Qmdls (DevTreePath.count + 2) 999
		if debug_out == true do format "->modelname\n" to:DebugWindow
		format "$modelname %\n\n" (RmodelPath + "\\" +ExpRoot + ".mdl")   to:QC_OUT --- modelname
		
		if debug_out == true do format "-> relative path to source\n" to:DebugWindow		
		format "$cd " to:QC_out
		for i = (DevTreePathArray.count + 1 )to ExpPathArray.count do 
			(
			format "%" ExpPathArray[i] to:QC_OUT
			if I < ExpPathArray.count do (format "\\" to:QC_OUT )
			
			)
		format "\n\n" to:QC_out	
		
		if debug_out == true do format"-> relative path to texture source"	to:DebugWindow	
		format "$cdtexture "  to:QC_out
		TexPathArray = filterstring UniqueTexList[1] "\\"
		for J = (DevTreePathArray.count + 1) to TexPathArray.count do 
			(
			format "%" TexPathArray[J] to:QC_OUT
			if J < TexPathArray.count do (format "\\" to:QC_OUT)
		
			)
		format "\n\n" to:QC_out	

		
		if debug_out == true do format "->VRM File\n" to:DebugWindow
		format "$body \"body\" " to:QC_OUT
		format "\"%\" % %" (ExpRoot) mrmMax Mrmmin to:QC_OUT
		if mrmflip == true do (format "reverse" to:QC_OUT)
		format "\n\n" to:QC_OUT
		
		if debug_out == true do format "Sequence\n" to:DebugWindow
		format "$sequence \"IDLE\" \"%\" fps 5 loop \n\n" ExpRoot to:QC_OUT
		
		close QC_OUT
		if ShowQC == true do (edit  Exportto)
		
		if debug_out == true do format "call Studiomdl\n" to:DebugWindow
		DosCommand   (GetFileNamePath QStudiomdl + "compile_QC.bat  " +  Exportto + "> C:\\test.txt")
		
		if debug_out == true do format "Call HLMV\n" to:DebugWindow
		if (getfiles (QMdls + "\\" + ExpRoot + ".mdl")).count > 0 then 
				(
				DosCommand (QHLMV + " " + QMdls + "\\" + ExpRoot + ".mdl")
				)
			else
				( if debug_out == true do  format "No MDL found in target dir\n" to:DebugWindow )
		Expflag = true -- end condition
		)	
		
		if debug_out == true do format "complete\n" to:DebugWindow
	
	-- Error Reporting	
	if errormsg != true then
		 	(
			if Errormsg == "tree" then
				(
				Messagebox "You have no ValveConfig.dat file or this \n model is not in your currently active tree. \n Please set your tree paths and try export again."
				 escapeEnable = true
				filein (Scriptspath + "\\Valveconfig1.ms")
				)
			else
				(
				if querybox(Errormsg +"\nDelete VRM and/or .SMD files from failed export?")!= "no" do
					(
					if VRMOutput != undefined do (if debug_out == true do print "deleting VRM file"; deletefile VRMOutput )
					if SMDOutput != undefined do (if debug_out == true do print "deleting SMD file"; deletefile SMDOutput )
					)
				)
			) 
		 else 
		 	(
			 
			)
	--- closing cleanup
	EscapeEnable = true
	
	) -- end Macro