#ifdef WANT_I18N
// _L10N( kBuildWarningPacifier, "This file requires no tagging." );
#endif
//-
// ==========================================================================
// Copyright (C) 1995 - 2006 Autodesk, Inc. and/or its licensors.  All 
// rights reserved.
//
// The coded instructions, statements, computer programs, and/or related 
// material (collectively the "Data") in these files contain unpublished 
// information proprietary to Autodesk, Inc. ("Autodesk") and/or its 
// licensors, which is protected by U.S. and Canadian federal copyright 
// law and by international treaties.
//
// The Data is provided for use exclusively by You. You have the right 
// to use, modify, and incorporate this Data into other products for 
// purposes authorized by the Autodesk software license agreement, 
// without fee.
//
// The copyright notices in the Software and this entire statement, 
// including the above license grant, this restriction and the 
// following disclaimer, must be included in all copies of the 
// Software, in whole or in part, and all derivative works of 
// the Software, unless such copies or derivative works are solely 
// in the form of machine-executable object code generated by a 
// source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND. 
// AUTODESK DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED 
// WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF 
// NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR 
// PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE, OR 
// TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS LICENSORS 
// BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL, 
// DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK 
// AND/OR ITS LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY 
// OR PROBABILITY OF SUCH DAMAGES.
//
// ==========================================================================
//+

//
//	Procedure Name:
//	connectObjectToPointOnSubd
//
//	Description Name;
//	Given an object and a subd face selection, hook the objects
//  translate and rotate so that it would stick to the middle of
//  the subd face.  You can adjust it from the middle after the fact
//  in the attribute editor.  The object should start "Y" up; if it
//  isn't, you need to change the value on the angle between node's
//  vector1 attribute.
//
//  Here's an example of how to use it:
//
//      loadPlugin pointOnSubdNode;
//
//      file -f -new;
//      polyCube -w 1 -h 1 -d 1 -sx 1 -sy 1 -sz 1 -ax 0 1 0 -tx 1 -ch 1;
//      cone -p 0 0 0 -ax 0 1 0 -r 1 -hr 2 -d 3 -ut 0 -s 8 -nsp 1 -ch 1;
//      scale -r 0.05 0.05 0.05 ;
//      select -r pCube1 nurbsCone1 ;
//      group;
//      scale -r 20 20 20;
//      select -r pCube1 ;
//      CreateSubdivSurface;
//      subdivDisplaySmoothness -smoothness 3;
//      select -r nurbsCone1 ;
//      select -tgl pCube1.smf[2][67108864] ;
//      connectObjectToPointOnSubd;
//
//	Input Value:
//		object - the object to "stick" on the middle of the given
//               subd face
//		face - the face on the subdivision surface that gets this
//               object stuck in the middle
//
//	Output Value:
//		None
//

global proc connectObjectToPointOnSubdOne( string $object, string $face )
{
	int $firstIndex = -1;
	int $secondIndex = -1;
	string $subd = "";

	string $tok[];
	int $nf = `tokenize $face ".smf\\[" $tok`;

	if( $nf > 2 ) {
		string $tmpS;
		$subd = substring( $tok[0], 1, size($tok[0]) );

		$tmpS = substring($tok[1], 1, size($tok[1])-1 );
		$firstIndex = (int)$tmpS;

		$tmpS = substring($tok[2], 1, size($tok[2])-1 );
		$secondIndex = (int)$tmpS;
	}

	if( "" != $subd ) {
		string $pointOnSubd = `createNode pointOnSubd`;
		string $angleBetween = `createNode angleBetween`;

		// Put it in the middle of the face
		setAttr ($pointOnSubd + ".faceFirst") $firstIndex;
		setAttr ($pointOnSubd + ".faceSecond") $secondIndex;

		setAttr ($pointOnSubd + ".uValue") 0.5;
		setAttr ($pointOnSubd + ".vValue") 0.5;
		setAttr ($pointOnSubd + ".relative") 1;

		connectAttr ($pointOnSubd + ".point") ($object + ".translate");
		connectAttr ($pointOnSubd + ".normal") ($angleBetween + ".vector2");
		connectAttr ($angleBetween + ".euler") ($object + ".rotate");

		connectAttr ($subd + ".outSubdiv") ($pointOnSubd + ".subd");
	}
}

global proc connectObjectToPointOnSubd()
{
	string $sel[] = `ls -sl`;
	if( size($sel) < 2 ) {
		error("Select an object followed by a subdivision surface face.");
		return;
	}

	string $subds[] = `filterExpand -ex true -sm 38`;
	if( size($sel) < 1 ) {
		error("Select an object followed by a subdivision surface face.");
		return;
	}

	connectObjectToPointOnSubdOne( $sel[0], $subds[0] );
}
