// Copyright 2014 Autodesk, Inc. All rights reserved. 
//
// Use of this software is subject to the terms of the Autodesk 
// license agreement provided at the time of installation or download, 
// or which otherwise accompanies this software in either electronic 
// or hard copy form.

//-
// ==========================================================================
//
// ==========================================================================
//+

//
//	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] );
}
