一半君的总结纸

听话只听一半君

#42 How do I find/close Maya’s editor windows — the Dope Sheet and Graph Editor, for example?

Maya editor windows are primarily based on panels (an editor window is typically a torn-off panel). You can query panel information using the MEL command getPanel.

There are several ways to determine if the Graph Editor window/panel is currently displayed.

  1. You could do a string-match to find the Graph Editor panel(s).
    getPanel -visiblePanels;
    // Result: modelPanel4 graphEditor1 //
    

    Scan the returned array for the string “graphEditor”, likely using the ‘gmatch’ MEL command. Those that match stand a good chance of representing a Graph Editor panel.

  2. Typically the Graph Editor has the label “Graph Editor”.
    getPanel -withLabel "Graph Editor";
    // Result: graphEditor1 //
    

    The panel returned in this query is likely a Graph Editor.

  3. Query all existing panels of scripted type “graphEditor”.
    getPanel -scriptType graphEditor;
    // Result: graphEditor1 //
    

    As above, this is likely a Graph Editor.

As implied here, the Graph Editor is a scripted panel type. Its behavior is controlled (at least partially) through MEL support scripts.

Closing the Graph Editor window is not as simple as deleting “graphEditor1.” This doesn’t kill the window but rather deletes the panel within the window.

Unfortunately, there is no mechanism in MEL to easily backtrack from an existing panel to its parent window — the ‘−parent’ flag for the ‘scriptedPanel’ command does not offer a Query mode.

It is sometimes possible to query the “control” for the scriptedPanel and tokenize the UI path to extract the window portion:

string $graphEditor[] = `getPanel -scriptType graphEditor`;
for ( $editor in $graphEditor )
{
  string $tokens[];
  string $control = `scriptedPanel -q -ctl $editor`;

  if ( $control != "" )
  {
    if ( 0 < `tokenize $control "|" $tokens` )
    {
      print ( "Window for Graph Editor: " + $tokens[0] + "\n" );
    }
  }
  else
    print ( "Could not determine Window for Graph Editor.\n" );
}

// Result: Window for Graph Editor: graphEditor1Window //

Note: Heed the documentation warnings — it is possible for the control to be NULL. Make sure you check for this.

As the above suggests, it is somewhat a standard practice within Maya for a panel’s parent window to simply be the name of the panel appended with the string “Window”. If you want to make this assumption, you could do this:

string $graphEditor[] = `getPanel -scriptType graphEditor`;
for ( $editor in $graphEditor )
{
  string $graphEditorWindow = ( $editor + "Window" );

  if ( `window -exists $graphEditorWindow` )
    print ( "Window for Graph Editor: " + $graphEditorWindow + "\n" );
  else
    print ( "Could not determine Window for Graph Editor.\n" );
}

// Result: Window for Graph Editor: graphEditor1Window //

Now that you’ve found the window for the Graph Editor you can “close” it using the ‘deleteUI’ command:

deleteUI -window graphEditor1Window;

MEL Tip

There are two ways to ensure that the UI you are deleting is in fact a window:

  1. Assert that the window exists.
    if ( `window -exists $window` ) { deleteUI $window }
    
  2. Specify that deleteUI should only delete a window.
    deleteUI -window $window;
    

Note: If the editor panel is currently docked, your query will identify the main Maya window:

// Result: Window for Graph Editor: MayaWindow //

It’s likely not a good idea to delete this window. The user may get upset.

Most of the above should work for all UI panels, and not just the Graph Editor.

// Query derived from panel's control
string $dopeSheetPanels[] = `getPanel -scriptType dopeSheetPanel`;
// Result: dopeSheetPanel1 //

string $dopeSheet = $dopeSheetPanels[0];

string $control = `scriptedPanel -q -ctl $dopeSheet`;
// Result: dopeSheetPanel1Window|TearOffPane|dopeSheetPanel1 //

if ( $control != "" )
{
  string $tokens[];
  if ( 0 < `tokenize $control "|" $tokens` )
  {
    print ( "Window for Dope Sheet: " + $tokens[0] + "\n" );
  }
}
else
  print ( "Could not derive Dope Sheet Window from control.\n" );

// Result: Window for Graph Editor: dopeSheetPanel1Window //

// Query assuming name includes "Window" string
string $dopeSheetWindow = ( $dopeSheet + "Window" );

if ( `window -exists $dopeSheetWindow` )
  print ( "Window for Dope Sheet: " + $dopeSheetWindow + "\n" );
else
  print ( "Could not find Dope Sheet Window by name.\n" );

// Result: Window for Dope Sheet: dopeSheetPanel1Window //

Related How-To’s

Wednesday, January 10, 2001

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: