// 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.

global proc int performPolyCopyUVsToUVsetArgList (
	string $version,
	string $args[]
	)
//
//	$args
//	Version 1
//	[0]	$inputUVset:   The uv set to copy from.
//	[1]	$toUVset:      The uv set to be copied to.
//	[2]	$createNewSet: Whether or not to force creation of a new UV set
{
	// First version of this proc, size of array must be 3.
	if( size($args) != 3) {
		error("Copy UVs to UV Set: Incorrect number of paramaters in call to performPolyCopyUVsToUVsetArgList.");
		return 0;
	}

	string $inputUVset = $args[0];
	string $toUVset = $args[1];
	int $createNewSet = $args[2];

	// Check that there is a valid selection: single poly object
	// or selected faces from a single objct
	// else display warning
	// Returns >0 if something was done, otherwise returns 0

	// Check first if there's a selected object
	// It must be a mesh shape or its child shape must be a mesh shape
	// Or it must be 

	string $selection[] = `ls -sl`;
	string $selectComp[] = `filterExpand -ex false -sm 34`;	// 34 means poly mesh faces
	string $selectObj[] = `ls -o -sl`;
	int $numObjects = size($selectObj);
	string $shapes[]; 
	if( $numObjects > 0) {
		$shapes = `listRelatives -shapes -fullPath $selectObj[0]`;
	}

	if( (($numObjects > 0)  && (`nodeType $selectObj[0]` == "mesh"))
		|| ((size($shapes) > 0) && (`nodeType $shapes[0]` == "mesh" ))) 
	{
		// if the object doesn't have any uvsets, then
		// warning that it's not allowed and return 0
		string $uvsets[] = {"BootUVs" }; //`getUVs($inputUVset)`;
		if( size($uvsets) == 0 ) {
			error("Copy UVs to UV Set: The current UVset is empty.  No copying was done.");
			return 0;
		}

		// Get the selected components, if any	
		string $copyCmd = ( "polyCopyUV -uvSetNameInput \"" 
					+ $inputUVset + "\" -uvSetName \""
					+ $toUVset + "\" ");
					
		if ($createNewSet) {
			$copyCmd += "-createNewMap 1";
		}

		// If it's only one object with face components, then put no argument
		// If it's only one object with no face componnets, then convert the obj to its faces
		// If it's >1 object, then put the first selection item

		if( ($numObjects == 1) && (size($selectComp)==0)) {
			$copyCmd = ( "polyCopyUV -uvSetNameInput \\\"" 
					+ $inputUVset + "\\\" -uvSetName \\\""
					+ $toUVset + "\\\" ");

			if ($createNewSet) {
				$copyCmd += "-createNewMap 1";
			}
					
			$copyCmd = ("polyPerformAction \"" + $copyCmd + "\" \"f\"  0;");
		} else if( $numObjects > 1 ) {
			$copyCmd += ($selection[0] + "; ");
		}

		// if selection was > 1 object and really only
		// 1 object is being used, then print a warning
		if( $numObjects > 1 ) {
			warning("Copy UVs to UV Set only worked on the first selected object: "
			+  $selectObj[0] );
		}

		eval( $copyCmd );

		// Need to somehow test that the copy was successful
		// Return 1 to say that something was done
		return 1;

	} else {
		error("No object selected.  Copy UVs to UV Set works on polygonal faces.  Select either a polygonal object or faces of a single polygonal object");
	}
	
	// Return 0 to say that nothing was done;
	return 0;
}
