Notifications
Clear all

[Closed] Way to tokenize a name like with Mel?

Is there anything like Tokenize in Max?

I have a max file name that’s like pig_Neutral_RunCycle1_4_0611 And all the files end in some serious of numbers like that as some sort of version numbering, or some are like _v3855. But when I export out the animation file I just want pig_Neutral_RunCycle1_4 without the version number on the end. And I don’t know that the length of characters is the same at the end of all these files or that even all of them have it.

So it there a way to break it up by the underscores and then string it back together without the version part on the end?

I found a script on scriptswell that removes the spaces from names and replaces them with underscores, but I don’t think that’s quite what I need nor do I quite follow how that works.
http://www.scriptswell.net/2009/05/maxscript-removing-spaces-from-names.html

6 Replies
1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

if you are sure that all filenames end with useless numbers with wildcard pattern “_” or “_v” you can use trim method:


 trimmedStr = trimright originStr "vV0123456789"
  -- removes numbers and letters v or V, stops on the last [i]underscore[/i] 
 trimmedStr = trimright trimmedStr "_" 
  -- trims the last [i]underscore[/i] 
  

Hi,
Lookup filterstring in maxscript help
it return an array of the parts of a string filtered by another string

filterstring "pig_Neutral_RunCycle1_4_0611" "_"
 #("pig", "Neutral", "RunCycle1", "4", "0611")

Ah. Cool. Ok, So I can get the tokens out of it now, but how do I access them in the script without naming them specifically? Like in Maya it’d be something like $buffer[0] + $buffer[1]+$buffer[2]

Looking though the help files and I don’t see off hand what I use for that.

2 Replies
(@solitude)
Joined: 11 months ago

Posts: 0

foo = filterstring “pig_Neutral_RunCycle1_4_0611” “_”
foo[1]

(@bobo)
Joined: 11 months ago

Posts: 0

Keep in mind MAXScript uses 1-based indexing, otherwise it is the same as Ian pointed out.
You could also use matchPattern() to perform simple pattern matching using wildcards for example to detect whether a token is a version string. (For real pattern matching, you would have to use DotNet methods).

So for example if you say

theTokens = filterString someName "_"

you can then check whether the last token is a version token by saying

matchPattern theTokens[theTokens.count] pattern:"v*" 

which will return TRUE if the last token starts with “v” or “V” (matchPattern is case-insensitive by default, but has a flag to force case-sensitivity), or FALSE if it is not.
Also note that thanks to MXS being 1-based, you can use theTokens.count without having to subtract 1 from it which is more natural for artists albeit uncomfortable for programmers who are used to 0-based indexing…

So if you want to print the version number, you could say

IF matchPattern theTokens[theTokens.count] pattern:"v*" AND isKindOf (execute (substring theTokens[theTokens.count] 2 -1)) Number DO 
  format "Version Number is %
" theTokens[theTokens.count]

(I know you did not ask for this, but I am providing bonus info )

To build the rest of the name, you could do a FOR loop:

newName = ""
for i = 1 to theTokens.count-1 do 
  newName += theTokens[i]+ (if i < theTokens.count-1 then "_" else "")
format "New Name: %
" newName

This will rebuild the name from the tokens while skipping the last token (which we assume is the version – you can check again with matchPattern() to be sure)

You can find a quick overview of MEL vs. MXS here:
http://www.scriptspot.com/bobo/mel2mxs/mel2mxs.htm
(I had problems seeing the Index with Google Chrome, but it works in other browsers)

Awesome guys. Thanks for the help. This is sorta my first real dive into Max Script from Mel, so this has been really helpful.

The Trim was exactly what I needed (I think. There’s some thousand files in here and several categories, so we’ll see still, but it’ll cover the bulk of it.)
Thanks again. This gets this export script to at least a much more manageable place.