//
//  Alias|Wavefront Script File
//
//  Creation Date:	2006-2007
//  Author:			Sebastian H. Schmidt www.subsites.org
//  Contact:		sschmidt@subsites.org
//
//	shs_OBJSeqImporter.mel
// 
//	Description:	menu and creation functionality for the shs_objSequence Importer
// 
// 

string $shs_ObjSeqSelect,
		$shs_ObjSeqMNUSelected,
		$shs_ObjSeqMNUClearCache;
string $shs_ObjSeq_CUI_Filename,	// UI Elements
		$shs_ObjSeq_CUI_Create,		// UI Elements
		$shs_ObjSeq_CUI_DataStart,	// UI Elements
		$shs_ObjSeq_CUI_DataEnd,	// UI Elements
		$shs_ObjSeq_CUI_TimeStart,	// UI Elements
		$shs_ObjSeq_CUI_Cache,		// UI Elements
		$shs_ObjSeq_CUI_MergeVertCheck,		// UI Elements
		$shs_ObjSeq_CUI_MergeVertSld;	// UI Elements
string $shs_ObjSeq_CUI_FI_Name,		// Fileinfo
		$shs_ObjSeq_CUI_FI_Start,	// Fileinfo
		$shs_ObjSeq_CUI_FI_End,		// Fileinfo
		$shs_ObjSeq_CUI_FI_Padding;	// Fileinfo

string	$shs_ObjSeq_CUI_FI_Name,
		$shs_ObjSeq_CUI_FI_Start,
		$shs_ObjSeq_CUI_FI_End,
		$shs_ObjSeq_CUI_FI_Padding;


global proc shs_ObjSeqClearCache()
{
	global string $shs_ObjSeqSelect;
	setAttr ($shs_ObjSeqSelect +".ClearCacheIn") 1;
	getAttr ($shs_ObjSeqSelect +".ClearCache");

}

//************************
//	Mark functionality
//************************

global proc shs_ObjSeq_NewScene()
{
	global string $shs_ObjSeqSelect;
	$shs_ObjSeqSelect = "";
	shs_ObjSeqCheckSelection;
}

global proc shs_ObjSeqSelect()
{
	global string $shs_ObjSeqSelect;
	$selection = `ls -sl`;
	$shs_ObjSeqSelect = "";

	for ($obj in $selection)
	{
		$nType = `nodeType $obj`;
		if ($nType =="shsObjSequence")
		{
			$shs_ObjSeqSelect = $obj;
			scriptJob -ro 1 -e "deleteAll" "shs_ObjSeq_NewScene";
			break;
		}	
	}
	if ($shs_ObjSeqSelect=="")
	{
		warning " no .obj sequence node selected";
	}
	shs_ObjSeqCheckSelection;
}

global proc int shs_ObjSeq_CheckExistance()
{
	global string $shs_ObjSeqSelect;
	int $ret_val = 1;
	if (($shs_ObjSeqSelect!= "") && (!`objExists $shs_ObjSeqSelect`))
	{
			$ret_val = 0;
			$shs_ObjSeqSelect="";
			warning "former selected node does not exist anymore";
	}
	return $ret_val;
}

global proc shs_ObjSeqCheckSelection()
{
	global string $shs_ObjSeqSelect,$shs_ObjSeqMNUSelected,$shs_ObjSeqMNUClearCache;
	if (($shs_ObjSeqSelect =="") || (!`shs_ObjSeq_CheckExistance`))
	{
		menuItem -e -label "marked Seq.: none" -command "" -en 0 $shs_ObjSeqMNUSelected;
		menuItem -e -en 0 $shs_ObjSeqMNUClearCache;
		
	}
	else
	{
		menuItem -e -label ("marked Seq.: "+ $shs_ObjSeqSelect) -command ("select -r "+$shs_ObjSeqSelect) -en 1 $shs_ObjSeqMNUSelected;
		menuItem -e -en 1 $shs_ObjSeqMNUClearCache;
	}
}
global proc shs_ObjSeqClearSelection()
{
	global string $shs_ObjSeqSelect;
	$shs_ObjSeqSelect = "";
}


global proc string shs_Helper_LeadingZero(int $numb, int $padd)
{
	string $ret_val = $numb;
	while (size($ret_val) < $padd)
	{
		$ret_val = ("0"+$ret_val);
	}
	return $ret_val;
}

//*************************************************
//	Create Functionality
//*************************************************

global proc int shs_ObjSeqCreate_getPadding(string $filename)
{
	$fname = `basename $filename ""`;
	$dname = `dirname $filename`;
	
	$number = `match "[0-9]+" $fname`;
	int $ret_val = 0;
	if ((`substring $number 1 1`=="0") && (`size $number` >1))
	{
		$ret_val = size($number);
	}

	return $ret_val;
}

