//-
// ==========================================================================
// Copyright (C) 1995 - 2005 Alias Systems Corp. and/or its licensors.  All 
// rights reserved. 
// 
// The coded instructions, statements, computer programs, and/or related 
// material (collectively the "Data") in these files are provided by Alias 
// Systems Corp. ("Alias") and/or its licensors for the exclusive use of the 
// Customer (as defined in the Alias Software License Agreement that 
// accompanies this Alias software). Such Customer has the right to use, 
// modify, and incorporate the Data into other products and to distribute such 
// products for use by end-users.
//  
// THE DATA IS PROVIDED "AS IS".  ALIAS HEREBY DISCLAIMS ALL WARRANTIES 
// RELATING TO THE DATA, INCLUDING, WITHOUT LIMITATION, ANY AND ALL EXPRESS OR 
// IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND/OR FITNESS FOR A 
// PARTICULAR PURPOSE. IN NO EVENT SHALL ALIAS BE LIABLE FOR ANY DAMAGES 
// WHATSOEVER, WHETHER DIRECT, INDIRECT, SPECIAL, OR PUNITIVE, WHETHER IN AN 
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, OR IN EQUITY, 
// ARISING OUT OF ACCESS TO, USE OF, OR RELIANCE UPON THE DATA.
// ==========================================================================
//+

//
//	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] );
}
