// Copyright (C) 1997-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 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.

//
//  Alias Script File
//  MODIFY THIS AT YOUR OWN RISK
//
//  Creation Date:  2000
//
//<doc>
//<name setStampDensity>
//<owner "Alias Unsupported">
//
//<synopsis>
//	setStampDensity (float $stampDensity)
//
//<returns>
//  None.
//
//<description>
//  Sets the stamp density for selected paint effects strokes while 
//	automatically adjusting the brush transparency so that the
//  opacity of the stroke stays constant. This will only have
//  an effect for paint effects brushes with a "paint" brushtype.
//  Higher stamp densities can look smoother, while lower values
//  will render faster.
//
//<flags>
//	float $stampDensity The new brush stamp density value.
//
//<examples>
//  setStampDensity 1.5;
//
//</doc>


global proc setStampDensity( float $stampDensity )
{
	string $selected[] = getSelectedBrushes(); 
	string $sel;
	float  $oldStampDensity, $expn;
	float  $transp1R, $transp1G, $transp1B;
	float  $transp2R, $transp2G, $transp2B;
	float  $incand, $oldT, $newT;
	string $param;

	if( size( $selected ) <= 0  )
	{
		warning( (uiRes("m_setStampDensity.kNoBrushesSelected")));
		return;
	}	
	for( $sel in $selected )
	{
		$oldStampDensity = `getAttr ($sel + ".stampDensity")`;
		$transp1R = `getAttr ($sel + ".transparency1R")`;
		$transp1G = `getAttr ($sel + ".transparency1G")`;
		$transp1B = `getAttr ($sel + ".transparency1B")`;
		$transp2R = `getAttr ($sel + ".transparency2R")`;
		$transp2G = `getAttr ($sel + ".transparency2G")`;
		$transp2B = `getAttr ($sel + ".transparency2B")`;
		if( $oldStampDensity > 0 && $stampDensity > 0 )
		{
			$stampRatio  = 	$oldStampDensity/$stampDensity;
			float $oldT1R = $transp1R;
			float $oldT1G = $transp1G;
			float $oldT1B = $transp1B;
			float $oldT2R = $transp2R;
			float $oldT2G = $transp2G;
			float $oldT2B = $transp2B;
			$transp1R  = pow( $transp1R, $stampRatio );
			$transp1G  = pow( $transp1G, $stampRatio );
			$transp1B  = pow( $transp1B, $stampRatio );
			$transp2R  = pow( $transp2R, $stampRatio );
			$transp2G  = pow( $transp2G, $stampRatio );
			$transp2B  = pow( $transp2B, $stampRatio );
			$oldT = $oldT1R;
			$newT = $transp1R;
			$param = ($sel + ".incandescence1R");
			if( $oldT != 1 )
			{
				$incand = getAttr( $param ) * (1-$newT)/(1-$oldT);
				setAttr $param $incand; 
			}
			$oldT = $oldT1G;
			$newT = $transp1G;
			$param = ($sel + ".incandescence1G");
			if( $oldT != 1 )
			{
				$incand = getAttr( $param ) * (1-$newT)/(1-$oldT);
				setAttr $param $incand; 
			}
			$oldT = $oldT1B;
			$newT = $transp1B;
			$param = ($sel + ".incandescence1B");
			if( $oldT != 1 )
			{
				$incand = getAttr( $param ) * (1-$newT)/(1-$oldT);
				setAttr $param $incand; 
			}

			$oldT = $oldT2R;
			$newT = $transp2R;
			$param = ($sel + ".incandescence2R");
			if( $oldT != 1 )
			{
				$incand = getAttr( $param ) * (1-$newT)/(1-$oldT);
				setAttr $param $incand; 
			}
			$oldT = $oldT2G;
			$newT = $transp2G;
			$param = ($sel + ".incandescence2G");
			if( $oldT != 1 )
			{
				$incand = getAttr( $param ) * (1-$newT)/(1-$oldT);
				setAttr $param $incand; 
			}
			$oldT = $oldT2B;
			$newT = $transp2B;
			$param = ($sel + ".incandescence2B");
			if( $oldT != 1 )
			{
				$incand = getAttr( $param ) * (1-$newT)/(1-$oldT);
				setAttr $param $incand; 
			}

		}
		setAttr ($sel + ".stampDensity") $stampDensity;
		setAttr ($sel + ".transparency1R") $transp1R;
		setAttr ($sel + ".transparency1G") $transp1G;
		setAttr ($sel + ".transparency1B") $transp1B;
		setAttr ($sel + ".transparency2R") $transp2R;
		setAttr ($sel + ".transparency2G") $transp2G;
		setAttr ($sel + ".transparency2B") $transp2B;
	}
}
