0

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

和错误:

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

2 回答 2

0

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

global string $sceneName_new;

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

proc confirmOverwriteOkay() 
{
    global string $sceneName_new;
    //content
}
于 2013-04-12T12:26:26.597 回答
0

我通过将global关键字添加到 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");
    }
}
于 2011-04-11T10:09:01.087 回答