///////////////////////////////////////////////////////////////////////////////////
// Panda3D Exporter Tool														 //
// Carnegie Mellon University ETC (Entertainment Technology Center) Panda3D Team //
// Author: Shao Zhang															 //
// 04/14/2005																	 //
//																				 //
// This tool will allow an artist to export and view assets directly from		 //
// maya.																		 //
//																				 //
// To run this mel script, drag and drop it (the .mel file) into your current	 //
// maya workspace.																 //
///////////////////////////////////////////////////////////////////////////////////

global proc pandaExporterUI()
{
	if ( `window -exists PandaExporter` )
	deleteUI -window PandaExporter;

	// Window is locked for image dimensions: 280×575
	// (add a few pixels to compensate for title bar and borders)
	window
		-title "Panda Exporter"
		-width 280
		-height 575
		-sizeable 0
		PandaExporter;

	// Use a formLayout so you control the overlap of the controls
	formLayout
			-numberOfDivisions 575
			-width 275
			-height 550
			mainForm;

		// First add controls
		button
			-label "Export"
			-command "exportButton"
			exportButton;

		button
			-label "Browse"
			-enable 0
			-command ( "browseForFolder \"texPath\"" )
			browseTexPathButton;

		button
			-label "Browse"
			-enable 0
			-command ( "browseForFolder \"outputPath\"" )
			browseOutputPathButton;

		button
			-label "Select a file to Pview"
			-command "getFile2Pview"
			pviewButton;

		button
			-label "Select a file to EGG"
			-command "getFile2Egg"
			eggButton;

		button
			-label "Select a file to BAM"
			-command "getFile2Bam"
			bamButton;

		button
			-label "www.panda3d.org"
			-command "openBrowser"
			webButton;

		checkBox
			-label "Double sided faces"
			-value 0
			bfaceCB;

		checkBox
			-label "Overwrite if file exists"
			-value 1
			overwriteCB;

		checkBox
			-label "Run Pview after export"
			-value 0
			pviewCB;

		radioCollection exportOptionsRC;
			radioButton
				-label "Mesh"
				-select
				-onCommand ( "exportOptionsUI" )
				chooseMeshRB;
			radioButton
				-label "Animation"
				-onCommand ( "exportOptionsUI" )
				chooseAnimationRB;
			radioButton
				-label "Both"
				-onCommand ( "exportOptionsUI" )
				chooseBothRB;
			radioButton
				-label "Pose"
				-onCommand ( "exportOptionsUI" )
				choosePoseRB;
		setParent ..;  //drop out of exportOptionsRC

		radioCollection selectionOptionsRC;
			radioButton
				-label "Export entire scene"
				-select
				//-onCommand ( "" )
				chooseSceneRB;
			radioButton
				-label "Export selected objects"
				//-onCommand ( "" )
				chooseObjectsRB;
		setParent ..;  //drop out of selectionOptionsRC

		radioCollection animationOptionsRC;
			radioButton
				-label "Full Range"
				-enable 0
				-onCommand ( "animationOptionsUI" )
				chooseFullRangeRB;
			radioButton
				-label "Custom Range"
				-enable 0
				-onCommand ( "animationOptionsUI" )
				chooseCustomRangeRB;
		setParent ..;  //drop out of animationOptionsRC

		radioCollection outputFileOptionsRC;
			radioButton
				-label "EGG (ASCII)"
				-select
				chooseEggRB;
			radioButton
				-label "BAM (binary)"
				-enable 0
				chooseBamRB;
			radioButton
				-label "Both"
				chooseEggBamRB;
		setParent ..;  //drop out of outputFileOptionsRC

		radioCollection texPathOptionsRC;
			radioButton
				-label "Reference textures relative to maya file (default)"
				-select
				-onCommand ( "texPathOptionsUI" )
				chooseDefaultTexPathRB;
			radioButton
				-label "Reference textures relative to custom path"
				-onCommand ( "texPathOptionsUI" )
				chooseCustomTexPathRB;
		setParent ..;  //drop out of texPathOptionsRC

		radioCollection outputPathOptionsRC;
			radioButton
				-label "Export to root directory of source file (default)"
				-select
				-onCommand ( "outputPathOptionsUI" )
				chooseDefaultOutputPathRB;
			radioButton
				-label "Export to other directory:"
				-onCommand ( "outputPathOptionsUI" )
				chooseCustomOutputPathRB;
		setParent ..;  //drop out of outputPathOptionsRC

		radioCollection outputFilenameOptionsRC;
			radioButton
				-label "Use original filename"
				-select
				-onCommand ( "outputFilenameOptionsUI" )
				chooseOriginalFilenameRB;
			radioButton
				-label "Use alternate filename"
				-onCommand ( "outputFilenameOptionsUI" )
				chooseCustomFilenameRB;
		setParent ..;  //drop out of outputFilenameOptionsRC

		text
			-label "Maya Panda Exporter v.1.2  CMU/ETC Panda3D Team"
			-font "plainLabelFont"
			titleText;

		text
			-label "Selection Options:"
			-font "plainLabelFont"
			selectionOptionsText;

		text
			-label "Output File Options:"
			-font "plainLabelFont"
			outputFileOptionsText;

		text
			-label "Texture Path Options:"
			-font "plainLabelFont"
			texPathOptionsText;

		text
			-label "Specify alternate directory:"
			-font "plainLabelFont"
			customTexPathText;

		text
			-label "Output directory Options:"
			-font "plainLabelFont"
			outputPathOptionsText;

		text
			-label "Specify alternate directory:"
			-font "plainLabelFont"
			customOutputPathText;

		text
			-label "Output filename Options:"
			-font "plainLabelFont"
			outputFilenameOptionsText;

		textField
			-editable 1
			-enable 0
			customTexPathTF;

		textField
			-editable 1
			-enable 0
			customOutputPathTF;

		textField
			-editable 1
			-enable 0
			customFilenameTF;

		frameLayout
				-label "Export Options:"
				-font "plainLabelFont"
				-labelIndent 2
				-collapsable 0
				-collapse 0
				-borderStyle "etchedOut"
				leftFrame;
		setParent ..;  //drop out of leftFrame

		frameLayout
				-label "Animation Options:"
				-font "plainLabelFont"
				-labelIndent 2
				-collapsable 0
				-collapse 0
				-borderStyle "etchedOut"
				rightFrame;

			columnLayout
				-columnAttach "left" 0
				-rowSpacing 0
				-columnWidth 60
				leftFrameColumn;

				text -label "\n\n\n";
				text -label "Start Frame:";
				textField
					-editable 1
					-enable 0
					-width 108
					startFrameTF;

				text -label "End Frame:";
				textField
					-editable 1
					-enable 0
					-width 108
					endFrameTF;

			setParent ..;  //drop out of leftFrameColumn
		setParent ..;  //drop out of rightFrame

		frameLayout
				-label "Extra Tools"
				-font "plainLabelFont"
				-labelIndent 38
				-collapsable 0
				-collapse 0
				-borderStyle "etchedOut"
				extraToolsFrame;
		setParent ..;  //drop out of extraToolsFrame

	setParent ..;  //drop out of "mainForm"

	// Now edit the layout of the controls in "mainForm
	formLayout -edit


		/* text */
		-ap 	titleText				"top"		0	2
		-ap 	titleText				"left"		0	5
		-ap 	titleText				"right" 	0	575

		-ap 	selectionOptionsText	"top"		0	75
		-ap 	selectionOptionsText	"left"		0	10
		-ap 	selectionOptionsText	"right" 	0	210

		-ap 	outputFileOptionsText	"top"		0	188
		-ap 	outputFileOptionsText	"left"		0	10
		-ap 	outputFileOptionsText	"right" 	0	210

		-ap 	texPathOptionsText		"top"		0	226
		-ap 	texPathOptionsText		"left"		0	10
		-ap 	texPathOptionsText		"right" 	0	210

		-ap 	customTexPathText		"top"		0	280
		-ap 	customTexPathText		"left"		0	10
		-ap 	customTexPathText		"right" 	0	300

		-ap 	outputPathOptionsText	"top"		0	324
		-ap 	outputPathOptionsText	"left"		0	10
		-ap 	outputPathOptionsText	"right" 	0	300

		-ap 	customOutputPathText	"top"		0	378
		-ap 	customOutputPathText	"left"		0	10
		-ap 	customOutputPathText	"right" 	0	300

		-ap 	outputFilenameOptionsText	"top"		0	422
		-ap 	outputFilenameOptionsText	"left"		0	10
		-ap 	outputFilenameOptionsText	"right" 	0	290



		/* textfields */
		-ap 	customTexPathTF 		"top"		0	298
		-ap 	customTexPathTF 		"left"		0	10
		-ap 	customTexPathTF 		"right" 	0	480

		-ap 	customOutputPathTF		"top"		0	396
		-ap 	customOutputPathTF		"left"		0	10
		-ap 	customOutputPathTF		"right" 	0	480

		-ap 	customFilenameTF		"top"		0	480
		-ap 	customFilenameTF		"left"		0	10
		-ap 	customFilenameTF		"right" 	0	284



		/* frames */
		-ap 	leftFrame				"top"		0	20
		-ap 	leftFrame				"bottom"	0	186
		-ap 	leftFrame				"left"		0	2
		-ap 	leftFrame				"right" 	0	330

		-ap 	rightFrame				"top"		0	20
		-ap 	rightFrame				"bottom"	0	186
		-ap 	rightFrame				"left"		0	334
		-ap 	rightFrame				"right" 	0	574

		-ap 	extraToolsFrame 		"top"		0	425
		-ap 	extraToolsFrame 		"bottom"	0	570
		-ap 	extraToolsFrame 		"left"		0	290
		-ap 	extraToolsFrame 		"right" 	0	574



		/* checkboxes */
		-ap 	bfaceCB 				"top"		0	128
		-ap 	bfaceCB 				"left"		0	12

		-ap 	overwriteCB 			"top"		0	145
		-ap 	overwriteCB 			"left"		0	12

		-ap 	pviewCB 				"top"		0	162
		-ap 	pviewCB 				"left"		0	12



		/* exportOptionsRC */
		-ap 	chooseMeshRB			"top"		0	40
		-ap 	chooseMeshRB			"left"		0	20

		-ap 	chooseBothRB			"top"		0	59
		-ap 	chooseBothRB			"left"		0	20

		-ap 	chooseAnimationRB		"top"		0	40
		-ap 	chooseAnimationRB		"left"		0	150

		-ap 	choosePoseRB			"top"		0	59
		-ap 	choosePoseRB			"left"		0	150



		/* selectionOptionsRC */
		-ap 	chooseSceneRB			"top"		0	91
		-ap 	chooseSceneRB			"left"		0	20

		-ap 	chooseObjectsRB 		"top"		0	108
		-ap 	chooseObjectsRB 		"left"		0	20



		/* animationOptionsRC */
		-ap 	chooseFullRangeRB		"top"		0	40
		-ap 	chooseFullRangeRB		"left"		0	346

		-ap 	chooseCustomRangeRB 	"top"		0	59
		-ap 	chooseCustomRangeRB 	"left"		0	346



		/* outputFileOptionsRC */
		-ap 	chooseEggRB 			"top"		0	205
		-ap 	chooseEggRB 			"left"		0	10

		-ap 	chooseBamRB 			"top"		0	205
		-ap 	chooseBamRB 			"left"		0	196

		-ap 	chooseEggBamRB			"top"		0	205
		-ap 	chooseEggBamRB			"left"		0	380



		/* texPathOptionsRC */
		-ap 	chooseDefaultTexPathRB	"top"		0	244
		-ap 	chooseDefaultTexPathRB	"left"		0	10

		-ap 	chooseCustomTexPathRB	"top"		0	264
		-ap 	chooseCustomTexPathRB	"left"		0	10



		/* outputPathOptionsRC */
		-ap 	chooseDefaultOutputPathRB	"top"		0	342
		-ap 	chooseDefaultOutputPathRB	"left"		0	10

		-ap 	chooseCustomOutputPathRB	"top"		0	362
		-ap 	chooseCustomOutputPathRB	"left"		0	10



		/* outputFilenameOptionsRC */
		-ap 	chooseOriginalFilenameRB	"top"		0	440
		-ap 	chooseOriginalFilenameRB	"left"		0	10

		-ap 	chooseCustomFilenameRB		"top"		0	460
		-ap 	chooseCustomFilenameRB		"left"		0	10



		/* browseTexPathButton */
		-ap 	browseTexPathButton 	"top"		0	298
		-ap 	browseTexPathButton 	"left"		0	485
		-ap 	browseTexPathButton 	"right" 	0	575



		/* browseOutputPathButton */
		-ap 	browseOutputPathButton	"top"		0	396
		-ap 	browseOutputPathButton	"left"		0	485
		-ap 	browseOutputPathButton	"right" 	0	575



		/* extra buttons */
		-ap 	pviewButton 			"top"		0	445
		-ap 	pviewButton 			"left"		0	305
		-ap 	pviewButton 			"right" 	0	560

		-ap 	eggButton				"top"		0	475
		-ap 	eggButton				"left"		0	305
		-ap 	eggButton				"right" 	0	560

		-ap 	bamButton				"top"		0	505
		-ap 	bamButton				"left"		0	305
		-ap 	bamButton				"right" 	0	560

		-ap 	webButton				"top"		0	535
		-ap 	webButton				"left"		0	305
		-ap 	webButton				"right" 	0	560



		/* exportButton */
		-ap 	exportButton			"top"		0	510
		-ap 	exportButton			"bottom"	0	570
		-ap 	exportButton			"left"		0	8
		-ap 	exportButton			"right" 	0	284

	mainForm; //End of layout

	showWindow PandaExporter;
}


