Command to Toggle Orthographic Attribute

Hi, I'd like to create an hotkey that toggles between an Orthographic persp Camera to a NonOrthographic.

When I do this manually from the Camera Attribute Editor the code recorded from the Sript Editor is:

setAttr "perspShape.orthographic" 0;
cameraCheckOrthographic "perspShape";
attrFieldSliderGrp -e -en false attrFieldSliderGrp11;

and when disabling the orthographic value:

setAttr "perspShape.orthographic" 0;
cameraCheckOrthographic "perspShape";
attrFieldSliderGrp -e -en true attrFieldSliderGrp11;

Now how an I make a Command in the HotKey Editor that when I press the assigned hotkey toggles the camera orthographic attribute to 0 or 1?

with my basic knowledge of Maya scripting I tried those two:

MEL(?):

$val = getAttr (perspShape.orthographic);
if ($val == 0) {$val = 1;}
else {$val = 0;} setAttr "perspShape.orthographic" $val;
cameraCheckOrthographic "perspShape";
attrFieldSliderGrp -e -en false attrFieldSliderGrp11;

Python(?):

val = getAttr "perspShape.orthographic";
if val == 0:   setAttr "perspShape.orthographic" 0;
else:   setAttr "perspShape.orthographic" 0;
cameraCheckOrthographic "perspShape";
attrFieldSliderGrp -e -en false attrFieldSliderGrp11;

But obviously they give mistakes.

I definitely want to learn scripting the proper way, but the topic is so vast that I would like to be adressed on where to start learining to solve this kind of problems.. any book or on-line source you would advise?

Even if you give the solution to my problem I think can be a very formative information to begin with.

I'd rather study Python because could be andy also in other software/situations.. but if you think is the wrong choice, pleae feel free to advise me otherwise.

Many Thanks,

Sergio 

//Persp Cam Ortho Toggle
int $val = getAttr "perspShape.orthographic";
if ($val == 0){$val = 1;}
else {$val = 0;}
setAttr "perspShape.orthographic" $val;

Tha'ts all you need to do to toggle the persp cams ortho view.

Keep in mind that since the perspective camera's shape has been written directly into the code (MEL), it will only work if the camera's shape node is in fact called "perspShape" and not somehting like "perspShape1" etc.

There are a few things you can do from here. You can highlight this code in the script editor and just drag it to you shelf to create a button. Or you can make this a global procedure and map the procedure to a hot key. Personally I like the shelf button method because while you may like this feature for this project, your next project probably won't require it usage. Which means you'll now have a hotkey mapped that you will probably forget about vs haveing a visual button in you shelf.

A global procedure would look something like the below. It could be saved as a .mel script and called by executing DD_TogglePerspOrtho():

global proc DD_TogglePerspOrtho(){

    //Persp Cam Ortho Toggle
    int $val = getAttr "perspShape.orthographic";
    if ($val == 0){$val = 1;}
    else {$val = 0;}
    setAttr "perspShape.orthographic" $val;

}