Jump to content

VEX problem - for s in string


Recommended Posts

Hi

I'm having difficulty doing something which I think it probably quite straightforward. I have a new VEX operator type which takes a user string input.

For each of the characters I want to be able to append a group of integers to an integer array, effectively I want to do something like this :

 

for S in STRING{

    do something;

}

 

I've tried all sorts of braces and such but it's not working. Can anyone help?

Thanks very much

Link to comment
Share on other sites

Thanks Ryan - I think that might be beyond me for now unfortunately, I'm a bit of a newcomer to coding in Houdini. I'll look into python when I get chance - would I do that with a python SOP or what?

Thanks again

Link to comment
Share on other sites

That's very kind, thanks!

I didn't realise that VEX was limited like that with strings. I'm coming from a Maya background where you can do

 

for ($S in $STRING)

 

and I just presumed I could do that here too.

Link to comment
Share on other sites

Yes just like Tom said, every Houdini artist has an equal voice for SESI, maybe some more than others :)

 

The reason I ask people to submit these is because as much as SESI uses VEX inside Houdini, it's not comparable to regular usage of VEX day in and day out in the long run. Also SESI is enjoying the mighty boost library to notice the pain of manipulating strings in VEX :)

Link to comment
Share on other sites

string my_string = "t h i s i s m y s t r i n g";
string char_array[] = split(my_string);

foreach(string char; char_array)
   printf("%c, ", char);
   //do someting else with char...
 

Not perfect as characters have to be separated with spaces, but works. Wonder if sprintf() can't be used to separate chars? Another trick would be to make two params. First for user's usage, second for python expression returning spaced copy of first parm...

  • Like 1
Link to comment
Share on other sites

Thanks Symek. I thought I would try and do it in python as Ryan suggested, good opportunity to learn some. This is what I have come up with in the end, in case anyone wants to do the same sort of thing or has suggestions to improve the code :

 

---------------------------

 

def pixeldisplay():
 
    n = hou.pwd()
    g = n.geometry()
 
#get geometry attributes and calculate index
    row = g.pointFloatAttribValues('row')
    col = g.pointFloatAttribValues('col')
    numrows = g.attribValue('numrows')
    numpts = len(row)
 
    g.addAttrib(hou.attribType.Point,'index',-1)
    for point in g.points():
        ptnum = point.number()
        index = (col[ptnum] * numrows) + row[ptnum]
        point.setAttribValue('index',int(index))
        
#declare letters
    letterA = [0,1,1,1,1,1,0,1,0,0,0,1,1,1,1]
    letterB = [1,1,1,1,1,1,0,1,0,1,0,1,0,1,0]
    letterC = [0,1,1,1,0,1,0,0,0,1,1,0,0,0,1]
    letterD = [1,1,1,1,1,1,0,0,0,1,0,1,1,1,0]
    letterE = [1,1,1,1,1,1,0,1,0,1]
    letterF = [1,1,1,1,1,1,0,1,0,0]
    letterG = [1,1,1,1,1,1,0,0,0,1,1,0,0,1,1]
    letterH = [1,1,1,1,1,0,0,1,0,0,1,1,1,1,1]
    letterI = [1,0,0,0,1,1,1,1,1,1,1,0,0,0,1]
    letterJ = [0,0,0,0,1,1,1,1,1,0,0,0,0,0,0]
    letterK = [1,1,1,1,1,0,0,1,0,0,0,1,0,1,1]
    letterL = [1,1,1,1,1,0,0,0,0,1]
    letterM = [1,1,1,1,1,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,1,1,1,1,1]
    letterN = [1,1,1,1,1,0,1,0,0,0,0,0,1,0,0,1,1,1,1,1]
    letterO = [0,1,1,1,0,1,0,0,0,1,0,1,1,1,0]
    letterP = [1,1,1,1,1,1,0,1,0,0,0,1,0,0,0]
    letterQ = [0,1,1,1,0,1,0,0,0,1,0,1,1,1,1]
    letterR = [1,1,1,1,1,1,0,1,0,0,0,1,0,1,1]
    letterS = [0,1,0,0,1,1,0,1,0,1,1,0,0,1,0]
    letterT = [1,0,0,0,0,1,1,1,1,1,1,0,0,0,0]
    letterU = [1,1,1,1,1,0,0,0,0,1,1,1,1,1,1]
    letterV = [1,1,1,0,0,0,0,0,1,1,1,1,1,0,0]
    letterW = [1,1,1,1,0,0,0,0,0,1,0,0,1,1,0,0,0,0,0,1,1,1,1,1,0]
    letterX = [1,1,0,1,1,0,0,1,0,0,1,1,0,1,1]
    letterY = [1,1,0,0,0,0,0,1,1,1,1,1,0,0,0]
    letterZ = [1,0,0,1,1,1,0,1,0,1,1,1,0,0,1]
    letter_ = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
 
#empty string to store letters
    text = []
 
#get user input text
    inputtext = hou.evalParm('text')
 
#append letter lists to text list
    for char in inputtext:
        l = "letter"
        l+=char
        seq=eval(l)
        space=eval("letter_")
        for digit in seq:
            text.append(int(digit))
        for s in space:
            text.append(int(s))
 
#for each point assign correct value
    p = g.points()
    points_in_text = len(text)
    for point in p:
        ptnum = point.number()
        if ptnum < points_in_text: #avoid out of range error
            on = text[ptnum]
            point.setAttribValue('on',on)
        
pixeldisplay()
Link to comment
Share on other sites

  • 5 years later...

Hi, this seems a lot like the problem L-systems are used to. this is my take on this subject: 

Strings in Houdini have a index property, so if a string is: word="Hello" then word[3]="l"

Also you can find out the length of a string by strlen(), It's one of the rare string functions in vex.  

A cheap way to do something from a user string input would be this: 

string word=chs("Axiom");

for(int l=0 ; l<strlen(word) ; l++){

   if(word[l]==F) doSomething();

   if(word[l]==B && word[l+1]==t) doSomethingElse();

   if(word[l]==B && word[l+1]==g) doSomethingElse();

   }

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...