//Automatically start the tool
pandaExporterUI();

///////////////////////////////////////////////////////////
// Process: openBrowser 								 //
// Opens the default browser at the panda3d url 		 //
///////////////////////////////////////////////////////////
global proc openBrowser()
{
	print "\nGoing to www.panda3d.org...\n";

	launch -webPage "www.panda3d.org";
}

///////////////////////////////////////////////////////////
// Process: exportOptionsUI 							 //
// Updates the UI when a radio button is chosen 		 //
///////////////////////////////////////////////////////////
global proc exportOptionsUI()
{
	string $selectedRB = `radioCollection -query -select exportOptionsRC`;

	switch ($selectedRB)
	{
		case "chooseMeshRB":
			print("Export Mesh Chosen\n");
			textField -edit -enable 0 startFrameTF;
			textField -edit -enable 0 endFrameTF;
			radioButton -edit -enable 0 chooseFullRangeRB;
			radioButton -edit -enable 0 chooseCustomRangeRB;
			break;
		case "chooseAnimationRB":
			print("Export Animation Chosen\n");
			radioButton -edit -enable 1 -select chooseFullRangeRB;
			radioButton -edit -enable 1 chooseCustomRangeRB;
			animationOptionsUI();
			break;
		case "chooseBothRB":
			print("Export Meshes and Animation Chosen\n");
			radioButton -edit -enable 1 -select chooseFullRangeRB;
			radioButton -edit -enable 1 chooseCustomRangeRB;
			animationOptionsUI();
			break;
		case "choosePoseRB":
			print("Export Pose Chosen\n");
			textField -edit -enable 1 startFrameTF;
			textField -edit -enable 0 endFrameTF;
			radioButton -edit -enable 0 chooseFullRangeRB;
			radioButton -edit -enable 1 -select chooseCustomRangeRB;
			break;
	}
}

