一半君的总结纸

听话只听一半君

#42 如何找到/关闭 Maya的 editor windows — 比如 Dope Sheet 和 Graph Editor?

Maya的 editor windows 大部分是基于 panels (editor window 其实是一个 torn-off panel)的. 你可以使用MEL command getPanel来查询 panel 信息.

有好几种办法来知道 Graph Editor window/panel 是不是当前可见.

  1. 你可以用 string-match 的方法来找到 Graph Editor panel(s).
    getPanel -visiblePanels;
    // Result: modelPanel4 graphEditor1 //
    

    在返回的 array 中找字符串 “graphEditor”, 可以用 ‘gmatch’ MEL command. 如果match为真,那么很有可能他就是一个 Graph Editor panel.

  2. 一般 Graph Editor 的 label 叫做 “Graph Editor”.
    getPanel -withLabel "Graph Editor";
    // Result: graphEditor1 //
    

    上面返回的 panel 很有可能是一个 Graph Editor.

  3. 查询所有存在的 panels 中,scripted type 类型是 “graphEditor”的.
    getPanel -scriptType graphEditor;
    // Result: graphEditor1 //
    

    如上所述, 这可能是一个 Graph Editor.

Graph Editor 的类型是 scripted panel. 他的行为受(至少是部分受) MEL support scripts的控制.

关闭 Graph Editor window 并不是简单的删除 “graphEditor1.”就可以了,这样做并不会删除 window,只会删掉window里的panel而已.

不幸的是, MEL里并没有什么机制能够找到一个panel的parent window — ‘scriptedPanel’ command 的‘−parent’ flag 不能在查询模式下使用(没有scriptedPanel -q -parent ).

但有时候可以通过查询scriptedPanel 的 “control”,然后解析返回的 UI path 来得到window的名字:

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 //

注意: 帮助里有警告过 — control 有可能是 NULL. 你得考虑到这一点.

如上所述, 通常Maya里一个panel的parent window的名字是panel的名字后面加上string “Window”. 如果你这么假设的话,你可以像下面这样:

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 //

现在你找到了Graph Editor的window,你可以用‘deleteUI’ command来”关闭”( “close”)他:

deleteUI -window graphEditor1Window;

MEL Tip

有两种方法可以保证你正要删除的UI是一个 window:

  1. 假设这个 window 存在.
    if ( `window -exists $window` ) { deleteUI $window }
    
  2. 告诉 deleteUI 只能删除 window.
    deleteUI -window $window;
    

注意: 如果 editor panel 当前是 docked状态, 你的查询会返回 main Maya window:

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

显然不应该把这个window删掉. 使用者可能会被激怒.

上面的方法应该适用于所有 UI panels, 而不仅仅是 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 博主赞过: