MEL substitute command won't execute within a function

Hello!

Currently, I’m removing the namespaces from a string and storing the new string in a variable. I’m using the substitute command and replacing the unwanted string with an empty string to delete it. It works fine when I ctrl+enter the selected code, but when I try to make a UI and put the code inside a function, I get errors.

I’m wondering if anyone knows a fix for using substitute with an empty string inside a function, or can suggest a better way of removing namespaces from a string (not actually removing them, just storing a non-namespaced object name in a variable so I can execute a code). I’ve never used Python in a MEL script, but I’d be up for it (it deals with strings a lot better). I’ve been coding heavily for about 8 months now, so I’m pretty new- any tips would be appreciated.

When the following code is executed by calling the global proc, functionScript, I get this error:

// Error: line 99: Invalid call to “substitute”. Check number and types of arguments expected by the procedure. //
// Error: line 1: Invalid call to “substitute”. Check number and types of arguments expected by the procedure. //


global proc functionScript()
{
    /*** Export: get the character's name from the namespace.  Remove the string "_rig:*" from it so it can be exported to the proper directory ***/
    // List the keyword objects and their namespace
    $exportCharNS = `ls -sns "*:*GEO*"`;
    // Take the 2nd item in the list (this is the full name space)
    $exportCharFullNS = $exportCharNS[1]; 
    // Remove "_rig:*" namespace
    string $remove_rigName = "_rig:*";
    string $exportCharDir = `substitute $remove_rigName $exportCharFullNS ""`; // THIS IS THE PROBLEM LINE //
    // Select sets then export
    select -r -ne shader_relationships; 
    select -tgl -ne shader_list_set; 
    file -force -options "v=0;" -typ "mayaBinary" -pr -es ("exampleDir" + $exportCharDir + "/textures/" + $exportCharDir + "_master_shaders.mb"); 
    select -cl;    
}

Again, it works no problem when I ctrl+enter the script, but when I run the function it breaks. Thanks for any help!

As a rule of thumb, always explicitly define your variable types. Don’t rely on maya determining what they are by their value as sometimes it doesn’t convert them to the type you expect and won’t work with certain commands (like substitute for instance).

Use these lines instead and see if it works:

string $exportCharNS[] = ls -sns "*:*GEO*";
string $exportCharFullNS = $exportCharNS[1];

Brad
:¬)

Thanks chinwagon! Worked like a charm :smiley: