// Copyright (C) 1997-2004 Alias Systems Corp.
// 
// The information in this file is provided for the exclusive use of the
// licensees of Alias.  Such users have the right to use, modify,
// and incorporate this code into other products for purposes authorized
// by the Alias license agreement, without fee.
// 
// ALIAS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
// INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
// EVENT SHALL ALIAS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
// DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

//
// Description : Script to prepare a scene so that a scene with some types
//				 of spurrious light linker nodes may be cleaned up by running
//				 the script, saving the scene, re-opening the scene (which
//				 will trigger the actual clean-up) and re-saving the scene.
//
// Warning : This is an example script, use at your own risk.
//
// Example:
//		Suppose you have a scene that has a number of light linker nodes,
// and that Maya refuses to clean them up. One possibility is that these nodes
// are not linked to the lightList (e.g., they were created in batch mode in a
// Maya 4.0 or before), so Maya doesn't know that it has to clean them up. You
// could use this script as follows to get Maya to clean up such spurrious
// light linker nodes:
//
// 	o Open your scene
//	o Source this script and run prepareForLightLinkerCleanup
//	o Save your scene
//	o Re-open your scene (To get Maya to clean up the spurrious light linkers)
//	o Save your scene again.
//
// From this point on, the spurrious light linker nodes should be gone.
// Depending on the the number of light linkers to clean up, the Re-open step
// could take a long time, but will only have to be done once.
//

// prepareForLightLinkerCleanup.
//
//	Cleans up a file by going through the list of light linkers, and linking
//	them to the lightList if that hasn't already been done.
//
global proc prepareForLightLinkerCleanup()
{
	// First, get the lightList
	//
	string $lightLists[] = `ls -type lightList`;
	string $lightList;
	if( size($lightLists) == 0 ){
		// Create one, if necessary
		//
		warning( "No light list, creating one.\n" );
		$lightList = `createNode -type lightList`;
	} else {
		$lightList = $lightLists[0];
		if( size($lightLists) > 1 ){
			warning( "Multiple light lists, using " + $lightList + "\n" );
		}
	}

	// Now get the light linkers
	//
	string $lightLinkers[] = `ls -type lightLinker`;
	$numLightLinkers = size($lightLinkers);
	$index = 0;
	while( $index < $numLightLinkers ){
		string $lightLinker = $lightLinkers[$index];
		
		// Check to see if this light linker is already connected to the
		// light list.
		//
		string $dstPlugs[] = `connectionInfo -dfs ($lightLinker + ".msg")`;
		int $addLink = true;
		$numPlugs = size($dstPlugs);
		$plugIndex = 0;
		while( $plugIndex < $numPlugs ){
			if( plugNode( $dstPlugs[0]) == $lightList ){
				// Found one, we don't need to add a link; bail out.
				//
				$addLink = false;
				break;
			}
			$plugIndex++;
		}
		if( $addLink ){
			// Connect this light linker to the light list
			//
			connectAttr -na ($lightLinker + ".msg") ($lightList + ".ln");
		}
		$index++;
	}
}

