MEL Script for hardening edges where normals between two faces exceed a threshold

Hi All,

I created a MEL script to hardening edges where the angle between adjacent faces exceed an angle. The idea is that you create a mesh, soften all the edges and then run the script that will harden edges the said edges.

An example use case - a barrel with slightly curved surfaces. It will harden the edges along the rim while leaving the slightly curved surfaces alone.





proc vector gcToNormal( string $str )
{
	string $data] = stringToStringArray($str, " ");
	int $c = size($data);
	float $v];
	$v[0] = $data$c-3];
	$v[1] = $data$c-2];
	$v[2] = $data$c-1];
	normalize($v);
	vector $n = << $v[0], $v[1], $v[2] >>;
	return $n;
}
	
proc gcHardenEdges()
{
	global string $gMainProgressBar;
	
	float $angleThreshold = (float)(`textField -q -tx gcAngleThreshold`) * 3.14 / 180.0;
	float $cosThreshold = `cos $angleThreshold`;

	string $edges] = `ls -selection`;
	$edges = `filterExpand -sm 32 -ex true $edges`;
	int $count = size($edges);
	int $i;

	if($count > 0)
	{
		progressBar -edit
		    -beginProgress
		    -isInterruptable true
		    -status "Hardening ..."
		    -maxValue $count
		    $gMainProgressBar;
		for($i=0;$i<$count;$i++)
		{
			string $type = `objectType $edges$i]`;
			//print $type;
			string $adjFaces] = `polyListComponentConversion -fe -tf $edges$i]`;
			$adjFaces = `filterExpand -sm 34 -ex true $adjFaces`;
			if(size($adjFaces) >= 2)
			{
				string $normal_0_str] = `polyInfo -fn $adjFaces[0]`;
				vector $n0 = gcToNormal($normal_0_str[0]);

				string $normal_1_str] = `polyInfo -fn $adjFaces[1]`;
				vector $n1 = gcToNormal($normal_1_str[0]);
				
				float $dp = abs(dotProduct($n0, $n1, 0));
				if($dp < $cosThreshold)
				{
					polySoftEdge -a 0 -ch 1 $edges$i];
				}	
			}
			if(`progressBar -query -isCancelled $gMainProgressBar`)
				break;

			progressBar -edit
				-step 1 $gMainProgressBar;
		
		}
		progressBar -edit
			-endProgress
			$gMainProgressBar;		
	}
}

proc gcCreateWindow()
{
    if (`window -q -ex gcHardenSelectedEdgesWindow`)
   	{
    	showWindow gcHardenSelectedEdgesWindow ;
    	return ;
   	}

    window -w 200 -h 80 -t ("Harden Selected Edges") gcHardenSelectedEdgesWindow ;
	columnLayout -rowSpacing 10;
	rowLayout -nc 2 -cw2 100 50;
    text -l "threshold angle:" gcTextThresholdLabel ;
    textField -tx "60" -w 50 gcAngleThreshold ;
	setParent ..;
    button -l "Harden" -w 200 -c ("gcHardenEdges();") gcButtonHarden ;

	showWindow gcHardenSelectedEdgesWindow;
}

gcCreateWindow();


Dude thats pretty nice and all… but you know the polySoftEdge node in maya has an angle attribute that does just that?
0 means no edges will be softened, then it gradually ups the threshold till at 180 all edges will be softened.

Edit: checked out your script: you even use the command there with -a 0 … why not just set -a to whatever your angle threshold needs to be and call it a day?
Porbably way faster than checking angle for each edge using mel anyway.

Thanks heaps, never knew that :slight_smile: