Maya Python: Reflection Angle form Face Selection

hi all,

i’m trying to calculate the (averaged) reflection angle of a selection of poly faces in relation to the pov. It’s difficult to describe…
I want to place the camera, then i want so select some faces on an object. Next i get a line or something that represents the reflection ray. By lining up a light on that ray i get the reflection of thelight exactly on the poly faces i selected earlier.
Hope it is understandable.

thanks in advance

BTW: I found a mel script that is exactly doing what i am looking for. But i could not convert it to python because i have no idea of vectors. Started python just some week ago…


global proc cShowReflectionRay(){
	if(`window -exists "cShowReflectionRay"`) deleteUI "cShowReflectionRay";
	if(`windowPref -exists "cShowReflectionRay"`) windowPref -remove "cShowReflectionRay";
	window -title "show reflection ray" -width 300 -resizeToFitChildren 1 "cShowReflectionRay";
	string $mainLayout = `rowColumnLayout -numberOfColumns 1 -cw 1 300`;
		separator -style "none" -height 10;
		rowColumnLayout -numberOfColumns 3 -cw 1 10 -cw 2 260 -cw 3 10;
			text -l "";
			string $radioGrp = `radioButtonGrp
				-numberOfRadioButtons 2
				-labelArray2 "average values" "per component"
				-cw 1 129
				-select 1`;			
			text -l "";

	setParent $mainLayout;
		separator -style "none" -height 7;
		rowColumnLayout -numberOfColumns 5 -cw 1 5 -cw 2 97 -cw 3 96 -cw 4 97 -cw 5 5;
			text -l "";
			button -l "show rays" -command ("cShowReflectionRay_doIt (`radioButtonGrp -q -select \"" + $radioGrp + "\"` - 1);");
			button -l "ok" -command ("cShowReflectionRay_doIt (`radioButtonGrp -q -select \"" + $radioGrp + "\"` - 1)");
			button -l "cancel" -command ("deleteUI \"cShowReflectionRay\"");
	setParent $mainLayout;
		separator -style "none" -height 5;
	showWindow "cShowReflectionRay";
};

global proc cShowReflectionRay_doIt(int $showRayPerComponent)
{
	string $sel[] = `ls -fl -sl`;
	string $activePanel = `getPanel -withFocus`;
	string $currentCam = `modelPanel -q -camera $activePanel`;
	string $camTransform[] = {};
	if(`nodeType $currentCam` == "camera")
	{
		$camTransform = `listRelatives -parent -f $currentCam`;
	}
	else $camTransform = {$currentCam};
	vector $camPos = `xform -q -ws -t $camTransform[0]`;

	vector $pList[] = {};
	vector $nList[] = {};
	for($i=0;$i<`size $sel`;$i++)
	{
		string $shape[] = `ls -long -o $sel[0]`;
		string $component[] = {$sel[$i]};

		vector $p, $n;
		if(`nodeType $shape[0]` == "nurbsSurface")
		{
			tokenize $component[0] "[]" $component;
			float $uv[] = {(float($component[1])),(float($component[2]))};
			$p = `pointOnSurface -u $uv[0] -v $uv[1] -p $shape[0]`;
			$n = `pointOnSurface -u $uv[0] -v $uv[1] -nn $shape[0]`;
		}
		else if(`nodeType $shape[0]` == "mesh")
		{
			string $fv[] = `polyListComponentConversion -ff -tvf $component[0]`;
			$fv = `filterExpand -sm 70 -ex true $fv`;
			if(`size $fv` < 3){
				print($component[0] + ": invalid face, skipped
");
				continue;
			};
			float $tmp[] = {};
			float $averagePos[] = {};
			for($k=0;$k<`size $fv`;$k++){
				string $vertex[] = `polyListComponentConversion -fvf -tv $fv[$k]`;
				float $pos[] = `xform -q -ws -t $vertex[0]`;
				$tmp[`size $tmp`] = $pos[0];
				$tmp[`size $tmp`] = $pos[1];
				$tmp[`size $tmp`] = $pos[2];

			};
			vector $v1 = <<($tmp[3] - $tmp[0]),($tmp[4] - $tmp[1]),($tmp[5] - $tmp[2])>>;
			vector $v2 = <<($tmp[6] - $tmp[0]),($tmp[7] - $tmp[1]),($tmp[8] - $tmp[2])>>;

			float $averagePos[] = {};
			for($k=0;$k<`size $fv`;$k++){
				$averagePos[0] = $averagePos[0] + $tmp[$k * 3];
				$averagePos[1] = $averagePos[1] + $tmp[$k * 3 + 1];
				$averagePos[2] = $averagePos[2] + $tmp[$k * 3 + 2];
			};
			$averagePos = {($averagePos[0] / `size $fv`),($averagePos[1] / `size $fv`),($averagePos[2] / `size $fv`)};
			$p = <<$averagePos[0],$averagePos[1],$averagePos[2]>>;
			$n = unit(`cross $v1 $v2`);
		}
		else
			return;

		if($showRayPerComponent == 1){
			vector $camToPOS = $camPos - $p;
			float $length_1 = mag($camToPOS);
			float $length_2 = sind(90 - `rad_to_deg (angle($n, $camToPOS))`) * $length_1;
			vector $mPoint = $p + $n * $length_2;
			vector $camToMPoint = $mPoint - $camPos;
			vector $finalPoint = $mPoint + $camToMPoint;
			string $curve = `curve -d 1 -p ($p.x) ($p.y) ($p.z) -p ($finalPoint.x) ($finalPoint.y) ($finalPoint.z)`;
			xform -piv ($p.x) ($p.y) ($p.z) $curve;
		}else{
			$pList[`size $pList`] = $p;
			$nList[`size $nList`] = $n;
		};
	};
	if($showRayPerComponent == 0){
		float $p[], $n[];
		int $numComponents = `size $pList`;
		for($i=0;$i<`size $pList`;$i++){
			vector $tmp = $pList[$i];
			$p[0] = $p[0] + ($tmp.x) / $numComponents; 
			$p[1] = $p[1] + ($tmp.y) / $numComponents; 
			$p[2] = $p[2] + ($tmp.z) / $numComponents; 
			$tmp = $nList[$i];
			$n[0] = $n[0] + ($tmp.x) / $numComponents; 
			$n[1] = $n[1] + ($tmp.y) / $numComponents; 
			$n[2] = $n[2] + ($tmp.z) / $numComponents; 
		};
		vector $camToPOS = $camPos - <<$p[0],$p[1],$p[2]>>;
		float $length_1 = mag($camToPOS);
		float $length_2 = sind(90 - `rad_to_deg (angle(&lt;&lt;$n[0],$n[1],$n[2]&gt;&gt;, $camToPOS))`) * $length_1;
		vector $mPoint = <<$p[0],$p[1],$p[2]>> + <<$n[0],$n[1],$n[2]>> * $length_2;
		vector $camToMPoint = $mPoint - $camPos;
		vector $finalPoint = $mPoint + $camToMPoint;
		string $curve = `curve -d 1 -p $p[0] $p[1] $p[2] -p ($finalPoint.x) ($finalPoint.y) ($finalPoint.z)`;
		xform -piv $p[0] $p[1] $p[2] $curve;
	};
};

basics here: Maya Python: How to calculate the Reflection Angle/Vector of an given face to camera? - Stack Overflow