//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//srbMultiCache.mel - MEL Script
//Copyright (C) Suchan Raj Bajracharya
//Title: SRB - Multiple Cache v0.3
//AUTHOR:	Suchan Raj Bajracharya
//				suchan@incessantrain.com
//				srb_yagya@hotmail.com		
//DATE: October 23rd, 2008
//DESCRIPTION: Use this Tool to automatically create geometry cache for selected geometries...
//USAGE: Select geometries to cache...
//			 Enter the directory path...(d:/storage/data/geo)
//			 Enter the start, end time  and click apply
//NOTE: Use substitute reference name in case the user want to substitute ":" or strings while referencing...
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//CREATE THE GLOBAL PROCEDURE---

global proc srbMultiCache() {

//USER INTERFACE---

if (`window -exists srbMultiCacheWin`) deleteUI srbMultiCacheWin;
window
	-title "SRB - Multiple Cache v0.3"
	-rtf 1
	-maximizeButton 0
srbMultiCacheWin;

columnLayout -adj 1;

textFieldButtonGrp 
	-label "Cache directory:" 
	-text "Enter cache file directory path (d:/data/sphere)..."
	-buttonLabel "Browse"
	-buttonCommand "srbFileBrowser"
dirPathTFG;

intSliderGrp  -field 1  -label "Start Frame:"
	-minValue -0 -maxValue 100
  -fieldMinValue -1000 -fieldMaxValue 1000
  -value ( `playbackOptions  -q  -min ` )
startFrameISG;
    
intSliderGrp -field 1  -label "End Frame:" 
	-minValue -0 -maxValue 100
  -fieldMinValue -1000-fieldMaxValue 1000
  -value ( `playbackOptions  -q  -max ` )
endFrameISG;

textFieldGrp 
	-label "Substitute reference name:" 
	-text ""
substituteTFG;

button -l "Export" -c "srbMultiCacheExportProc";
button -l "Import" -c "srbMultiCacheImportProc";

showWindow srbMultiCacheWin;

}

//CREATE THE MAIN PROCEDURE---

global proc srbMultiCacheExportProc () {

//DECLARE AND STORE SOME VARIABLES---
global string $gMainProgressBar;
string $dirPath = `textFieldGrp -query -text dirPathTFG`;
int $startFrameVal = `intSliderGrp -query -value startFrameISG`;
int $endFrameVal = `intSliderGrp -query -value endFrameISG`;

string $replaceName = `textFieldGrp -query -text substituteTFG`;

string $sel[] = `ls -sl`;
int $sizeSel = `size($sel)`;

//CREATE AND EDIT THE CUSTOM PROGRESSBAR ---

progressBar 
	-edit
	-beginProgress
	-isInterruptable true 
	-status "Exporting caches for selected objects...." 
	-maxValue ($sizeSel)
	$gMainProgressBar;

if ($sizeSel == 0)
{
warning "Please select at least one geometry to export cache...";
}

else
{

for ($obj in $sel)
{
if(`progressBar -query -isCancelled $gMainProgressBar`)
		{break;
		print "Exporting cache file cancelled by user...\n";
		}
	
	progressBar 
	-edit
	-step 1 
	$gMainProgressBar;
			
string $newName = `substitute  $replaceName $obj ("")`;
string $newDirPath = ($dirPath + "/" + $newName );
select -r $obj;
doCreateGeometryCache 4 { "3", $startFrameVal, $endFrameVal, "OneFilePerFrame", "1", $newDirPath,"0","","0", "add", "0", "1", "1","0","0" } ;
}

//END PROGRESS BAR---
progressBar -edit
	-endProgress
	$gMainProgressBar;
	
print "SRB - Multiple Cache v0.3 has completed exporting geometry cache for selected objects...";
}
}	

global proc srbMultiCacheImportProc ()
{
string $dirPath = `textFieldGrp -query -text dirPathTFG`;
int $startFrameVal = `intSliderGrp -query -value startFrameISG`;
int $endFrameVal = `intSliderGrp -query -value endFrameISG`;

string $replaceName = `textFieldGrp -query -text substituteTFG`;

string $sel[] = `ls -sl`;
int $sizeSel = `size($sel)`;

if ($sizeSel == 0)
{
warning "Please select at least one geometry to import cache...";
}

else
{

//MAIN LOOP---
for ($obj in $sel)
{
string $colon = ":";
string $newName = `substitute  $replaceName $obj ("")`;
string $cacheName = `substitute $colon $obj("_")`;
string $newDirPath = ($dirPath + "/" + $newName + "/" + $cacheName + "Shape.xml");
select -r $obj;

importCacheFile $newDirPath "xml";
}
print "SRB - Multiple Cache v0.3 has completed importing geometry cache for selected objects...";
}
}		

global proc srbFileBrowser ()
{
fileBrowserDialog -mode 4 -fc "srbUpdateDirectory" -an "Pick your cache file directory:";
return;
}

global proc srbUpdateDirectory (string $path, string $mode)
{
	string $bases[] = `ls -sl`;
	float $size = `size $bases`;
	string $base = $bases[0];
	select -ado;
	string $before[] = `ls -sl`;

textFieldButtonGrp -edit -text $path dirPathTFG;	
}

//IMPORTANT PIECE OF CODE---
source doImportCacheFile.mel;

global proc int 
importCacheFile(string $fileName, string $fileType)

{
	string $geometries[];
	return doImportCacheFile($fileName, $fileType,$geometries,{});
}

//THE END---
	    