介绍

使用 MEL 创建在发生特定系统事件时运行的脚本。使用 scriptJob 命令可完成此操作。Maya 会定义大量系统事件,您可以将脚本附加到这些事件中。正常使用 Maya 就会触发这些事件。有些事件会在选择发生更改时、打开新文件时以及拾取新工具时告知您。

可以使用以下带有 -listEvents 标志的 scriptJob 命令获取所有事件的完整列表:scriptJob -listEvents;这些事件的名称通常不需解释;详细说明可在 scriptJob 文档中找到。

还有另一种系统事件,称为条件。条件与事件类似,只是条件还具有 True 或 False 值。有些条件可在选择某些内容或动画播放时告知您。可以使用以下带有 -listConditions 标志的 scriptJob 命令获取所有条件的完整列表:

1
scriptJob -listConditions;

无论条件的状态是从 True 更改为 False,还是从 False 更改为 True,条件都会触发其附加的脚本。使用 isTrue 命令测试其状态

示例

1
2
3
4
5
6
7
8
9
10
global proc checkY(){
float $y = `getAttr myObject.ty`;
if ( $y > 10.0 ){
window;
columnLayout;
text -l "Object is too far up!";
showWindow;
}
}
scriptJob -attributeChange "myObject.ty" "checkY";

对象升高得太远时以警告形式通知

删除作业

scriptJob 命令将脚本附加到事件或条件时,该命令将返回唯一的 job number,使用此编号来删除(消除)已创建的作业。假设以上数字为 17。若要停止该脚本使其不再运行,可以使用带有 -kill 标志的 scriptJob 命令

1
scriptJob -kill 17;

获得系统中运行的所有 scriptJob 的完整列表,使用 -listJobs

1
scriptJob -listJobs;

如果在 -runOnce 标志设置为 True 的情况下创建作业,则作业只会运行一次,随后便删除其本身。

可以使用 -parent 标志将作业附加到 UI 的特定元素,以便在删除 UI 元素时,也一同删除作业

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
global proc updateSelWind(){
if ( `isTrue SomethingSelected` ){
text -edit -label "Something is selected." selText;
} else {
text -edit -label "Nothing is selected." selText;
}
}
string $windowName = `window`;
columnLayout;
text selText;
updateSelWind;
showWindow $windowName;
scriptJob
-parent $windowName //将作业附加到窗口
-conditionChange "SomethingSelected" "updateSelWind";