Home Coding, Scripting, Shaders

Persp / Orthographic camera toggle script for Maya

r3Dmond
polycounter lvl 6
Offline / Send Message
r3Dmond polycounter lvl 6
Hello everyone!

I was looking for a way to toggle the default camera from perspective to orthogonal view (not locked), similarly to what you would do in Zbrush or 3DS Max (which is very handy for modeling in my opinion).

I saw a copule of posts here in the forum but none of the suggestions ticked all the boxes for me so I came up with a little script and I thought it could turn out to be usefull to other people as well, so here it is:

///////////////////////////// Orthogonal camera toggle /////////////////////////////
// Script by Giacomo Colivicchi (AKA R3D)
//
// The script enables orthographic mode for the default persp camera
// and in order to avoid camera roll (which will happen if you pass your point
// of focus' zenith) locks its z axis.
//
// Optionally you can delete the double slash before "FrameSelectedWithoutChildren"
// to enable framing on the currently selected item
//
// Enjoy! :D


$cameraState = `camera -q -o persp`;

if ($cameraState < 1) {
	setAttr "perspShape.orthographic" 1;			//enables orthographic mode
	setAttr -lock true "persp.rz";				//locks the z axis to avoid camera roll
	tumbleCtx -e -orthoLock false tumbleContext;		//enables tumbling in orthographic mode
	tumbleCtx -e -autoOrthoConstrain false tumbleContext;
//	FrameSelectedWithoutChildren;				//optional: remove double slash to frame selected
} else {
	setAttr "perspShape.orthographic" 0;			//disables orthographic mode
	setAttr -lock false "persp.rz";				//unlocks the z axis
	tumbleCtx -e -orthoLock true tumbleContext;		//reset tumbling options to default behaviour
	tumbleCtx -e -autoOrthoConstrain true tumbleContext;
//	FrameSelectedWithoutChildren;				//optional: remove double slash to frame selected
}

The script is super basic (I'm new to scripting) but it gets the job done.
The annoying thing with Maya's ortho view (as already pointed out in this post: https://bit.ly/2mQ95AA) is that if you pass your model's zenith the camera looses it's orientation. To be more precise the Z axis will start rotating as well.

To counter this issue the script locks the camera's z axis.

Enjoy! :)


