Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

My Mel camera swap script is no longer workin in Maya 2024

My Mel camera swap script is no longer workin in Maya 2024

klawchi
Enthusiast Enthusiast
548 Views
1 Reply
Message 1 of 2

My Mel camera swap script is no longer workin in Maya 2024

klawchi
Enthusiast
Enthusiast

Hi,

I am using this camera script to switch cameras but it's no longer working and cause the Maya to crash!

Any idea why?

Thanks

 

global proc switchCam(int $snap)
{
	int $check = 0;
	string $switchCam;
	string $switchCamP = "switchCam_Persp1";
	string $switchCamO = "switchCam_Ortho1";

	if(`objExists $switchCamP`)
	{
		$switchCam = $switchCamP;
		$check = 1;
	}
	if(`objExists $switchCamO`)
	{
		$switchCam = $switchCamO;
		$check = 1;
	}

	if($check==0)
	{
		string $sel[] = `ls -sl`;
		$switchCam = $switchCamP;
		camera -centerOfInterest 5 -focalLength 35 -lensSqueezeRatio 1 -cameraScale 1 -horizontalFilmAperture 1.4173 -horizontalFilmOffset 0 -verticalFilmAperture 0.9449 -verticalFilmOffset 0 -filmFit Horizontal -overscan 1 -motionBlur 0 -shutterAngle 144 -nearClipPlane 1 -farClipPlane 100000 -orthographic 0 -orthographicWidth 30 -n $switchCam;
		hide;
		select $sel;
	}


	string $currentCam = `lookThru -q`;
	float $camPos[] = `xform -q -ws -m $currentCam`;
	xform -m $camPos[0] $camPos[1] $camPos[2] $camPos[3]
			$camPos[4] $camPos[5] $camPos[6] $camPos[7]
			$camPos[8] $camPos[9] $camPos[10] $camPos[11]
			$camPos[12] $camPos[13] $camPos[14] $camPos[15]
	 $switchCam;
	
	int $ortho = `getAttr ($currentCam + ".orthographic")`;

	float $orthow = `getAttr ($currentCam + ".orthographicWidth")`;
	float $coi = `getAttr ($currentCam + ".centerOfInterest")`;
	float $happ = `getAttr ($currentCam + ".horizontalFilmAperture")`;
	float $focL = `getAttr ($currentCam + ".focalLength")`;
	setAttr ($switchCam + ".orthographicWidth") $orthow;
	setAttr ($switchCam + ".centerOfInterest") $coi;
	setAttr ($switchCam + ".focalLength") $focL;
//	setAttr ($switchCam + ".horizontalFilmAperture") $happ;

	$orthow  *= 25.4; // convert inches to mm
	$coi  *= 25.4; // convert inches to mm
	$happ  *= 25.4; // convert inches to mm

	lookThru $switchCam;
		
	if($ortho)
	{
		rename $switchCam $switchCamP;

	    // make persp
		setAttr ($switchCamP + ".orthographic") 0;
	}
	else
	{
		rename $switchCam $switchCamO;

		float $wci[] = `camera -q -wci $switchCamO`;
		vector $wcoi = <<$wci[0],$wci[1],$wci[2]>>;
		float $wt[] = `xform -q -ws -t $switchCamO`;
		vector $aim = <<$wt[0],$wt[1],$wt[2]>> - $wcoi;
		vector $naim = unit($aim);

		// calculate ortho width
		setAttr ($switchCamO + ".orthographic") 1;
		$orthow = (($coi*$happ)/$focL) / 25.4; // convert mm to cm
		setAttr ($switchCamO + ".orthographicWidth") $orthow;

		if($snap)
		{
			// rotate camera onto nearest axis
			vector $new = <<1,0,0>>;
			float $minAngle = `angle $naim <<1,0,0>>`;

			float $rnx = `angle $naim <<-1,0,0>>`;
			if ($rnx < $minAngle)
			{
	            $minAngle = $rnx;
				$new = <<-1,0,0>>;
	        }
			float $ry = `angle $naim <<0,1,0>>`;
			if ($ry < $minAngle)
			{
	            $minAngle = $ry;
				$new = <<0,1,0>>;
	        }	
			float $rny = `angle $naim <<0,-1,0>>`;
			if ($rny < $minAngle)
			{
	            $minAngle = $rny;
				$new = <<0,-1,0>>;
	        }
			float $rz = `angle $naim <<0,0,1>>`;
			if ($rz < $minAngle)
			{
	            $minAngle = $rz;
				$new = <<0,0,1>>;	
	        }
			float $rnz = `angle $naim <<0,0,-1>>`;
			if ($rnz < $minAngle)
			{
	            $minAngle = $rnz;
				$new = <<0,0,-1>>;
	        }

			// find nearest up vector
			float $wup[] = `camera -q -wup $switchCamO`;
			vector $wupv = unit(<<$wup[0],$wup[1],$wup[2]>>);
	
			vector $up = <<1,0,0>>;
			float $minAngle = `angle $wupv <<1,0,0>>`;
	        
			float $upnx = `angle $wupv <<-1,0,0>>`;
			if ($upnx < $minAngle)
			{
	            $minAngle = $upnx;
				$up = <<-1,0,0>>;
	        }
			float $upy = `angle $wupv <<0,1,0>>`;
			if ($upy < $minAngle)
			{
	            $minAngle = $upy;
				$up = <<0,1,0>>;
	        }
			float $upny = `angle $wupv <<0,-1,0>>`;
			if ($upny < $minAngle)
			{
	            $minAngle = $upny;
				$up = <<0,-1,0>>;
	        }
			float $upz = `angle $wupv <<0,0,1>>`;
			if ($upz < $minAngle)
			{
	            $minAngle = $upz;
				$up = <<0,0,1>>;
	        }
			float $upnz = `angle $wupv <<0,0,-1>>`;
			if ($upnz < $minAngle)
			{
	            $minAngle = $upnz;
				$up = <<0,0,-1>>;
	        }
	
			// move cam and set up and aim vectors		
			vector $target = (dot($naim,$new)) * $new;
			$target = unit($target) * mag($aim);
			$target = $wcoi + $target;
			
			float $rot[] = `xform -q -ws -ro $switchCamO`;
			
			xform -ws -t ($target.x) ($target.y) ($target.z) $switchCamO;
			camera -e -wci ($wcoi.x) ($wcoi.y) ($wcoi.z) $switchCamO;
			camera -e -wup ($up.x) ($up.y) ($up.z) $switchCamO;
		}		
	}
}

 

549 Views
1 Reply
Reply (1)
Message 2 of 2

brianchapmandesign
Collaborator
Collaborator

I was able to solve it


"Very funny, Scotty. Now beam down my clothes.
0 Likes