--get rid of already opened instances
try (destroyDialog export_tool) catch()


rollout export_tool "Export Tool" width:232 height:267
(

	dropdownList 'ddl_units' "" pos:[105,26] width:120 height:21 items:#("cm", "m") align:#left
	label 'lbl_unit' "Export Unit" pos:[10,26] width:61 height:17 align:#left
	checkbox 'chk_exportMCM' "Export object both in cm and m" pos:[10,53] width:221 height:18 align:#left
	GroupBox 'grp_units' "Unit Setup" pos:[3,7] width:227 height:71 align:#left
	edittext 'edt_exportPath' "" pos:[105,156] width:120 height:20 align:#left
	label 'lbl_exportPath' "Export Path" pos:[10,156] width:90 height:20 align:#left
	label 'lbl_exportPathDesc' "The tool will create the folders 'cm' and 'm' in the specified location, depending on unit setup" pos:[10,108] width:217 height:44 align:#left
	button 'btn_export' "Export FBX" pos:[7,230] width:214 height:30 align:#left
	button 'btn_resetXForm' "Reset X Form" pos:[7,190] width:214 height:30 align:#left
	GroupBox 'grp_exportOpt' "Export Settings" pos:[3,88] width:227 height:94 align:#left
	
	--check if export path is valid
	fn isExportPathValid exportPath = (

		if edt_exportPath.text != "" then (
			doesExist = doesFileExist exportPath;
			
			if doesExist == false then (
				createFolder = querybox("Folder '" + exportPath + "' doesn't exist. Do you want to create it?") beep:true;
				
				if createFolder then (
					success = makeDir exportPath;
					if success then (
						return true;
					)
					else (
						messagebox("Folder could not be created");
						return false;
					)
				)
				else return false;
			)
			else return true;			
		)
		else messagebox("Please enter an Export Path");
		return false;
	)

	
	--EXPORT	
	
	on btn_export pressed do
	(
		selectedObj = getcurrentselection();
		-- is at least one object selected?
		if(selectedObj.count != 0) then (
			--export both m and cm?
			if chk_exportMCM.state == false then (				
				exportPath = edt_exportPath.text;
				exportPath = exportPath  + "/" + ( ddl_units.selected as string );
				FbxExporterSetParam "convertUnit" (ddl_units.selected as string );
				print(FbxExporterGetParam "convertUnit");
				--is export path valid
				if (isExportPathValid(exportPath)) then (
					for obj in selectedObj do (
						select obj
						originalpos = obj.pos;
						obj.pos = [0,0,0];
						
						exportPathObj = exportPath + "/" + (obj.name as string);
						print(exportPathObj);
						exportFile exportPathObj #noPrompt selectedOnly:true using:FBXEXP;
						
						obj.pos = originalpos;

					)				
				)
			)--export both m and cm?
			else (
				for i in ddl_units.items do (
					exportPath = edt_exportPath.text;
					exportPath = exportPath  + "/" + ( i as string );
					FbxExporterSetParam "convertUnit" ( i as string );	
					print(FbxExporterGetParam "convertUnit");
					--is export path valid
					if (isExportPathValid(exportPath)) then (
						for obj in selectedObj do (
							select obj					
							originalpos = obj.pos;
							obj.pos = [0,0,0];
							exportPathObj = exportPath + "/" + (obj.name as string);
							print(exportPathObj);
							exportFile exportPathObj #noPrompt selectedOnly:true using:FBXEXP;
							
							obj.pos = originalpos;

						)				
					)
				)
			)
	
		)--is at least one object selected?
		else messagebox("Nothing selected");		
	)
	
	--RESET XFORM
	on btn_resetXForm pressed do
	(
		selectedObj = getcurrentselection();
		
		if (selectedObj.count != 0) then (
			for obj in selectedObj do (
				resetxform obj;
				converttopoly obj;
				print( obj.name + " XForm reset " );
			)
		)
		else messagebox("Nothing selected"); 
	)
)

createdialog export_tool