Tuesday, May 13, 2025

Script call from file as shelf item in Maya

Here's a nifty way to dynamically source a file as a shelf command but add a bit of error handling in it to make sure it exists! replace YOURSCRIPT.EXTENSION and YOURPROCEDURE as appropriate.


    string $filePath = `internalVar -userAppDir` + "scripts/YOURSCRIPT.EXTENSION";
string $procedureName = "YOURPROCEDURE()";

// Check if the procedure exists and clear it if it does
if (`exists $procedureName`) {
    print($procedureName + " exists. Clearing.\n");
    eval("global proc " + $procedureName + "() {}");
}

// Check if the file exists
if (`filetest -f $filePath`) {
    print("File found: " + $filePath + "\n");
    eval("source \"" + $filePath + "\""); // Use eval to source the file
    // Check if the procedure exists after sourcing the file
    if (`exists $procedureName`) {
        print("Procedure found: " + $procedureName + "\n");
        eval($procedureName + "()");
    } else {
        warning("Procedure not found after sourcing: " + $procedureName);
    }
} else {
    warning("File not found: " + $filePath);
}