Commands

 

String Evaluation

Strings play an important role in assembling commands in MEL while a script is running. Every command can be seen as a string which is evaluated. Thus the following example:

    string $myCommand = "sphere" + " " + "-n mySphere";
    eval($myCommand);

The example first declares a string and gives it the value "sphere -n mySphere" then the string is evaluated as a command.

 

String Results

Very often commands return one or more string values as results. These then have to be saved in string variables or arrays. See the following example:

string $myResults[] = `ls -tr`;
print $myResults[0];

The command ls -tr lists all transform nodes in the current scene, this list is then saved in a string array. The first element of the array is then printed. Let us look at an example which connects the two previous examples:

sphere -n mySphere1; 
sphere -n mySphere2; 
string $myResults[] = `ls -tr "mySphere*"`;    
int $numResults = size($myResults); 
string $myCommand = "setAttr " + $myResults[$numResults-1] + ".translateX 10"; 
eval($myCommand); 

This example creates two new spheres in a scene with special names, then it lists all transform objects which contain "mySphere". This list gets stored in an array and an attribute of the last element gets changed.

Very often the strings which are received as results cannot directly be used as an input for new commands, thus there are different commands, which are used to split, add or compare strings in order to put them into the right form. The following commands are an extract of what is possible

 

gmatch

Returns a non-zero result if the pattern specified by the second argument matches the search string in the first argument. gmatch provides UNIX shell-style pattern matching.

Examples

gmatch "matches" "m*s"; // Result: 1 //
gmatch "matches" "mat*"; // Result: 1 //
gmatch "no match" "?atch"; // Result: 0 //

mostly this command is used in if- or other control-statements.

 

substitute

Finds the portion of the second argument, the input string, that matches the regular expression in the first argument and replaces it with the string in the third argument. If there is no match between the input string and the regular expression then the original string is returned unaltered.

Example

string $test = "Hello ->there<-"; 
string $regularExpr = "->.*<-"; 
string $s1 = `substitute $regularExpr $test "Mel"`; // Result: Hello Mel // 
string $s2 = `substitute "foo" $test "Mel"`; // Result: Hello ->there<- // 

The string $s1 will contain "Hello Mel" and the string $s2 will contain "Hello ->there<-".

 

tokenize

This command will split the first string argument up according to split characters provided in the optional second argument. If this argument is not provided, tokenize will use a default split string consisting of whitespace characters. The input string is scanned for substrings (tokens) which are separated by any of the split characters. Note: tokenize does not match the entire split string; it matches any character in the string. The resulting token strings are put into the third argument which is a string array. The return value of this procedure is the number of tokens into which the original string is divided.

Example

string $buffer[];
$numTokens = tokenize("Mildred Pierce Femme Fatale", $buffer);

Buffer will contain 4 strings: "Mildred", "Pierce", "Femme", and "Fatale." and $numTokens will be 4.

This website has been archived and is no longer maintained.