Function Maya 2011中未声明的变量,使用MEL

Function Maya 2011中未声明的变量,使用MEL,function,scope,maya,mel,Function,Scope,Maya,Mel,来自Java/C背景的MEL站点让我有点失望,可能是因为我不习惯它,但认为它没有一些API站点那么清晰 我正在尝试编写一个MEL命令来自动执行“另存为”、“重命名文件”和“保存周期”。我知道一定有一些脚本已经做到了这一点,但我也想学习自己 最初,我希望用户单击工具架上的“用户定义”按钮,并使用“另存为”和“取消”按钮创建一个提示对话框,其中预加载了当前场景名称。重命名和保存很好,这是分离成函数的过程。函数和过程之间有区别吗?这开始产生错误 string $sceneName_new; //

来自Java/C背景的MEL站点让我有点失望,可能是因为我不习惯它,但认为它没有一些API站点那么清晰

我正在尝试编写一个MEL命令来自动执行“另存为”、“重命名文件”和“保存周期”。我知道一定有一些脚本已经做到了这一点,但我也想学习自己

最初,我希望用户单击工具架上的“用户定义”按钮,并使用“另存为”和“取消”按钮创建一个提示对话框,其中预加载了当前场景名称。重命名和保存很好,这是分离成函数的过程。函数和过程之间有区别吗?这开始产生错误

string $sceneName_new;


// Initiates the scene saving, checking filename meets standards
proc saveSceneAs() {

    string $sceneName_old = `file -q -sceneName`;
    string $result = `promptDialog 
        -title "Save scene as"
        -message "Scene name:"
        -button "Save" -button "Cancel"
        -text $sceneName_old
        -defaultButton "Save" -cancelButton "Cancel"
        -dismissString "Cancel"`;

    if ($result == "Save") {
        $sceneName_new = `promptDialog -query -text`; // get result
        $sceneName_new = strip($sceneName_new); // clean whitespace (start/end)

        // check length of new name has at least one character
        if (size($sceneName_new) < 1) {
            print("Error: file name must contain at least one character. File not saved.\n");
        } else if ($sceneName_old == $sceneName_new) {
            confirmOverwriteOkay();
        } else {
            // good to save :D
            saveScene($sceneName_new);
        }
    } else {
        print("Cancelled. File not saved.\n");
    }   
}

// Asks user in case of unchanged filename, if okay to overwrite
proc confirmOverwriteOkay() {
    string $overwriteConfirm = `promptDialog 
                -title "Warning"
                -message "Are you sure you want to overwrite the current file?"
                -text $sceneName_new;
                -button "Yes, overwrite" -button "No, rename" -button "No, cancel"
                -defaultButton "No, rename" -cancelButton "No, cancel"
                -dismissString "No, cancel"`;

    if ($overwriteConfirm == "Yes, overwrite") {
        saveScene($sceneName_new);
    } else if ($overwriteConfirm == "No, rename") {
        // go back and try again
        saveSceneAs();
    } else {
        print("Cancelled. File not saved.\n");
    }
}


// Saves the scene with the given file name
proc saveScene(string $nameToSave) {
    // TODO: rename, save file
    print("File saved: " + $nameToSave);
}


saveSceneAs();    

我通过向sceneName_新变量添加global关键字来解决这个问题,并在调用它的函数中声明我对它的使用-这将使过程使用该全局变量,而不是创建一个新的局部变量

global string $sceneName_new;

// Asks user in case of unchanged filename, if okay to overwrite
proc confirmOverwriteOkay() {

global string $sceneName_new;       
string $overwriteConfirm = `promptDialog 
                -title "Warning"
                -message "Are you sure you want to overwrite the current file?"
                -text $sceneName_new;
                -button "Yes, overwrite" -button "No, rename" -button "No, cancel"
                -defaultButton "No, rename" -cancelButton "No, cancel"
                -dismissString "No, cancel"`;

    if ($overwriteConfirm == "Yes, overwrite") {
        saveScene($sceneName_new);
    } else if ($overwriteConfirm == "No, rename") {
        // go back and try again
        saveSceneAs();
    } else {
        print("Cancelled. File not saved.\n");
    }
}

您需要在$sceneName\新变量定义中使用全局关键字。您还需要在使用全局变量的每个过程中指定全局变量

global string $sceneName_new;

proc saveSceneAs() 
{
    global string $sceneName_new;
    //content
}

proc confirmOverwriteOkay() 
{
    global string $sceneName_new;
    //content
}

如果我注释掉overwriteConfirm函数中的代码,那么它在没有以下功能的情况下可以正常工作:P
global string $sceneName_new;

proc saveSceneAs() 
{
    global string $sceneName_new;
    //content
}

proc confirmOverwriteOkay() 
{
    global string $sceneName_new;
    //content
}