Maya crash on executing simple mel script

Hey guys, relatively new to the scripting side of things but really starting to enjoy learning it, however i’m encountering a bit of a brick wall right now with a script i’m working on.

Basically iv created a simple mel script which switches between perspective and orthographic views based on the direction the perspective camera faces, and it was all working perfectly while i was writing it. But then i close down maya for the weekend, and when i try to run my script again today, it just straight up crashes maya every time with no error messages or anything. This alone seems quite odd to me, but as i was trying to figure out what was going on, i realised if i run little bits of my script through the script editor first, and then run the whole thing, it works again, which has only confused me more.

Was wondering if anyone could shed some light on possible reasons why this might be happening? My only thought is that for some reason its not recognising some of the procedures in my script on start up, and instead of throwing an error its just crashing, but if thats true i’ve no idea how to deal with it…

Cheers in advance! :):

Without seeing your script, it’s hard to say. I would try to narrow down exactly where it is crashing. Start by running half of the code through the script editor. If it crashes, then you know the crash is in that part, so next time try running a smaller bit of the code. If it doesn’t crash, then restart Maya and try running a larger chunk of the script. Keep trying smaller and larger chunks until you figure out where the crash is.

Hey RFlannery, thanks for the reply.

I don’t think i explained properly what i’d already tried, so my bad there, but i’v already gone through the process you’ve described and come up with nothing. I got to the last line, and if i delete it and retype exactly as it was, then the script works when i run it. It seems to me like the act of editing it in the script editor does something which allows it to run, but once i save it out as a mel file and try to run the procedure either in the script editor or through a hotkey/shelf item in a freshly opened maya, it just crashes. Even simply adding a new comment to the script allows it to run in that instance of maya, but on a new instance its back to crashing.

Heres the script if it helps:


//compare camera name strings, matching string = 0, otherwise 1 or -1
//does cam exist boolean set to true on match, returns int value
proc int doesCamExist (string $camName)
{
    string $cameraList[] = `listCameras`;
    int $doesCamExist = false;
    for ($camera in $cameraList)
    {
        $compare = `strcmp $camName $camera`;
        if ($compare == 0)
        {
            $doesCamExist = true;
            break;
        }
    }
    return $doesCamExist;
}

//check if an orthographic camera already exists for desired view
//if true, use existing caera, if false create new camera
proc camCheck (string $camStruct[])
{
    if (size($camStruct) > 1)
    {
        string $camName = $camStruct[0];
        string $axis = $camStruct[1]; 
        int $camCheck = doesCamExist($camName);
        if ($camCheck == 1)
        {
            lookThru $camName;
            print ($camName + " camera exists - using existing camera");
        }        
        else if ($camCheck == 0)
        {
            camera;
            rename "camera1" $camName;
            camera -e -o 1 -ncp 0.001 -fcp 10000 $camName;
            lookThru $camName;
            viewAlongAxis modelPanel4 $axis;
            modelEditor -e -twoSidedLighting true modelPanel4;
            print ($camName + " camera doesnt exist - creating new camera");
        }
    }
}

//if rotations go beyond 360, fix with modulus
//if rotations are negative, fix by adding 360
proc float fixRotation (float $Rot)
{
    $fixedRot = $Rot % 360;
    if ($fixedRot < 0)
    {
       $fixedRot += 360; 
    }
    return $fixedRot;
}

//main function to switch between perspective and orthographic views based on camera orientation
global proc perspToOrtho ()
{
    $currentCam = `lookThru -q`;
    $camRot = `camera -q -rot persp`;
    $xValue = fixRotation($camRot[0]);
    $yValue = fixRotation($camRot[1]);   

    //check if camera is perspective, if not switch to perspective 
    if ($currentCam != "persp")
    {
        lookThru persp;
    }
      
    //check if top camera
    else if ($xValue <= 315 && $xValue >= 225)
    {
        camCheck({"top", "-Y"});
        setAttr "top.translateX" 0;
        setAttr "top.translateY" 5;
        setAttr "top.translateZ" 0;
    }
  
    //check if bottom camera
    else if ($xValue >= 45 && $xValue <= 135)
    {
        camCheck({"bottom", "Y"});
        setAttr "bottom.translateX" 0;
        setAttr "bottom.translateY" -5;
        setAttr "bottom.translateZ" 0;
    }
    
    //check if front camera
    else if ( ($yValue <= 45 && $yValue >= 0) || ($yValue >= 315 && $yValue <= 360) )
    {
        camCheck({"front", "-Z"});
        setAttr "front.translateX" 0;
        setAttr "front.translateY" 0;
        setAttr "front.translateZ" 5;
    }
    
    //check if back camera
    else if ($yValue >= 135 && $yValue <= 225)
    {
        camCheck({"back", "Z"});
        setAttr "back.translateX" 0;
        setAttr "back.translateY" 0;
        setAttr "back.translateZ" -5;
    }
    
    //check if left camera
    else if ($yValue > 225 && $yValue < 315) 
    {
        camCheck({"left", "X"});
        setAttr "left.translateX" -5;
        setAttr "left.translateY" 0;
        setAttr "left.translateZ" 0;
    }
    
    //check if right camera
    else if ($yValue > 45 && $yValue < 135)
    {
        camCheck({"right", "-X"});
        setAttr "right.translateX" 5;
        setAttr "right.translateY" 0;
        setAttr "right.translateZ" 0;
    }
   
    else
    {
        print "error: no valid camera choice from current values";
    }

}

I ran your code. I didn’t get a crash, but I was able to get a stack trace (depending on the camera angle I started with):

Cannot find procedure "viewAlongAxis".
Start of trace: (file: U:/maya/scripts/perspToOrtho.mel, line 39).
viewAlongAxis (file: U:/maya/scripts/perspToOrtho.mel, line 39).
camCheck (file: U:/maya/scripts/perspToOrtho.mel, line 120).
perspToOrtho (command window: line 1).

Oh, I should have asked: What version of Maya are you using?

Im using Maya LT 2017 (for now anyway, lack of python is kinda bugging me).

Could you explain how you go about getting that stack trace?
viewAlongAxis i got from the viewport view menu: view > view along axis > (choice of axis), so not really sure why that wouldnt be recognised?

Okay, I was using Maya 2016. It looks like “viewAlongAxis” was not introduced until Maya 2016.5.
Anyways, I tried using Maya 2017 and I don’t get any errors or crashes. At this point you might try resetting your preferences? That is what I usually do when I get errors that make no sense.

ah id almost forgotten that little trick, thanks for reminding me. After deleting my preferences it seems to have only somewhat fixed the problem. I can now run the script on start-up through the command line without crashing, however as soon as i bind it to a hotkey and try again, its still crashing, even in a totally new default hotkey setup with that single change.

Ill try playing around with the hotkey stuff and see if theres some workaround i can figure out, but this is just continuing to not make any sense