///////////////////////////////////////////////////////////
// Process: animationOptionsUI							 //
// Updates the UI when a radio button is chosen 		 //
///////////////////////////////////////////////////////////
global proc animationOptionsUI()
{
	string $selectedRB = `radioCollection -query -select animationOptionsRC`;

	switch ($selectedRB)
	{
		case "chooseFullRangeRB":
			print("Animation Full Range Chosen\n");
			textField -edit -enable 0 startFrameTF;
			textField -edit -enable 0 endFrameTF;
			break;
		case "chooseCustomRangeRB":
			print("Animation Custom Range Chosen\n");
			textField -edit -enable 1 startFrameTF;
			textField -edit -enable 1 endFrameTF;
			break;
	}
}

///////////////////////////////////////////////////////////
// Process: texPathOptionsUI							 //
// Updates the UI when a radio button is chosen 		 //
///////////////////////////////////////////////////////////
global proc texPathOptionsUI()
{
	string $selectedRB = `radioCollection -query -select texPathOptionsRC`;

	switch ($selectedRB)
	{
		case "chooseDefaultTexPathRB":
			print("Default Texture Path Chosen\n");
			textField -edit -enable 0 customTexPathTF;
			button -edit -enable 0 browseTexPathButton;
			break;
		case "chooseCustomTexPathRB":
			print("Custom Texture Path Chosen\n");
			textField -edit -enable 1 customTexPathTF;
			button -edit -enable 1 browseTexPathButton;
			break;
	}
}

