MEL How-To #41

Back · Previous · Next Maya

How do I replicate the Frame Selected and Frame All hotkey functions in MEL?

Maya's built-in command for this is viewFit. You will need the object(s) in the selection list to be able to frame only specific items in the view.

For example, to "frame selected" only "pSphere1" in the current camera view:

select -r pSphere1;
viewFit;

To "frame all" in the current camera view:

viewFit -all;

The internal MEL script that Maya calls with the ‘f’ or ‘a’ key is "fitPanel.mel". It takes a string argument -- either "−all" or "−selected" (although anything other than "−all" counts as "−selected").

This script takes care of the differences between the model views and the various editors/panels. These editors have their own commands for framing selected items.

To conclude, the script command you would want to include in your scripts is:

// Frame selected only in panel under mouse pointer
fitPanel -selected;
// Frame all in panel under mouse pointer
fitPanel -all;

The ‘fitPanel’ procedure uses the panel under the mouse pointer, irregardless of whether it has active focus.


Framing Control For Arbitrary Panels

What if you want to perform a "fit" for a specific panel instead of relying on the user to float the mouse pointer in your direction? In this case, you will need to:
  1. Find the instance(s) of the panel currently available.

  2. Determine which instance is the one you want to process.

  3. Use the panel type's command syntax to perform the necessary view fit.

Some of these steps are utilized in the "fitPanel.mel" script, and these are the reasons for this function.


Examples

Perspective Camera (or any Camera, really)

Conveniently, we don't have to bother with the first two steps here. When dealing with a camera view you can use the 'viewFit' command which accepts the name of a camera.

  1. Find the instance(s) of the panel currently available.

  2. Determine which instance is the one you want to process.

  3. Use that panel's type's command syntax to perform the necessary view fit.

      // Frame the selected items in the persp camera view
      viewFit persp;
      

HyperGraph

  1. Find the instance(s) of the panel currently available.

  2.   string $hyperGraphPanels[] = `getPanel -scriptType hyperGraphPanel`;
      // Result: hyperGraphPanel1 hyperGraphPanel2 //
      
  3. Determine which instance is the one you want to process.

    The main HyperGraph panel is labelled "Hypergraph"; tearoff copies are named "Hyper Graph Panel 2", etc.

  4.   string $mainHyperGraph = "";
      for ( $panel in $hyperGraphPanels )
      {
        // Looking for the main HyperGraph panel
        if ( "Hypergraph" == `scriptedPanel -q -label $panel` )
        {
          $mainHyperGraph = $panel;
          break;
        }
      }
      // Result: hyperGraphPanel1 //
      
  5. Use that panel's type's command syntax to perform the necessary view fit.

    In the case of the HyperGraph the MEL command used is ‘hyperGraph’. However, its use here requires a little trickery. The HyperGraph you live and work with is actually a ‘hyperPanel’ object within a scriptedPanel. The name we have found is the scriptedPanel. By convention Maya names the hyperPanel editor by appending the string "HyperGraphEd" to the name of the panel.

      // Assert that we've found our desired HyperGraph panel
      if ( "" != $mainHyperGraph )
      {
        string $hyperGraphEditor = ( $mainHyperGraph + "HyperGraphEd" );
        // Frame selected items in the HyperGraph
        hyperGraph -e -frame $hyperGraphEditor;
    
        // To frame all items the command syntax is:
        // hyperGraph -e -frameGraph ...
      }
      

Note: You use the same technique to frame the selected items in the HyperShade Editor. You would use the ‘hyperGraph’ command, but would append the string "HyperShadeEd" instead of "HyperGraphEd".

Graph Editor

The Graph Editor is also a scriptedPanel:

  1. Find the instance(s) of the panel currently available.

  2.   string $graphEditors[] = `getPanel -scriptType graphEditor`;
      // Result: graphEditor1 graphEditor2 //
      
  3. Determine which instance is the one you want to process.

    The main Graph Editor is labelled "Graph Editor"; tearoff copies are named "Graph Editor 2", etc.

  4.   string $mainGraphEditor = "";
      for ( $panel in $graphEditors )
      {
        // Looking for the main Graph Editor
        if ( "Graph Editor" == `scriptedPanel -q -label $panel` )
        {
          $mainGraphEditor = $panel;
          break;
        }
      }
      // Result: graphEditor1 //
      
  5. Use that panel's type's command syntax to perform the necessary view fit.

    Again, similar trickery as the Graph Editor is presented as a scriptedPanel and Maya identifies the embedded editor by appending to its name the string "GraphEd". This time the panel is accessed by the MEL command ‘animCurveEditor’.

      if ( "" != $mainGraphEditor )
      {
        string $graphEditor = ( $mainGraphEditor + "GraphEd" );
        animCurveEditor -e -lookAt selected $graphEditor;
    
        // To frame all items the command syntax is:
        // animCurveEditor -e -lookAt all ...
      }
      

NOTE: All of this from Maya v2.5.1. No idea if any of this changed for v3.0


Related How-To's

Friday, September 29, 2000