If Else and such in Vex

   65246   5   0
User Avatar
Member
164 posts
Joined: Feb. 2014
Offline
Hi,

I'd like to know the best way to use If Else type statements in Vex code.

What I'm trying to do is have some particles orbiting a sphere and not going out too far and not getting too close.

In a Pop Attract VEXpression I've used:

if (length(@P) > 1.2) {
forcescale = 5;
}
;

if (length(@P) < 0.9) {
forcescale = -5;
}
;

If I wanted to have lots of conditional statements is this the best way?

I've looked at the Loops and Flows reference for VEXpressions (http://www.sidefx.com/docs/houdini14.0/vex/statement#idm139785337745856 [sidefx.com]) but I'm pretty new to this programming and it's a little hard for me to decipher.

Thanks

Attachments:
If Else Vex.hipnc (254.3 KB)

http://simonfarussell.com [simonfarussell.com]
User Avatar
Member
636 posts
Joined: June 2006
Offline
i would put the length(@P) in to a variable.

float lenP = length(@P);

if (lenP > 1.2) {
forcescale = 5;
}
if (lenP < 0.9) {
forcescale = -5;
}

i don't know if it still today the case but it helped me a long time ago to optimize a adobe flash ressource problem (AS2). make a if statement with bot cases it takes less ressources.


float lenP = length(@P);

if (lenP > 1.2 || lenP < 0.9) {
if (lenP > 1.2)
forcescale = 5;
}
else {
forcescale = -5;
}
}



i'm not a good programmer hope there are much better way's but we will see whats coming up :-)
User Avatar
Member
25 posts
Joined: Oct. 2012
Offline
VEXpressions inside the dop network can't SET values.
forcescale =-5; SETS the value to -5 so it doesn't work.
You can however MODIFY the value. The base value is defined by the parameter in the node. Then you can do:
forcescale -= 5; which subtracts 5 from the base value.
User Avatar
Member
164 posts
Joined: Feb. 2014
Offline
Doodlez
VEXpressions inside the dop network can't SET values.
forcescale =-5; SETS the value to -5 so it doesn't work.
You can however MODIFY the value. The base value is defined by the parameter in the node. Then you can do:
forcescale -= 5; which subtracts 5 from the base value.

Hey,

I think you can set things like force but not position and maybe velocity.
http://simonfarussell.com [simonfarussell.com]
User Avatar
Member
25 posts
Joined: Oct. 2012
Offline
You can take a look at the documentation on this topic here: http://www.sidefx.com/docs/houdini14.0/dopparticles/vexpressions [sidefx.com]

It states: “This is the same VEX expression syntax used in “wrangler” nodes such as the Point Wrangle SOP. However, whereas wranglers affect geometry by directly setting attribute values, particle nodes cannot change attribute values (usually below). Instead, they work by reading parameter and attribute values and then modifying the parameters values accordingly. This lets you change the values of the node's parameters for each particle.”

In your case you can set the forcescale to 1 in the parameter menu and use your old code but just use multiplication instead of directly setting the value:

float lenP = length(@P);

if (lenP > 1.2) {
forcescale *= 5;
}
if (lenP < 0.9) {
forcescale *= -5;
}
User Avatar
Member
8535 posts
Joined: July 2007
Offline
Doodlez
VEXpressions inside the dop network can't SET values.
forcescale =-5; SETS the value to -5 so it doesn't work.
You can however MODIFY the value. The base value is defined by the parameter in the node. Then you can do:
forcescale -= 5; which subtracts 5 from the base value.

setting forcestale to 5 or -5 should work fine
forcescale is not the attribute, but variable containing parameter value, which can be either modified or completely changed by setting directly

and if you really want to be lazy you can just write:
float dist = length(@P);
forcescale = dist<0.9?-5:dist>1.2?5:0;
Tomas Slancik
FX Supervisor
Method Studios, NY
  • Quick Links