|
Camera Switching with MEL In this tutorial we are going to devise a way to toggle through the available cameras, making the current panel the camera view, just by pressing a hotkey. I will be using this scene to test our script Typically the "0" (zero) key is not associated as a hotkey so we are going to create a script that is invoked every time the "0" key is pressed. To perform such an action manually in Maya we do the following; 1) Select a panel While this is fairly simple, it is time consuming when you are doing a lot of camera work and especially when you are relying on visual cues to select your camera. So how does Maya perform the above tasks in MEL? Lets have a look at the MEL script that the script editor spits out when we perform the above actions. select -r camera1 ; Hmmm... thats not very impressive is it? Yet you will find that this is all Maya that is prepared to share with you for this particular action. So out come the thumb screws! Lets make Maya spill the beans. In the script editor select: Script > Echo all commands Now lets see what Maya yields select -r camera1
; This is the absolute explicit Maya feedback, but most of what we see is useless information (which is half the reason why we normally have the "Echo all commands" turned off). But at least we can see here the pertinent command: lookThroughSelected modelPanel4; so a simplified version of the above would be select -r camera1 ; Easy! Luckily for us there is a MEL command that will take us most of the way here. It is called the lookThru command. We can use it to both query Maya as to which camera we are currently looking through, and secondly to make the current panel look through a specified camera. (1) To find out which camera we are currently looking through we can execute lookThru in query mode lookThru -q and we can store the camera name with $currentCamera = `lookThru -q`; (2) Next we need to find out a list of the cameras in the scene and where our active camera lies on this list and then choose the next camera in the list. To do this we can use the listCameras command. The listCameras -p will list only perspective cameras, and -o will list orthographic cameras only. string $allCameras[]; We now create a marker variable that records the position on the list after the active camera. int $nextCamera; With this we can now run through the list of cameras and find out where the active camera lies. for($pos=0;$pos<size($allCameras);$pos++)
The last check is to make sure we don't run off the end of the list, if we do, then we jump back to the top of the list. if($nextCamera>=size($allCameras))
$nextCamera = 0; Finally we can change the camera to look through the new camera with the simple command lookThru $allCameras[$nextCamera]; So now our entire code is thus; $currentCamera = `lookThru -q`; If we wanted to toggle backwards we would make the following modifications; $currentCamera = `lookThru -q`; Setting up Hotkeys
So thats it. I hope this makes using maya just that little bit easier.
| ||||