///////////////////////////////////////////////////////////
// Process: outputPathOptionsUI 						 //
// Updates the UI when a radio button is chosen 		 //
///////////////////////////////////////////////////////////
global proc outputPathOptionsUI()
{
	string $selectedRB = `radioCollection -query -select outputPathOptionsRC`;

	switch ($selectedRB)
	{
		case "chooseDefaultOutputPathRB":
			print("Default File Path Chosen\n");
			textField -edit -enable 0 customOutputPathTF;
			button -edit -enable 0 browseOutputPathButton;
			break;
		case "chooseCustomOutputPathRB":
			print("Custom File Path Chosen\n");
			textField -edit -enable 1 customOutputPathTF;
			button -edit -enable 1 browseOutputPathButton;
			break;
	}
}

///////////////////////////////////////////////////////////
// Process: outputFilenameOptionsUI 					 //
// Updates the UI when a radio button is chosen 		 //
///////////////////////////////////////////////////////////
global proc outputFilenameOptionsUI()
{
	string $selectedRB = `radioCollection -query -select outputFilenameOptionsRC`;

	switch ($selectedRB)
	{
		case "chooseOriginalFilenameRB":
			print("Default Filename Chosen\n");
			textField -edit -enable 0 customFilenameTF;
			break;
		case "chooseCustomFilenameRB":
			print("Custom Filename Chosen\n");
			textField -edit -enable 1 customFilenameTF;
			break;
	}
}