global proc int shs_ObjSeqCreate_getNumber(string $filename)
{
	$fname = `basename $filename ""`;
	$dname = `dirname $filename`;
	
	$number = `match "[0-9]+" $fname`;
	
	$ret_val = $number;
	return $ret_val;
}
global proc string shs_ObjCreate_genNewName(string $filename, int $padd, int $idx)
{
	$fname = `basename $filename ""`;
	$dname = `dirname $filename`;
	
	$number = `match "[0-9]+" $fname`;
	string $buffer[];
	tokenize $filename $number $buffer;

	$newIdx = `shs_Helper_LeadingZero $idx $padd`;


	string $newName = `stringArrayToString $buffer $newIdx`;
	if (size($buffer)==1)
	{
		$newName = ($newIdx + $buffer[0]);
	}
	
	return $newName;

}

global proc int shs_ObjSeqCreate_hSeqStart(string $filename, int $padd)
{
	
	$fname = `basename $filename ""`;
	$dname = `dirname $filename`;

	int $idx = `shs_ObjSeqCreate_getNumber $fname`;

	string $newFile = ($dname + "/" + `shs_ObjCreate_genNewName $fname $padd $idx`);

	while (`filetest -r $newFile`) 
	{
		$idx--;
		$newFile = ($dname + "/" + `shs_ObjCreate_genNewName $fname $padd $idx`);

	}
	$idx++;
	
	return $idx;
}

global proc int shs_ObjSeqCreate_hSeqEnd(string $filename, int $padd)
{
	$fname = `basename $filename ""`;
	$dname = `dirname $filename`;

	int $idx = `shs_ObjSeqCreate_getNumber $fname`;
	string $newFile = ($dname + "/" + `shs_ObjCreate_genNewName $fname $padd $idx`);
	
	while (`filetest -r $newFile`) 
	{
		$idx++;
		$newFile = ($dname + "/" + `shs_ObjCreate_genNewName $fname $padd $idx`);
	}
	$idx--;
	
	return $idx;
}



global proc shs_Helper_SliderAlign(string $sliderLow,string $sliderHigh, string $editSlider)
{
	$actVal = `intSliderGrp  -q -v $editSlider`;
	$minVal = `intSliderGrp  -q -v $sliderLow`;
	$maxVal = `intSliderGrp  -q -v $sliderHigh`;
	if (($actVal == $maxVal) && ($maxVal < $minVal))
	{
		intSliderGrp  -e -v $maxVal $sliderLow;
	}
	if (($actVal == $minVal) && ($minVal > $maxVal))
	{
		intSliderGrp  -e -v $minVal $sliderHigh;
	}
}
global proc string shs_ObjSeq_genPaddFName(string $filename,string $pChar)
{
	$fname = `basename $filename ""`;
	$dname = `dirname $filename`;
	$number = `match "[0-9]+" $fname`;
	string $buffer[];
	tokenize $fname $number $buffer;
	string $ret_val = ($dname + "/" + `stringArrayToString $buffer $pChar`);
	if (size($buffer)==1)
	{
		$ret_val = ($dname + "/" + $pChar + $buffer[0]);
	}
	return $ret_val;

}


global proc shs_ObjSeq_Create()
{
	global string $shs_ObjSeq_CUI_Filename,$shs_ObjSeq_CUI_DataStart,$shs_ObjSeq_CUI_DataEnd,$shs_ObjSeq_CUI_TimeStart,$shs_ObjSeq_CUI_Cache;
	global string $shs_ObjSeq_CUI_MergeVertCheck,$shs_ObjSeq_CUI_MergeVertSld;
	

	$filename	= `textField -q -tx $shs_ObjSeq_CUI_Filename`;
	$nfilename	= `shs_ObjSeq_genPaddFName $filename "#"`;
	$paddingNr	= `shs_ObjSeqCreate_getPadding $filename`;
	$padding = 0;
	
	if ($paddingNr>0) $padding = 1;
	$dStart			= `intSliderGrp -q -v $shs_ObjSeq_CUI_DataStart`;
	$dEnd			= `intSliderGrp -q -v $shs_ObjSeq_CUI_DataEnd`;
	$sStart			= `intSliderGrp -q -v $shs_ObjSeq_CUI_TimeStart`;
	$cache			= `checkBox -q -v $shs_ObjSeq_CUI_Cache`;
	$mergeNode		= `checkBox -q -v $shs_ObjSeq_CUI_MergeVertCheck`;
	$mergeThreshold = `floatSliderGrp -q -v $shs_ObjSeq_CUI_MergeVertSld`;

	shs_ObjSeqCreate $nfilename  "#" $padding $paddingNr $mergeNode $mergeThreshold $dStart $dEnd $sStart $cache;
}
 

