2011-04-10 113 views
0

从Java/C#背景来看,MEL网站对我来说有点让人失望,也许是因为我不习惯它,但并不认为它是作为一些API网站清楚。2011年Maya中的未声明变量使用MEL

我想写一个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. // 
+0

如果我注释掉overwriteConfirm()函数的代码,它工作正常(没有的功能:P) – ataulm 2011-04-11 09:18:30

回答

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

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

global string $sceneName_new; 

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

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

感谢@emigue我解决了它去年使用该技术。 – ataulm 2013-04-12 14:54:05