///////////////////////////////////////////////////////////////
// Process: browseForFolder 								 //
// Browses for a directory and returns the path as a string  //
///////////////////////////////////////////////////////////////
global proc browseForFolder(string $destination)
{
  fileBrowserDialog
	-mode 4
	-fileCommand ( "browseForFolderCallback \"" + $destination + "\"" )
	-actionName "Pick a Folder";
}

global proc browseForFolderCallback(string $destination, string $result, string $type )
{
  // Do whatever you need to with the $result
  print ( "Folder selection: " + $result + "\n" );

  if ( $destination == "texPath")
  {
	textField -edit -text $result customTexPathTF;
  }
  else
  {
	textField -edit -text $result customOutputPathTF;
  }
}

///////////////////////////////////////////////////////////
// Process: filePart									 //
// Extracts the path portion of an absolute filepath.	 //
// Input: e.g. "D:/projects/default/scenes/myScene.mb"	 //
// Result: e.g. "myScene.mb"							 //
// Filepath can be delimited with						 //
// either slash ("/" or "\")							 //
///////////////////////////////////////////////////////////
global proc string filePart( string $path )
{
  string $filePart = match( "[^/\\]*$", $path );

  return $filePart;
}

////////////////////////////////////////////////////////////
// Process: pathPart									  //
// Extracts the path portion of an absolute filepath.	  //
// Input: e.g. "D:/projects/default/scenes/myScene.mb"	  //
// Result: e.g. "D:/projects/default/scenes"			  //
// Note: Requires that the filepath be delimited with	  //
//		 _forward_ slashes ("/")						  //
////////////////////////////////////////////////////////////
global proc string pathPart( string $path )
{
  string $dir = match( "^.*/", $path );

  // Strip off trailing '/'
  //   int $sz = size( $dir );
  //   if ( ( $sz > 1 ) && ( substring( $dir, $sz, $sz ) == "/" ) )
  //   {
  //	$dir = substring( $dir, 1, ($sz - 1) );
  //   }
  return $dir;
}