global proc shs_ObjSeqCreate_hFile(string $filename, string $filter)
{
	global string $shs_ObjSeq_CUI_Filename,$shs_ObjSeq_CUI_DataStart,$shs_ObjSeq_CUI_DataEnd,$shs_ObjSeq_CUI_TimeStart,$shs_ObjSeq_CUI_TimeEnd;
	global string $shs_ObjSeq_CUI_FI_Name,$shs_ObjSeq_CUI_FI_Start,$shs_ObjSeq_CUI_FI_End,$shs_ObjSeq_CUI_FI_Padding,$shs_ObjSeq_CUI_Create;
	global string $shs_ObjSeq_CUI_MergeVertCheck,$shs_ObjSeq_CUI_MergeVertSld;
	$fname		= `basename $filename ""`;
	$padd		= `shs_ObjSeqCreate_getPadding $filename`;
	$seqStart	= `shs_ObjSeqCreate_hSeqStart $filename $padd`;
	$seqEnd		= `shs_ObjSeqCreate_hSeqEnd $filename $padd`;

	text -e -label ("Filename       : " + $fname)		$shs_ObjSeq_CUI_FI_Name;
	text -e -label ("StartFrame     : " + $seqStart)	$shs_ObjSeq_CUI_FI_Start;
	text -e -label ("EndFrame       : " + $seqEnd)		$shs_ObjSeq_CUI_FI_End;
	text -e -label ("Leading Zeros  : " + $padd)		$shs_ObjSeq_CUI_FI_Padding;
	textField -e -tx $filename $shs_ObjSeq_CUI_Filename;

	intSliderGrp -e -en 1 -v 0			-fmn $seqStart -fmx $seqEnd -min $seqStart -max $seqEnd -dc ("shs_Helper_SliderAlign " + $shs_ObjSeq_CUI_DataStart+ " "+ $shs_ObjSeq_CUI_DataEnd + " " + $shs_ObjSeq_CUI_DataStart) $shs_ObjSeq_CUI_DataStart;
	intSliderGrp -e -en 1 -v $seqEnd	-fmn $seqStart -fmx $seqEnd -min $seqStart -max $seqEnd -dc ("shs_Helper_SliderAlign " + $shs_ObjSeq_CUI_DataStart+ " "+ $shs_ObjSeq_CUI_DataEnd + " " + $shs_ObjSeq_CUI_DataEnd) $shs_ObjSeq_CUI_DataEnd ;
	button -e -en 1 $shs_ObjSeq_CUI_Create;
}


