Jump to content
magneto

How to use string tokens in expressions in parameter fields?

Recommended Posts

Hi,

I have an ordered menu with items that have tokens similar to Sphere SOP's Primitive Type, which have string based tokens. Using them in Switch SOP's using ch() works based on their order in the menu, which is fine.

But I want to use logical comparison inside a parameter field to see if it's a particular value. I tried both indices and the tokens like this, but they don't work:

if(ch("../primType") == "poly", 1, 5)

Thanks :)

Share this post


Link to post
Share on other sites

The indices seem to work fine for me; if(ch("type") == 0, 1 ,0)

parm.hip

  • Like 1

Share this post


Link to post
Share on other sites

Thanks Eetu, I noticed those worked for me as well, but after trying 2 if statements, they seem to stop working:

if(ch("type") == 0, if(ch("type") == 1, 1, 2), 3)

I would have suspected the if statements but I used multiple ifs successfully before. Do you think this is a bug, or am I missing something? :)

Share this post


Link to post
Share on other sites

Yeah, you've mixed up the true and false values in your outer if clause.

if(ch("type") == 2, 2 , if(ch("type") == 1, 1 ,0))

parm2.hip

  • Like 1

Share this post


Link to post
Share on other sites
Guest mantragora

Thanks Eetu, I noticed those worked for me as well, but after trying 2 if statements, they seem to stop working:

if(ch("type") == 0, if(ch("type") == 1, 1, 2), 3)

I would have suspected the if statements but I used multiple ifs successfully before. Do you think this is a bug, or am I missing something? :)

1) What @eetu said.

2) Don't you prefer to do it this way ?

{
    # Come back, come back to me
    comeback = 0;

    # I'll be waiting,
    ptype = ch("../torus1/type");    
    if (ptype == 0) 
    {
        comeback = 0;
    }
    else if (ptype == 1) comeback = 1;
    else comeback = 2;

    # patiently
    return  comeback;
}

Yes, you can write hscript this way.

Edited by mantragora
  • Like 1

Share this post


Link to post
Share on other sites

you can simply use arg() Hscript expression for this

if your type parameter returns values like 0,1,2

then

arg("1 2 3", ch("type"))

will return 1 for ch("type") == 0

2 for ch("type") == 1

3 for ch("type") == 2

so in the space separated string you can choose whatever you want to return for every index instead of huge nested if expression

  • Like 1

Share this post


Link to post
Share on other sites

if(ch("../primType") == "poly", 1, 5)

The "ch" expression always returns a numeric value. You should be using "chs" if you want the parameter to eval to a string result.

If you reference an ordered menu using "ch" it will return the index of the selected item, but if you use "chs" it will return the token string for the menu item.

if(chs("../primType") == "poly", 1, 5)

Edited by hopbin9
  • Like 2

Share this post


Link to post
Share on other sites

Thanks alot guys, very good tricks.

Btw hopbin, I tried using chs and string comparison but it returns 0 no matter what. Did you have success with that? You can try it but I couldn't get the values, but instead 0.

Share this post


Link to post
Share on other sites

Thanks alot guys, very good tricks.

Btw hopbin, I tried using chs and string comparison but it returns 0 no matter what. Did you have success with that? You can try it but I couldn't get the values, but instead 0.

Yea you're right, it appears to be an issue with how hscript handles strings in integer fields. I don't think it's return zero, but rather it's failing to parse and not displaying an error. For example; "string1"=="string1" should eval to 1 but it doesn't.

If you used a Python expression, then I think it would work.

We should report this as a RFE

  • Like 1

Share this post


Link to post
Share on other sites

Hscript doesn't support string comparisons using '=='. You need to use either strcmp() or strmatch() to test 2 strings for being the same.

  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites

Thanks guys, == not working for string surprised me, but thanks Graham for mentioning the string compare functions. I thought they didn't work for this before.

Share this post


Link to post
Share on other sites

i am trying to change the value of Polybevels filletshape (round, solid, crease etc) through HScript, this is how far i got.

if(ch("../thickness")*2 < ch("../basebevel") || ch("../basebevel") < 0.16), "solid", ch("../filletshape"))

it doesnt work though, what am i doing wrong?

 

btw chs does not work for this ordered menu. even when i am pasting relative references from another Polybevel node filletshape menu. i suppose it expects the index of the item?

Share this post


Link to post
Share on other sites

shouldn't "solid" be 1 ?

menu could be

0 - blah

1 - blah blah

2 - blah blah blah

so you use 0, 1 or 2......not blah...coz that just for people to read.....machines want 0, 1 or 2....

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

×