///////////////////////////////////////////////////////////////
// Process: exportScene 									 //
// exports the entire scene/selected objects				 //
///////////////////////////////////////////////////////////////
global proc string exportScene( string $selection )
{
	print "Exporting scene...\n";

	string $sceneFilePath = `file -q -sceneName`;  //get scene filename and path

	if ( $sceneFilePath == "" )
	{
		error "You have not yet saved this scene.\nPlease save your scene or specify a custom output directory and filename.\n";
		return "failed";
	}

	string $scenePath = pathPart($sceneFilePath);
	string $sceneFile = filePart($sceneFilePath);
	string $fileName = `match "^[^\.]*" $sceneFile`;  //cut off the file extension
	string $tempScenePath = ($scenePath + $fileName + "_temp.mb");

	if ( $selection == "all")
	{
		file -op "v=1" -typ "mayaBinary" -ea $tempScenePath;  //export the whole scene
		print ("Saved entire scene as temporary file: " + $fileName + "_temp.mb\n");
	}
	else
	{
		file -op "v=1" -typ "mayaBinary" -es $tempScenePath;  //export only selected objects
		print ("Saved selected objects as temporary file: " + $fileName + "_temp.mb\n");
	}

	return $tempScenePath;
}

///////////////////////////////////////////////////////////////
// Process: argsBuilder 									 //
// constructs the arguments to pass to maya2egg 			 //
///////////////////////////////////////////////////////////////
global proc string argsBuilder()
{
	string $args = "maya2egg";

	//get the version of maya and append it to the maya2egg call
	string $mayaVersionLong = `getApplicationVersionAsFloat`;
	string $mayaVersionShort = substituteAllString($mayaVersionLong, ".", "");
	string $mayaVersionShort = substituteAllString($mayaVersionShort, "0", "");

	$args += $mayaVersionShort;

	$args += " ";

	//check back face culling
	if ( `checkBox -query -value bfaceCB` )
	{
		$args += "-bface ";
	}

	//check animation options
	string $exportOptionsARGS = `radioCollection -query -select exportOptionsRC`;

	switch ($exportOptionsARGS)
	{
		case "chooseMeshRB":
			$args += "-a model ";
			break;
		case "chooseAnimationRB":
			$args += "-a chan ";

			//set the start frames **Does not check if start frame < end frame **Does not support negative values  **Does not check if start/end frame is within bounds of the scene
			if ( `radioCollection -query -select animationOptionsRC` == "chooseCustomRangeRB" )
			{
				string $startFrameARGS = `textField -query -text startFrameTF`;
				string $endFrameARGS = `textField -query -text endFrameTF`;

				//start frame
				if ( `match "[0-9]+" $startFrameARGS` == $startFrameARGS && $startFrameARGS != "")
				{
					$args += ( "-sf " + `match "[0-9]+" $startFrameARGS` + " " );
				}
				else
				{
					error "Start Frame entered data is the wrong format.  Should be an integer.\n";
					return "failed";
				}

				//end frame
				if ( `match "[0-9]+" $endFrameARGS` == $endFrameARGS && $endFrameARGS != "")
				{
					$args += ( "-ef " + `match "[0-9]+" $endFrameARGS` + " " );
				}
				else
				{
					error "End Frame entered data is the wrong format.	Should be an integer.\n";
					return "failed";
				}
			}
			break;
		case "chooseBothRB":
			$args += "-a both ";

			//set the start frames **Does not check if start frame < end frame **Does not support negative values  **Does not check if start/end frame is within bounds of the scene
			if ( `radioCollection -query -select animationOptionsRC` == "chooseCustomRangeRB" )
			{
				string $startFrameARGS = `textField -query -text startFrameTF`;
				string $endFrameARGS = `textField -query -text endFrameTF`;

				//start frame
				if ( `match "[0-9]+" $startFrameARGS` == $startFrameARGS && $startFrameARGS != "")
				{
					$args += ( "-sf " + `match "[0-9]+" $startFrameARGS` + " " );
				}
				else
				{
					error "Start Frame entered data is the wrong format.  Should be an integer.\n";
					return "failed";
				}

				//end frame
				if ( `match "[0-9]+" $endFrameARGS` == $endFrameARGS && $endFrameARGS != "")
				{
					$args += ( "-ef " + `match "[0-9]+" $endFrameARGS` + " " );
				}
				else
				{
					error "End Frame entered data is the wrong format.	Should be an integer.\n";
					return "failed";
				}
			}


			break;
		case "choosePoseRB":
			$args += "-a pose ";

			//set the start frames **Does not check if start frame < end frame **Does not support negative values  **Does not check if start/end frame is within bounds of the scene
			//start frame
			string $startFrameARGS = `textField -query -text startFrameTF`;

			if ( `match "[0-9]+" $startFrameARGS` == $startFrameARGS && $startFrameARGS != "")
			{
				$args += ( "-sf " + `match "[0-9]+" $startFrameARGS` + " " );
			}
			else
			{
				error "Start Frame entered data is the wrong format.  Should be an integer.\n";
				return "failed";
			}
			break;
	}

	//Relative path check
	if ( `radioCollection -query -select texPathOptionsRC` == "chooseCustomTexPathRB" )
	{
		$args += ( "-pd " + "\"" + `textField -query -text customTexPathTF` + "\"" + " " );
	}

	print ( "Using these arguments: " + $args + "[END]\n" );

	return $args;
}

