Jump to content
Sign in to follow this  
caskal

Switch using attributes, no Intergers?

Recommended Posts

Hi Magicians,

I'm using some TOPs PDG to autogenerate stuff with data conmig from a CSV spreadsheet.

I had the system working by having something like this:

 0NUnxEDU1v.png.21a953ecf3c7e432aa0d91bd175920c7.png

Where I used the "Hats_Type_Switch" to drive a Switch node (with Platinium / Gold / Brown)

But the client is giving me a different format:

HPdXiliWkc.thumb.png.e8cb485db4eb640db69eec54000f9e50.png

I have imported this as CSV and I can read the attributes, I did a "Font" test with attribute "Mouth" that gives me the result "Carrot"

 

U0XyksWPcW.thumb.png.ebe970505fd86d644bcf3e538d4f6eb2.png

My question is, how can I drive that switch with the attribute Mouth instead of 1, 2, 3? For example, if the attribute result for Mouth is Carrot, show the carrot, if the Mouth = Pipe, switch to Pipe

GB3Ebqx6yg.thumb.png.6d8cb3da2c10bc048c71d93b1cd7348b.png

Thanks!

 

 

 

Share this post


Link to post
Share on other sites

If only VEX had a switch() function that let you go case by case...

You can implement your own, example here. The idea is to set up an enumerator (which is kind of like using integers anyway) that compares an input to a kind of key-value in a dictionary. Depending on which value is matched, a different output can be chosen, and a default for if no match is found. I'd still recommend converting/outputting integers as it's probably faster than comparing string values, and the Switch works with ints anyway.

One thing to note is that the Switch SOP doesn't know what its inputs are, only that they are ordered by input index. It also has no way of ensuring that inputs are unique, so if you accidentally plugged 2 Carrots into it, it wouldn't be able to tell which one you wanted. Best to just stick with ints, imo.

  • Thanks 1

Share this post


Link to post
Share on other sites

Hey @Fenolis! thanks for the info. I made some progress but still trying to figure out something.

I made this on a Wrangle to convert the attributes to interger:

i@SwitchVal = 0;

if(s@mouth == "Empty") @SwitchVal = 0;
if(s@mouth == "Carrot") @SwitchVal = 1;
if(s@mouth == "Pipe") @SwitchVal = 2;

This works, but only if I set the attributes on SOPs. 

And I have the attributes coming from TOPs

Xwvq453Ws0.png.8729228c84702e697b094e2ae3b124d5.png

Not sure why the wrangle doens't look it, but when I put 'Mouth' on a Font node, it actually returns the value 'Pipe' on the viewport.

Lets say I fix the issue on the wrangle and I can call the attribute from TOPs, next thing I'm trying is to use it on a switch node.

point("../SWITCH_VAL/", @ptnum, "SwitchVal", 0)

But this also don't work, @ptnum is giving me an error, if I use 1 or 2, it works, but I cant access that attribute number I set on the wrangle for some reason.

Any thoughts?

Thanks!

CC Grandpa @f1480187

 

Share this post


Link to post
Share on other sites
Posted (edited)

It depends upon what you're wedging over. If you are wedging over points, in a 1:1 ratio, you might be able to replace @ptnum with the back ticked variable `@pdg_index`.

Or place your switched network inside a FOR/EACH running over points, then you can replace @ptnum with the constant 0.

Edited by Atom
  • Thanks 1

Share this post


Link to post
Share on other sites

Hey @Atom

Here's a sample pic ilustrating what I have to do, basically creating characters with accesories, with info coming from a CSV spreadsheet.

I made it work when I did a test creating a cel with intergers for the switchs, but the client spreadsheet will be tricky since it has a 10k output with random order.
Maybe worst case scenario, I can do a new excel with > Replace word > and do a "Carrot" gets replaced by "1" and I use that for the Switch interger selector.

But would love to solve this without that manual work.

carr0t.thumb.jpg.8375b71718e8abe84409b4e89a05a416.jpg

Thanks!

Share this post


Link to post
Share on other sites
Posted (edited)

Can't you just write an IF statement inside a wrangle that generates a companion index for each expected variation?

if (s@column_variation=="Carrot"){i@variation_index = 0;}
if (s@column_variation=="Pipe"){i@variation_index = 1;}

 

Quote

Glasses, Red, Tiger, Sail, Carrot
Glasses_Gold, Black, Disco, Golden, Pipe

Here is a simple shelf tool that will read the data posted above, examine the forth column, analyze it for expected matches and generate a companion index. It will write the extended CSV file back to disk.

csv_source_file = r"S:\Houdini_Scenes\temp_data_csv.csv"
csv_target_file = r"S:\Houdini_Scenes\temp_data_csv_EXTENDED.csv"

# Read the original source file.
with open(csv_source_file, 'r') as f:
    lines = f.read().splitlines(True)   # Remove slash n character at the end of each line.
f.close()

new_lines = []
for line in lines:
    ary = line.split(",")
    index = -1  #Unknown index default.
    
    # Extend this if list as needed.
    if ary[4].strip()=="Carrot": index = 0
    if ary[4].strip()=="Pipe": index = 1
    
    new_lines.append("%s,%s" % (line.strip(),index))

#Write the new extended CSV file.
f = open(csv_target_file, 'w')
for line in new_lines:
    f.write("%s\n" % line)
f.close()
Quote

Glasses, Red, Tiger, Sail, Carrot,0
Glasses_Gold, Black, Disco, Golden, Pipe,1

You could supply the newly generated CSV to the TableImport node to fetch the fifth column associated switch index value for each line.

Edited by Atom
  • Thanks 1

Share this post


Link to post
Share on other sites

Hey @Atom

Thanks for the script! Wish I knew Phyton!

Not sure if I'm doing something wrong, but I get this error:

DPCquIuAcf.png.8d0d0e38b326c20a3d24af0a980a763d.png

I'm using this:

ZNQNEfXuHT.thumb.png.f6997737f8eb846f1e41d69bab085ce9.png

I tried changing 

 if ary[4].strip()=="Carrot": index = 0 to this if ary[8].strip()=="Carrot": index = 0 since is on the column 8 but not sure if this is correct.

 

Another question, trying to figure this PDG stuff, why when I call '@Mouth' on a "Font" it returns properly "Carrot"

But I can't call it in a wrangle?

56W0mtTeip.thumb.png.e5a0e4fc0c732ec9827b59f8fea31a73.pngyWPhHrTAV7.thumb.png.2daec793074ee0ae2103155a06fa089d.png

If I could call s@Mouth in a wrangle and convert to 0, 1 like you said with:

if (s@Mouth=="Carrot"){i@variation_index = 0;}
if (s@Mouth=="Pipe"){i@variation_index = 1;}

and then figure out the point hscript in a switch, that would be amazing, but I can't either call s@mouth to get an interger, and make the point(../path, value, attrib) work.

Thanks! 

Share this post


Link to post
Share on other sites

Got the data on SOPs working! yeah!, dropped this on a Wrangle (had to be in detail)

s@Mouth = chs("value");

if (s@Mouth=="Pipe"){i@variation_index = 0;}
if (s@Mouth=="Carrot"){i@variation_index = 1;}

fdCSLEkdNj.png.7d18298f6f5db1b53925ea59fc045738.png

YgQZSGgy7S.png.badf9c49a02ad97a82705fd8d47c60c4.png

Next step, call that wrangle variation index in the switch!

Thanks!

Share this post


Link to post
Share on other sites

YES!!!!!!!!!!!!!!!!!!!!!

 

IdtYntrKQv.thumb.gif.7983d6201e083b8c9f1bb83d8977288c.gif

  • Like 1

Share this post


Link to post
Share on other sites

The index is out of range because arrays are zero based. For the 8th column used the 7th index. ary[7].

  • Thanks 1

Share this post


Link to post
Share on other sites
4 hours ago, Atom said:

The index is out of range because arrays are zero based. For the 8th column used the 7th index. ary[7].

Ah stupid me, works now, million thanks sensei @Atom!

Share this post


Link to post
Share on other sites

Last question @Atom

Got the extended CSV from your script working on tableimport.

How can I drop those values in a switch? or use them to pick different geo?

LT87QKnDJX.thumb.png.2dab08b167bbd8fdd92b716dc635d2ec.png

Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×