Friday, August 24, 2012

Camera Look Through Tip (Python/MEL)

Hi,
I was finding it hard to position a camera and use the perspective view simultaneously so here is a little script that helped me switch between different cameras quickly so that we don't lose the position of our positioned camera and still can move freely in the perspective view.  I can imagine this may also be helpful in modeling and quickly looking through reference camera.  I used this as a hot key for previewing some skinning deformation.


Hope you find it helpful,
Cheers,
Nate


Helpful commands lookThroughModelPanel, selection list (ls), eval (eval,maya.mel), and a hierarchy command (listRelatives).

Example Before and After:


Python and MEL side by side

MEL
/**look through selected camera
@pre camera selected on scene
*/
global proc na_lookThroughSelectedCamera()
{
    string $panel = "modelPanel4";
    string $sel[] = `ls -sl`;
    
    //if camera is only selection look through it in chosen panel
    if(size($sel) == 1)
    {
        if( size( `listRelatives -children -type camera $sel[0]`) == 1 )
            { eval( "lookThroughModelPanel "+$sel[0]+" "+$panel); }
    }
    
}

PYTHON
import maya.cmds as cmds
import maya.mel as mel


##look through selected camera
#
#@pre camera selected on scene
#
#
def lookThroughSelectedCamera():
    panel = "modelPanel4"  
    sel = cmds.ls(selection=True)

    #if camera is only selection look through it in chosen panel
    if( len(sel) == 1 ):
        if( len(cmds.listRelatives(children = True, type = "camera")) == 1 ):
            mel.eval("lookThroughModelPanel "+sel[0]+" "+panel)