///////////////////////////////////////////////////////////
// Process: export2Egg									 //
///////////////////////////////////////////////////////////
global proc string export2Egg ( string $mbfile, string $destPath, string $destFilename, string $ARGS)
{
        string $pandaEnvSetup = "set PATH=%MAYA_LOCATION%\\bin;%PATH% & ";

	//Check if there is a valid maya binary file to operate on
	if ( $mbfile == "" )
	{
		error "Not a valid Maya Binary file.";

		return "failed";
	}
	//Start export process
	else
	{
		//make the final egg path
		string $eggFile = ( $destPath + $destFilename );

		print ("Your scene will be saved as this egg file: " + $destFilename + "\n" );

		print ("In this directory: " + $destPath + "\n" );

		//Check if overwriting is enabled
		if ( `checkBox -query -value overwriteCB` )
		{
			//Overwrite
			print "!!Overwrite enabled!!\n";
			string $result =
				system
				(
                                        $pandaEnvSetup + $ARGS
					+ " -o "
					+ "\"" + $eggFile + "\""
					+ " "
					+ "\"" + $mbfile + "\""
				);

				print ($result + "\n");
		}
		else
		{
			//Don't overwrite
			string $result =
				system
				(
                                        $pandaEnvSetup + $ARGS
					+ "\"" + $mbfile + "\""
					+ " "
					+ "\"" + $eggFile + "\""
				);

				print ($result + "\n");
		}

		print ("Finished exporting (.mb -> .egg)\n");
		return $eggFile;
	}
}

