Home Coding, Scripting, Shaders

Help with mel script is not working with Maya 2024

polycounter lvl 7
Offline / Send Message
triton polycounter lvl 7
I was using this Mel script to switching between different camera angles for years but when I updated to Maya 2024 it stopped working.

Can you please help me to fix it?

Thank you.



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;
        }      
    }
}

Replies

  • triton
    Offline / Send Message
    triton polycounter lvl 7
    You can use it when you put the script in a .mel file in your maya\2024\prefs\scripts directory.
    And you can launch it with the Mel command: switchCam 1

    The error I am getting:

    // Warning: float $minAngle = `angle $wupv <<1,0,0>>`;
    // Warning: Line 118.45 : Redeclaration of variable "$minAngle" shadows previous declaration at line 80. Previous value will be overwritten by explicit initializer.
Sign In or Register to comment.