global proc shs_ObjSeqCreate_UI()
{
	global string $shs_ObjSeq_CUI_Filename,$shs_ObjSeq_CUI_DataStart,$shs_ObjSeq_CUI_DataEnd,$shs_ObjSeq_CUI_TimeStart,$shs_ObjSeq_CUI_Cache;
	global string $shs_ObjSeq_CUI_FI_Name,$shs_ObjSeq_CUI_FI_Start,$shs_ObjSeq_CUI_FI_End,$shs_ObjSeq_CUI_FI_Padding,$shs_ObjSeq_CUI_Create;
	global string $shs_ObjSeq_CUI_MergeVertCheck,$shs_ObjSeq_CUI_MergeVertSld;
	//-widthHeight 750 450 
	string $window = `window -title "add .obj Sequence" -iconName ".obj Sequence" -widthHeight 750 450  -s 0`;

	string $form = `formLayout -numberOfDivisions 100`;

	string $fileName		= `text		-rs 1	-label "Sequence" `;
	string $fileText		= `textField -w 300	-tx "" `;
	$shs_ObjSeq_CUI_Filename = $fileText;
	string $fileButton		= `button	-h 20 -w 75	-label "select" -command "fileBrowserDialog -m 0 -fc \"shs_ObjSeqCreate_hFile\" -ft \"OBJ\" -an \"Import .obj Sequence\" -om \"Import\";"`;

	string $fileInfoLbl		= `text -rs 1 -label "Sequence Info:"`;
	string $fileInfoName	= `text -rs 1 -label "Filename      : -"`;
	string $fileInfoStart	= `text -rs 1 -label "StartFrame    : -"`;
	string $fileInfoEnd		= `text -rs 1 -label "EndFrame      : -"`;
	string $fileInfoPadding	= `text -rs 1 -label "Leading Zeros : -"`;

	
	$shs_ObjSeq_CUI_FI_Name		= $fileInfoName;
	$shs_ObjSeq_CUI_FI_Start	= $fileInfoStart;
	$shs_ObjSeq_CUI_FI_End		= $fileInfoEnd;
	$shs_ObjSeq_CUI_FI_Padding	= $fileInfoPadding;
	

	string $dataText		= `text  -rs 1 -label "Sequence" `;
	string $dataStartSldLbl	= `text -rs 1 -label "In  "`;
	string $dataStartSld	= `intSliderGrp -cw3 1 30 10 -f 1 -label ""  -en 0`;
	string $dataEndSldLbl	= `text -rs 1 -label "Out  "`;
	string $dataEndSld		= `intSliderGrp	-cw3 1 30 10 -f 1  -label "" -en 0`	;

	string $timeText		= `text  -rs 1 -label "Scene Starttime" `;
	string $timeStartSld	= `intSliderGrp -cw3 1 50 10 -f 1 -min 0 -max 1000 -label ""`;

	string $cachingChkbx	= `checkBox -label "allow File Caching" -v 1`;
	$shs_ObjSeq_CUI_Cache	= $cachingChkbx;


	
	string $MergeVertLabel	= `text -rs 1 -label "Merge Vertex Node"`;
	string $MergeVertDistSld= `floatSliderGrp -cw3 100 50 10 -f 1 -precision 4 -max 1.0 -min 0.0 -label  "Threshold" -v 0.001 "shs_ObjSeq_UIMergVertexSlider"`;
	string $MergeVertCheck	= `checkBox -label "add" -onc ("floatSliderGrp -e -en 1 "+$MergeVertDistSld) -ofc ("floatSliderGrp -e -en 0 "+$MergeVertDistSld) -v 1 `;
	
	$shs_ObjSeq_CUI_MergeVertCheck	= $MergeVertCheck;
	$shs_ObjSeq_CUI_MergeVertSld	= $MergeVertDistSld;
	
	
	
	string $buttonCreate	= `button -w 200 -h 30 -label "create" -command ("shs_ObjSeq_Create; deleteUI " + $window + ";") -en 0`;
	string $buttonCancel	= `button -w 200 -h 30 -label "cancel" -command ("deleteUI " + $window)`;

	$shs_ObjSeq_CUI_Create		= $buttonCreate;
	$shs_ObjSeq_CUI_DataStart	= $dataStartSld;
	$shs_ObjSeq_CUI_DataEnd		= $dataEndSld;
	$shs_ObjSeq_CUI_TimeStart	= $timeStartSld;
	
	string $copyRText  = `text -label "(c) 06-07 shs"`;	

	formLayout -edit
		
		-attachForm     $fileName     "top"  20 
		-attachForm     $fileName     "left"  20 
		
		-attachForm		$fileText		"top"  20 
		-attachControl  $fileText		"left"  20	$dataStartSldLbl

		-attachForm		$fileButton		"top"  20 
		-attachControl  $fileButton		"left"  20	$fileText


		-attachControl  $fileInfoLbl	"top"  20	$fileName
		-attachForm		$fileInfoLbl	"left"  20	


		-attachControl  $fileInfoName		"top"  20	$fileName
		-attachControl	$fileInfoName		"left"  20	$dataStartSldLbl

		-attachControl  $fileInfoStart		"top"  5	$fileInfoName
		-attachControl	$fileInfoStart		"left"  20	$dataStartSldLbl

		-attachControl  $fileInfoEnd		"top"  5	$fileInfoStart
		-attachControl	$fileInfoEnd		"left"  20	$dataStartSldLbl

		-attachControl  $fileInfoPadding	"top"  5	$fileInfoEnd
		-attachControl	$fileInfoPadding	"left"  20	$dataStartSldLbl


		-attachControl  $dataText		"top"  20	$fileInfoPadding
		-attachForm		$dataText		"left"  20	

		-attachControl  $dataStartSldLbl	"top"  20	$fileInfoPadding
		-attachControl  $dataStartSldLbl	"left"  10	$dataText

		-attachControl  $dataStartSld	"top"  20	$fileInfoPadding
		-attachControl  $dataStartSld	"left"  10	$dataStartSldLbl
		

		-attachControl  $dataEndSldLbl	"top"  20	$fileInfoPadding
		-attachControl  $dataEndSldLbl	"left"  10	$dataStartSld
		

		-attachControl  $dataEndSld		"top"  20	$fileInfoPadding
		-attachControl  $dataEndSld		"left"  10	$dataEndSldLbl
		
		-attachControl  $timeText		"top"  20	$dataStartSld
		-attachForm		$timeText		"left"  20	
		
		-attachControl  $timeStartSld	"top"  20	$dataStartSld
		-attachControl  $timeStartSld	"left"  10	$dataStartSldLbl

		-attachControl  $cachingChkbx	"top"  20	$timeStartSld
		-attachControl  $cachingChkbx	"left"  20	$dataStartSldLbl

		-attachControl  $MergeVertLabel	"top"  20	$cachingChkbx
		-attachForm		$MergeVertLabel	"left"  20	

		-attachControl  $MergeVertCheck	"top"  20	$cachingChkbx
		-attachControl	$MergeVertCheck	"left"  20	$dataStartSldLbl

		-attachControl  $MergeVertDistSld	"top"  20	$cachingChkbx
		-attachControl	$MergeVertDistSld	"left"  20	$MergeVertCheck



		-attachForm		$buttonCreate	"bottom"  20	
		-attachForm		$buttonCreate	"left"  20	
		
		-attachForm		$buttonCancel	"bottom"  20	
		-attachForm		$buttonCancel	"right"  20	
		
		-attachForm     $copyRText    "bottom" 1
		-attachForm     $copyRText    "right" 1

		$form;
	window -e -widthHeight 630 450 -s 0 $window;
	showWindow $window;	
}