///////////////////////////////////////////////////////////
// Process: exportButton								 //
///////////////////////////////////////////////////////////
global proc int exportButton ()
{
	//We need to do before calling export2EGG/BAM/Pview:
	//-Export a temporary MB
	//-Get the destination path
	//-Get the filename
	//-Get the custom arguments

	//Export the scene as a temporary file
	string $tempMBFile = "";
	if ( `radioCollection -query -select selectionOptionsRC` == "chooseSceneRB" )
	{
		$tempMBFile = exportScene( "all" );
		if ( $tempMBFile == "failed" )
		{
			return 0;
		}
	}
	else
	{
		$tempMBFile = exportScene( "selected" );
		if ( $tempMBFile == "failed" )
		{
			return 0;
		}
	}

	//get the original scene name
	string $origFileName = filePart ( `file -q -sceneName` );
	$origFileName = `match "^[^\.]*" $origFileName`;  //cut off the file extension

	//-Get the destination path
	//-Get the filename
	//-Get the custom arguments

	string $eggFile = exportPrep ( $tempMBFile, $origFileName );

	if ( $eggFile == "failed" )
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

///////////////////////////////////////////////////////////
// Process: send2Pview									 //
///////////////////////////////////////////////////////////
global proc send2Pview( string $file )
{
	print "\nStarting pview...\n";

	string $result =
				system
				(
                                        "shell pview -l -c "
					+ "\"" + $file + "\""
				);

	print ($result + "\n");
}

///////////////////////////////////////////////////////////
// Process: getFile2Pview								 //
///////////////////////////////////////////////////////////
global proc getFile2Pview()
{
	string $selectedFile = `fileDialog -directoryMask "*.egg;*.bam"`;

	string $origFileName = filePart ( $selectedFile );
	$origFileName = `match "^[^\.]*" $origFileName`;  //cut off the file extension

	if ( $selectedFile == "")
	{
		error ( "No file selected\n" );
	}
	else
	{
		send2Pview ( $selectedFile );
	}
}

///////////////////////////////////////////////////////////
// Process: getFile2Bam 								 //
///////////////////////////////////////////////////////////
global proc getFile2Bam()
{
	string $selectedFile = `fileDialog -directoryMask "*.egg"`;

	if ( $selectedFile == "")
	{
		error ( "No file selected\n" );
	}
	else
	{
		export2Bam ( $selectedFile );
	}
}

///////////////////////////////////////////////////////////
// Process: getFile2Egg 								 //
///////////////////////////////////////////////////////////
global proc getFile2Egg()
{
	string $selectedFile = `fileDialog -directoryMask "*.mb"`;

	string $fileName = filePart ( $selectedFile );
	$fileName = `match "^[^\.]*" $fileName`;  //cut off the file extension

	if ( $selectedFile == "")
	{
		error ( "No file selected\n" );
	}
	else
	{
		exportPrep ( $selectedFile, $fileName );
	}
}
///////////////////////////////////////////////////////////
// Process: exportPrep								 //
///////////////////////////////////////////////////////////
global proc string exportPrep( string $workFile, string $fileName )
{
	//Get the destination path
	string $destPath = "";
	if ( `radioCollection -query -select outputPathOptionsRC` == "chooseDefaultOutputPathRB" )
	{
		$destPath = pathPart($workFile);
	}
	else
	{
		if ( `textField -query -text customOutputPathTF` == "")
		{
			error "Output directory field is empty";
			return "failed";
		}
		else
		{
			$destPath = `textField -query -text customOutputPathTF` + "/";
		}
	}

	//Get the filename
	string $destFilename = "";
	if ( `radioCollection -query -select outputFilenameOptionsRC` == "chooseOriginalFilenameRB" )
	{
		$destFilename = $fileName + ".egg";
	}
	else
	{
		if ( `textField -query -text customFilenameTF` == "")
		{
			error "Custom filename field is empty";
			return "failed";
		}
		else
		{
			$destFilename = `textField -query -text customFilenameTF` + ".egg";
		}
	}

	//-Get the custom arguments
	string $ARGS = argsBuilder();

	if ( $ARGS == "failed" )
	{
		return "failed";
	}

	//export the egg
	string $eggFile = export2Egg ( $workFile, $destPath, $destFilename, $ARGS);

	if ( $eggFile == "failed" )
	{
		return "failed";
	}

	//run pview if option is selected
	if ( `checkBox -query -value pviewCB` )
	{
		send2Pview ( $eggFile );
	}

	if ( `radioCollection -query -select outputFileOptionsRC` == "chooseEggBamRB" )
	{
		export2Bam ( $eggFile );
	}

	return $eggFile;
}

///////////////////////////////////////////////////////////
// Process: export2Bam									 //
///////////////////////////////////////////////////////////
global proc export2Bam ( string $eggFile )
{
	//Check if there is a valid egg file to operate on
	if ( $eggFile == "" )
	{
		//Cannot operate without an egg first
		//throw error if the scene has no associated egg file
		error "Invalid egg file";
	}
	//Call egg2bam
	else
	{
		string $bamFile = "";
		string $fileName = filePart($eggFile);
		string $filePath = pathPart($eggFile);

		//Use either the original fileName or a new fileName for the output
		if ( `radioCollection -query -select outputFilenameOptionsRC` == "chooseCustomFilenameRB" )
		{
			$bamFile = $filePath + `textField -query -text customFilenameTF` + ".bam";
		}
		else
		{
			$bamFile = $filePath + `match "^[^\.]*" $fileName` + ".bam";
		}

		print ("Your file is: " + $fileName + "\nFound in path: " + $filePath + "\n\n");

		print ("Your file will be saved as this bam file: " + $bamFile + "\n");

		//Check if overwriting is enabled
		if ( `checkBox -query -value overwriteCB` )
		{
			//Overwrite
			string $result =
				system
				(
                                        "egg2bam -o "
					+ "\"" + $bamFile + "\""
					+ " "
					+ "\"" + $eggFile + "\""
				);

				print ($result + "\n");
		}
		else
		{
			//Don't overwrite
			string $result =
				system
				(
                                        "egg2bam "
					+ "\"" + $eggFile + "\""
					+ " "
					+ "\"" + $bamFile + "\""
				);

				print ($result + "\n");
		}
	}

	print ("Finished exporting (.egg -> .bam)\n");
}