Home Technical Talk

[Maya Melscript] Searching through a string of Text for a symbol?

StephenVyas
polycounter lvl 18
Offline / Send Message
StephenVyas polycounter lvl 18
Hi guys,

I'd like to find a way to  search through a piece of text for a specific symbol
This symbol     :


Think I got the hard part done, but could really use an extra brain to help with the rest
 
proc string checkforsymbol(string $inputName) {
if this    :      symbol exists within the $inputName string.
return removeNameSpace($inputName);
}else {
return $inputName;
}



proc string removeNameSpace(string $inputName){
string $outgoingName[];
$numTokens = `tokenize $inputName ":" $outgoingName`;
return $outgoingName[1];
}

Any ideas?

Replies

  • Bartalon
    Options
    Offline / Send Message
    Bartalon polycounter lvl 12
    Try using the match command.

    {
    string $test = "group1:pCube1";
    if ( `match ":" $test` == ":" ) {
    print "Found special character. \n";
    }
    }

    Alternatively you should just be able to tokenize without having to check for it first.  If tokenize doesn't catch anything to split, the destination array will have a size of 1, the original string.


  • StephenVyas
    Options
    Offline / Send Message
    StephenVyas polycounter lvl 18

    Perfect! Thanks Bartalon :)

  • kodde
    Options
    Offline / Send Message
    kodde polycounter lvl 19
    This is something python does easily for you. Could be a reason to switch to python instead? ;)
  • StephenVyas
    Options
    Offline / Send Message
    StephenVyas polycounter lvl 18
     Yeah, I'd like to learn a Python.
    Because Maya chooses to echo all of it's commands in Melscript.. it's simply easier for me to learn & figure out the syntax of the mel  language

    I have no doubt Python can do things more efficiently :)
  • tkvtoons
    Options
    Offline / Send Message
    tkvtoons vertex
    Took a crack at this in Python. I did it quickly, so only took into account if the inputString contains just 1 namespace.

    inputString = 'tester:test'

    def checkForSymbol(inputName):
        if ':' in inputName:
            outgoingName = inputName.split(':')[1]
            return outgoingName
        else:
            return inputName

    checkForSymbol(inputString)




  • StephenVyas
    Options
    Offline / Send Message
    StephenVyas polycounter lvl 18
    Holy crap Python is clean. Beauty job TkvToons 

    I really like how you don't have to declare whether variables are strings, ints, floats, etc
    or brackets even??  jeeez.. that would have saved me loads of time from trying to track down those misplaced squirrely sobs ;)
  • tkvtoons
    Options
    Offline / Send Message
    tkvtoons vertex
    I'm sure someone else could write it even cleaner than that. I'm just starting to get more comfortable with Python. I've been scripting in MEL for 6+ years or so and just started to force myself to convert to Python a few months ago.

    I do like how clean it can be, variables don't have to be defined, no ending lines with semi-colons. It just feels much more efficient. Only thing you really have to watch out for is indents - messed up indents can make everything stop working.
Sign In or Register to comment.