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

时间:2011-04-10 18:57:37

标签: function scope maya mel

来自Java / C#背景,MEL网站对我来说有些失望,可能是因为我不习惯它,但不认为它像某些API网站一样清晰。

我正在尝试编写MEL命令来自动执行保存,重命名文件,保存周期。我知道必须有一些脚本可以做到这一点,但我也想学习自己

最初,我希望用户从书架上单击用户定义的按钮,并有一个预加载对话框,预装了当前场景名称,带有save-as和cancel按钮。重命名和保存很好,它是分离出函数(函数和程序之间有区别吗?),它们开始产生错误。

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();    

错误:

// Error: -text $sceneName_new; // 
// Error: "$sceneName_new" is an undeclared variable. // 
// Error: -button "Yes, overwrite" -button "No, rename" -button "No, cancel" // 
// Error: Syntax error // 
// Error: saveScene($sceneName_new); // 
// Error: "$sceneName_new" is an undeclared variable. // 

2 个答案:

答案 0 :(得分:0)

我通过将全局关键字添加到sceneName_new变量中来解决这个问题,并且还声明我在调用它的函数中使用它 - 这将使该过程使用该全局变量而不是创建一个新的本地人。

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");
    }
}

答案 1 :(得分:0)

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

global string $sceneName_new;

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

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