global proc shs_ObjSeqCreate(string $filename, 
								string $paddingChar, int $paddingEnable, int $paddingSize, 
								int $mergeNode, float $mergeThreshold,
								int $dataStart,int $dataEnd,
								int $sceneStart, int $enableCache)
{
	string $objName		= `createNode "shsObjSequence"`;
	string $meshName	= `createNode "mesh"`;
	string $timeName[]	= `ls -et"time"`;

	connectAttr -f ($objName + ".Mesh") ($meshName + ".inMesh");
	connectAttr -f ($timeName[0] + ".outTime") ($objName + ".InTime");
	setAttr -type "string" ($objName + ".Filename") $filename;
	setAttr -type "string" ($objName + ".PaddingChar") $paddingChar;
	
	setAttr ($objName + ".LeadingZeros")	$paddingEnable;
	setAttr ($objName + ".PaddingSize")		$paddingSize;

	setAttr ($objName + ".StartFrame")		$dataStart;
	setAttr ($objName + ".StopFrame")		$dataEnd;
	setAttr ($objName + ".DisplayFrame")	$sceneStart;
	setAttr ($objName + ".EnableCaching")	$enableCache;



	if ($mergeNode)
	{
		polyMergeVertex  -d $mergeThreshold -ch 1 $meshName;
	}
}

global string $SHS_ToolMenueName		= "shsMyTools";
global string $SHS_ObjSeqSubMenueName	= "shsObjSeq";
global proc SHS_CreateMenuEntry()
{
	global string $SHS_ToolMenueName;
	if (`menuEditor -exists $SHS_ToolMenueName` == 0)
	{
		menu -label "shs-Tools" -aob 1 -tearOff true -p "MayaWindow" $SHS_ToolMenueName;
	}
	else 
	{
		menuItem -divider true	-parent $SHS_ToolMenueName;
	}

}

global proc SHS_AddMenu_ObjSequence()
{
	global string $shs_ObjSeqSelect,$shs_ObjSeqMNUSelected,$shs_ObjSeqMNUClearCache,$SHS_ToolMenueName,$SHS_ObjSeqSubMenueName;
	
	menuItem -subMenu 1 -label ".obj Sequence" -aob 1 -tearOff true -p $SHS_ToolMenueName;
								menuItem -label "Add Sequence"		-command "shs_ObjSeqCreate_UI"	/*-parent $SHS_ObjSeqSubMenueName*/;
								menuItem -divider true												/*-parent $SHS_ObjSeqSubMenueName*/;
	$shs_ObjSeqMNUSelected 	=  `menuItem -label ("marked Seq.: none") -en 0							/*-parent $SHS_ObjSeqSubMenueName*/`;
								menuItem -divider true												/*-parent $SHS_ObjSeqSubMenueName*/;
								menuItem -label "mark Sequence"			-command "shs_ObjSeqSelect" /*-parent $SHS_ObjSeqSubMenueName*/;
	$shs_ObjSeqMNUClearCache = `menuItem -label "clear marked Sequence Cache"	-en 0	-command "shs_ObjSeqClearCache" /*-parent $SHS_ObjSeqSubMenueName*/`;
}

global proc shs_ObjSeqInitMenu()
{
	SHS_CreateMenuEntry;
	SHS_AddMenu_ObjSequence;
}

