Home Technical Talk

[Maya] Regular Expression

polycounter lvl 17
Offline / Send Message
sama.van polycounter lvl 17
I am being lost reading several Google and Wiki tutorial because of the difference of each programming language.

Maya is using common parser but I do not success to do what I would like...

You can give a quick look at the Maya`s documentation here :

- http://download.autodesk.com/us/maya/2009help/index.html


They explain some tips in the "gmatch" and "substitute" definition.


I would like to find an easy way extracting index or object name in the following string :

"|Render|Props_Crate_001.f[5]"



I already have my way to do that but it looks weird in my code and I really would like to have better use of the Regular Expression.



The weird methods are :


- Object name :


--- Method A :

string $temp[] = stringToStringArray ("|Render|Props_Crate_001.f[5]", ".");
string $obj = $temp[0];

--- Method B :

listRelatives -p `listRelatives -p |Render|Props_Crate_001.f[5]`




- Component index :


--- Method A :

string $tmp[];
int $numTokens = `tokenize "|Render|Props_Crate_001.f[5]" "[]" $tmp`;
int $index = $tmp[ $numTokens - 1 ];

--- Method B :

string $temp = `substitute "^.*\\[" "|Render|Props_Crate_001.f[5]" ""`;
$temp = `substitute "\\].*" $temp ""`;




Is there any challenger? ^_^

Replies

  • MoP
    Offline / Send Message
    MoP polycounter lvl 18
    I always use tokenize for extracting stuff like the path name, namespace, or component index.

    I only use regular expressions for matching things in string comparisons.

    For the object name in your example you would tokenize by "|", take the last value from the buffer, then tokenize by "." and take the first value from the buffer.

    For the index you just tokenize by "[]" and take the last value from the buffer as you said.

    You could also write a little helper procedure for that so you could return the value:
    int $vertIndex = getComponentIndex( "|group1|pCube1.f[4]" );
  • sama.van
    Offline / Send Message
    sama.van polycounter lvl 17
    Oh yeah the tokenize is in my "weird method".

    And the "getComponentIndex" could be a solution...

    My workflow currently works with a "Common" library.
    For example it means I have a Polygon.mel file and I insert all very basic function I create. I already have several files like that...

    then for the "getComponentIndex" I shouldn't not create common files but a sub directory called "Polygon" (for the example) and then each function in a separate *.mel.
    And then if the files name match with the function name, the call is direct and I do not need to source...


    Damn. I have for one year to convert my current system to the new one :D
    But very interesting I will do some test.


    But about the Regular Expression it was more for understanding advance use...
    And to try using in a more complex situtation!

    Look the following one :


    string $temp = `substitute "^.*\\[" "|Render|Props_Crate_001.f[5]" ""`;
    $temp = `substitute "\\].*" $temp ""`;


    Don't you know a way to insert the second expression in the first one? is there now flag for "AND" ???
  • sama.van
    Offline / Send Message
    sama.van polycounter lvl 17
    shit, the call of the procedure is not automatic if I drop it in a sub directory >_<...
    It only work to the root of the "maya\2009-x64\scripts\" directory...
  • claydough
    Offline / Send Message
    claydough polycounter lvl 10
    Sorry code tags are hanging poly count again :poly127:

    below are some of my matching notes... ( Because I forget what works for me, all the time :whyme: )

    basically,
    for selection: pCube1.f[27]

    //method 1 using ".[]"

    ".[]"
    are all illegal characters..
    Therefore, they only need to be accounted for once in any match.
    Tokenize then seperates the returns in the order:

    [0] object
    [1] component type
    [2] index address

    pCube1
    f
    27


    // method 2 using "\\[.*\\]"

    Match and tokenize produce different returns using the same search expression.
    Returning the component address with brackets can be just as important/useful.
    In which case I use match.

    using the expression "\\[.*\\]"

    tokenize [2]:
    27

    vs

    match:
    [27]

    refer to the print results below:





    // begin expression notes
    {
    string $headSelection[] =`ls -sl -head 1`;
    string $tokenized[];

    // method 1
    tokenize $headSelection[0] ".[]" $tokenized;

    print ( "\n"+ $tokenized[0] + " <---- ( the 1rst array member ) the object\n" );
    print ( $tokenized[1] + " <---- ( the 2nd array member ) the component type\n" );
    print ( $tokenized[2] + " <---- ( the 3rd array member ) the component index\n" );
    print " wow, illegal character presumptions makes string seperation easy!\n";

    // The result:
    /*
    pCube1 <---- ( the 1rst array member ) the object
    f <---- ( the 2nd array member ) the component type
    27 <---- ( the 3rd array member ) the component index
    wow, illegal character presumptions makes string seperation easy!
    */


    // method 2
    tokenize $headSelection[0] "[URL="file://\\"]\\[/URL][.*\\]" $tokenizedIsDifferent; // "
    $matchInstead = ` match "[URL="file://\\"]\\[/URL][.*\\]" $headSelection[0]`;

    print " \nThe following Tokenized [2] search expression resulted in: \n\n";
    print $tokenizedIsDifferent[2];
    print " \nWhich will not be equal to the results gained from Match using same exact search expression...\n";
    print "Match's result: \n\n";
    print $matchInstead;
    print "\n\nHowever, returning the index in brackets can be just as important/useful when replacing strings.\n";

    // The result:
    /*
    The following Tokenized [2] search expression resulted in:

    27

    Which will not be equal to the results gained from Match using same exact search expression...
    Match's result:

    [27]

    However, returning the index in brackets can be just as important/useful when replacing strings.
    */
    }
    // end notes
  • Shawner
    Offline / Send Message
    Shawner polycounter lvl 18
    Have you considered using Python. It's really is excellent for this type of thing. I forced myself to start using python about 2 years ago and have not regretted it.
    mainStr = "|Render|Props_Crate_001.f[5]"
    
    #split the string into a array/list using '.' as the argument
    splitMe = mainStr.split('.')
    print splitMe
    
    #splice out the 'f['  and ']'
    index = splitMe[1][2:-1]
    print index
    
    #now to do this all in one line
    finalVtx = mainStr.split('.')[1][2:-1]
    print finalVtx
    
    #To get the name
    name = mainStr.split('.')[0]
    print 'name: %snface: %s' %(name, finalVtx)
    
Sign In or Register to comment.