MEL tokenize question...

MEL tokenize question...

Anonymous
Not applicable
532 Views
2 Replies
Message 1 of 3

MEL tokenize question...

Anonymous
Not applicable

Hi all

Wanted to know if anyone can recommend how to grab a certain part of a file name.

Example below:

abc_def_ghi_jkl_v001_pqr.ma

The main body that contains the underscores prior to the version number, can vary at times (e.g. abc_def_ghi_jkl_mno_v001_pqr.ma) so I want to be able to grab the version number (v001) by searching in reverse using the (.) as the delimiter. The section after the version number does not vary.

Thanks in advance!

Dan

0 Likes
533 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable

I think it would be easier to use regular expressions.

 

To get the part with the version number, you could probably do something like:

 

string $filename = "abc_def_ghi_jkl_v001_pqr.ma";
string $matchResult = `match "_v[0-9]+" $filename`;

 

Then you can do further work using the match result. The regular expression I used searches for anything that has "_v" plus at least one number.

If you can use Python, I always prefer that to MEL though. It's a lot better at this kind of stuff.

0 Likes
Message 3 of 3

Anonymous
Not applicable

Finally figured it out. Also, thanks for the feedback to Elveatles!

 

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

global proc string getCurrentSceneVersion()
{
string $currentVersion[];
string $currentScenePath = `file -q -sn`;
$dirSeperatedTokens = `tokenize $currentScenePath "/" $currentVersion`;
string $getFileName = $currentVersion[9];
$periodSeperatedTokens = `tokenize $getFileName "." $currentVersion`;
string $getFileNameMinusExt = $currentVersion[0];
return $getFileNameMinusExt;
}

 

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

global proc string substringBySeparator(string $subject,string $separator, int $startIndex, int $endIndex)
{
string $tokenized[];
tokenize $subject $separator $tokenized;
int $size = size($tokenized);

if ($startIndex<0) $startIndex = $size+$startIndex;
if ($endIndex<0) $endIndex = $size+$endIndex;

if ($startIndex<0 || $startIndex>=$size)
return "";
if ($endIndex<0 || $endIndex>=$size)
return "";

string $result = "";
for ($i=$startIndex;$i<$endIndex;$i++){
$result+=$tokenized[$i]+$separator;
}
$result += $tokenized[$endIndex];
return $result;
}

 

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

string $getSceneVersion = substringBySeparator(`getCurrentSceneVersion`,"_",-2,-2);

0 Likes