View Full Version : [MEL] search string variable OR Tokenize the quote character (")

03 March 2012, 02:37 AM
I know this is a stretch, but is there anyway that I can tokenize a " character?

if not, is there any way that I can search a string variable for a particular group of characters? I know that within maya I could search:
ls "*_RN*"

to find any node that contains "_RN" ... is there any way that I could do something like this within a string variable? what I am trying to do is pull both the path, and the reference name from the mayaAscii file.

---> In pseudo code I could get around this by:

$path = file -rdi 1 -ns "sphere___Copy" -dr 1 -rfn "sphere___CopyRN" "C:/Users/Will/Documents/maya/projects/default//scenes/sphere -";

//STEP 1//
tokenize $path "RN" $noRN;
(which gives me two arrays: $noRN[0] and $noRN[1] )
($noRN[1] would equal: "C:/Users/Will/Documents/maya/projects/default//scenes/sphere -";"

//STEP 2//
string $pathQuote[] = strip($noRN[1]);
(this separates the first two quotation marks)

//STEP 3//
and then I would need to reassemble everything from path[1] until (size($pathQuote) - 1) into one variable which would contain:
string $pathSColon = "C:/Users/Will/Documents/maya/projects/default//scenes/sphere -";

//STEP 4//
tokenize $pathSColon ";" $finalPath
(this would get rid of the semicolon, and leave me with nothing but the path in quotation marks)

It just feels like a bit much for what I am trying to do... I feel like there has to be an easier way that I am unaware of...

Thank you so much!!

03 March 2012, 01:32 PM
Is this what you are looking for? You simply need to escape the double quote in the tokenize command.

$str = "ab\"cd\"ef\"gh";

string $tokenized[];
tokenize $str "\"" $tokenized;

print $tokenized;

03 March 2012, 08:43 PM
The best solution would be to NOT use Mel ;-)

If i understand you correctly, here's how to do this in Python

path = 'file -rdi 1 -ns "sphere___Copy" -dr 1 -rfn "sphere___CopyRN" "C:/Users/Will/Documents/maya/projects/default//scenes/sphere -";'
path = path.split('"')[-1]

'C:/Users/Will/Documents/maya/projects/default//scenes/sphere -'

03 March 2012, 09:10 PM
Nothing wrong with MEL in this scenario:

string $str = "file -rdi 1 -ns \"sphere___Copy\" -dr 1 -rfn \"sphere___CopyRN\" \"C:/Users/Will/Documents/maya/projects/default/scenes/sphere -\"";
string $splitStr[];
tokenize $str "\"" $splitStr;
string $path = $splitStr[size($splitStr) - 1];

03 March 2012, 12:07 AM
Nothing wrong with MEL in this scenario:

Except for spending 3 lines and a lot of thinking to do something that you should be able to write in one line - and read and write pretty much as fast as you read a normal sentence in English.

That's 3 lines of clunky code to introduce bugs in, with the stupid tokenize command that doesnt work the same way as mel usally does (ie, you cant write string $split[] = `tokenize $word "/"`) - AND you have to write gibberish like $splitStr[size($splitStr) - 1] instead of just splitStr[-1].

Even Python called from mel is a lot easier and more readable than any possible way of doing it in Mel only

string $path = python("'"+$path+"'").split('\"')[-1]")

Yes, you can do this in Mel, but you generally end up spending a lot of time on things that are child's play in Python, and can't do a tenth of what you can do in Python in the first place. Hence my snarky comment about not doing this in Mel

03 March 2012, 12:44 AM
When the question specifically states "MEL" in the title, and it is referring to a specific MEL command (e.g. tokenize), there is little point in posting a snarky reply about how Python is superior.

Yes, Python is a very powerful language, my language of choice when scripting in Maya, but it doesn't change the fact that there is a massive amount of legacy MEL code out there and knowing how to work with it effectively is still a useful skill.

03 March 2012, 02:40 PM
Thank you so so much guys!!

using \" worked perfectly!! I haven't gotten into pyMel yet, I was hoping to go the C++ API route eventually, simply because I know a (tiny) bit of C++ already, and I have never used Python. Is there a big advantage to using Python over using the C++ API? Speed of writing the programs maybe?

I wanted to write a script to do this outside of maya but one of my friends at work told me that it would be better to make it something that could be a shelf button - that way it would be universal (some of my coworkers use Macs at home), and it would be one less program to keep track of.

I am so sorry about the late reply, I was moving my girlfriend in this weekend :). Thank you so much!!

CGTalk Moderation
03 March 2012, 02:40 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.