Replies

  • Leksey
    Options
    Offline / Send Message
    Leksey polycounter lvl 6
    Use Alp + P is default hotkey. Maya 2018 work fine for me, 
  • r3Dmond
    Options
    Offline / Send Message
    r3Dmond polycounter lvl 6
    Leksey said:
    Use Alp + P is default hotkey. Maya 2018 work fine for me, 
    Yes, that's true but ALT+P switches to locked ortographic while my script allows for free ortographic mode. Anyawy, it's just personal preference. I don't really see the use in a locked ortographic view.
  • gavku
    Options
    Offline / Send Message
    gavku polycounter lvl 18
    Sweet I will def give this a crack. Cheers.
  • r3Dmond
    Options
    Offline / Send Message
    r3Dmond polycounter lvl 6
    Hope you are enjoying the script, let me know if you find anything wrong with it and I'll see what I can do about it.
  • Dave Dind
    Options
    Offline / Send Message
    Dave Dind polycounter lvl 11
    This is great! I once set up a similar script at a job, didn't end up needing to use it much (job didn't really involve modeling), moved on from said job, and totally forgot about it! Really happy to have found this, and it works like a charm (or like rather like Max)!

    Thanks very much!
  • mingra2005
    Options
    Offline / Send Message
    Hi, Would love to try your script but having trouble installing it. I used to use an older perp to ortho script called "views" until 2018 didnt  allow it to work again. I'm just confused by your ////comments/// and <br> not sure what the exact script needs to be. Maya MEL keeps telling me bad syntax. any chance of sending me the script with no extra text? Or maybe you have a video showing installation? I'm on Maya 2018- thanks for all your help in advance! Michael
  • Alex_J
    Options
    Online / Send Message
    Alex_J grand marshal polycounter
    Hi, Would love to try your script but having trouble installing it...
    This seems to work: 
    ///////////////////////////// Orthogonal camera toggle /////////////////////////////
    // Script by Giacomo Colivicchi (AKA R3D)
    //
    // The script enables orthographic mode for the default persp camera
    // and in order to avoid camera roll (which will happen if you pass your point
    // of focus' zenith) locks its z axis.
    //
    // Optionally you can delete the double slash before "FrameSelectedWithoutChildren"
    // to enable framing on the currently selected item
    //
    // Enjoy! :D
    
    // Query the orthographic state of the camera
    int $cameraState = `getAttr "perspShape.orthographic"`;
    
    if ($cameraState == 0) {
        setAttr "perspShape.orthographic" 1;            // Enables orthographic mode
        setAttr -lock true "persp.rotateZ";             // Locks the z axis to avoid camera roll
        tumbleCtx -e -orthoLock false `currentCtx`;     // Enables tumbling in orthographic mode
        tumbleCtx -e -autoOrthoConstrain false `currentCtx`;
        // FrameSelectedWithoutChildren;                // Optional: remove double slash to frame selected
    } else {
        setAttr "perspShape.orthographic" 0;            // Disables orthographic mode
        setAttr -lock false "persp.rotateZ";            // Unlocks the z axis
        tumbleCtx -e -orthoLock true `currentCtx`;      // Reset tumbling options to default behavior
        tumbleCtx -e -autoOrthoConstrain true `currentCtx`;
        // FrameSelectedWithoutChildren;                // Optional: remove double slash to frame selected
    }
    If you want to know how I fixed it... I didn't. ChatGPT did. It's pretty good at fixing up old mel code that no longer works. I've been able to revive a few old plugins that throw errors in newer maya versions despite not really knowing mel. 
    It helps to know a little bit about programming cause sometimes you got to coax chatgpt through things in a guided way, but it's pretty handy for this sort of thing.

  • r3Dmond
    Options
    Offline / Send Message
    r3Dmond polycounter lvl 6
    I'm not quite sure what happened with the code I had originally posted here. Maybe something changed in the way Polycount displays code.. I honestly don't know why it got so messed up as I remember it showing correctly when I posted it.

    Here is a revised version. This should work correctly:

    // Orthogonal Camera Toggle --------------------------------------------------------------------------------------------------------------------------
    //
    //    Script by Giacomo Colivicchi (AKA R3D)
    //
    //
    //    The script toggles the default persp's camera ortho/persp mode and enables tumbling so that you can not only move, but also freely rotate in
    //    orthogtraphic mode. To avoid unwanted tilting around the Z axis (Camera Roll), which happens if you pass your focus point's zenith, the Z axis
    //    is locked temporarily.
    //    
    //    Run the script once again to restore the camera to it's default perspective mode.
    //
    //
    //    Enjoy! :D
    //
    //
    
    
    global proc j_togglePerspMode()
    // Description:
    //    Toggles orthographic mode for the defauld "persp" camera.
    //
    // Arguments:
    //    None.
    //
    // Returns:
    //    None.
    //
    //
    {
        int $cameraState = `camera -query -orthographic persp`;
        
        if ($cameraState < 1){
            setAttr "perspShape.orthographic" 1;
            setAttr -lock true "persp.rz";
            tumbleCtx -e -orthoLock false tumbleContext;
            tumbleCtx -e -autoOrthoConstrain false tumbleContext;        
        } else {  
            setAttr "perspShape.orthographic" 0;
            setAttr -lock false "persp.rz";
            tumbleCtx -e -orthoLock true tumbleContext;
            tumbleCtx -e -autoOrthoConstrain true tumbleContext;    
        }
    }
    j_togglePerspMode;


    Have a nice day!
  • r3Dmond
    Options
    Offline / Send Message
    r3Dmond polycounter lvl 6
    I also re-edited the code portion of the original post to show correctly as it was unreadable as it was =)
Sign In or Register to comment.