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

发布于 2024-10-31 17:44:17 字数 2590 浏览 8 评论 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. // 

Coming from a Java/C# background, the MEL site came as a bit of a letdown to me, maybe because I wasn't used to it, but didn't think it was as clear as some API sites.

I'm trying to write a MEL command to automate my save-as, rename file, save cycle. I know there must be some script out there already that does this, but I wanted to learn myself too

Initially, I want the user to click user-defined button from the shelf, and have a prompt-dialog, preloaded with the current scene name, with save-as and cancel button. Renaming and saving is fine, it's the separating out into functions (is there a difference between functions and procedures?) that started to spawn errors.

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

And the errors:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东京女 2024-11-07 17:44:17

我通过将 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");
    }
}

I solved this by adding the global keyword to the sceneName_new variable, and also declaring my use of it in the function it was called in - this will make the procedure use that global variable rather than create a new local one.

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");
    }
}
谎言 2024-11-07 17:44:17

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

global string $sceneName_new;

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

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

You need use global keyword in $sceneName_new variable definition. Also you need to specify global variable in every procedure where you use it.

global string $sceneName_new;

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

proc confirmOverwriteOkay() 
{
    global string $sceneName_new;
    //content
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文