# Switch sop

## Recommended Posts

I have a simple switch which randomly chooses between its inputs. This is done with a random number(between 0-1) multiplied by the number of inputs. This is good but I'd like to find a way to weight the probability of one or more of the inputs being selected. For example if it has 8 inputs, could the 3rd and 5th inputs be more likely to be chosen. Also making each inputs' probability of being selected easy to update with a simple control would be ideal.

Thanks

##### Share on other sites

I have a simple switch which randomly chooses between its inputs. This is done with a random number(between 0-1) multiplied by the number of inputs. This is good but I'd like to find a way to weight the probability of one or more of the inputs being selected. For example if it has 8 inputs, could the 3rd and 5th inputs be more likely to be chosen. Also making each inputs' probability of being selected easy to update with a simple control would be ideal.

Thanks

I might do this in a Python expression in the "Select Input" parameter and add a bunch of weight parameters to the Switch SOP for each input. Dunno how comfortable you are with Python scripting. I did a quick test that seems to work:

```n = pwd()
ninputs = len(n.inputs())
choices = []
#put index into choices weight number of times
for i in range(ninputs):
pname = 'weight%i' % i
choices += ([i] * n.parm(pname).eval())
#choose one of the list elements
choice = int(n.parm('randomnum').eval() * len(choices))
#return that choice
return choices[choice]```

See attached hip file. There's probably other, non-expression ways of doing it. This could probably be packaged up in an HDA pretty easily, I imagine.

switch_choices.hipnc

##### Share on other sites

Thanks a lot, I'm reasonable at python, this works great.

##### Share on other sites

I have a simple switch which randomly chooses between its inputs. This is done with a random number(between 0-1) multiplied by the number of inputs. This is good but I'd like to find a way to weight the probability of one or more of the inputs being selected. For example if it has 8 inputs, could the 3rd and 5th inputs be more likely to be chosen. Also making each inputs' probability of being selected easy to update with a simple control would be ideal.

In my zeal to crank out some Python code, I forgot to mention the easiest way to do the first part of your question. Just keep your expression the same, and re-connect the inputs you want weighted higher multiple times. That's basically what the code is doing. You don't get the cool sliders, though.

Edited by johner
##### Share on other sites

To ask a very basic question, after trying to implement it into my project I found that houdini doesn't seem to be recognizing the python. Highlighting much of the code in red etc. I'm guessing there's an option that needs to be turned on somewhere.

Cheers

##### Share on other sites

To ask a very basic question, after trying to implement it into my project I found that houdini doesn't seem to be recognizing the python. Highlighting much of the code in red etc. I'm guessing there's an option that needs to be turned on somewhere.

Cheers

You should be able to right-click on the parameter, choose the Expression menu, then Change Language to Python. You could also use the "H" menu in the upper right hand corner of the Parameter pane to change the default language to Python for that node, but you may want to leave it in HScript if you'll be writing things like "rand(\$F)" in the random # parameter in the example. HScript expressions still seem to be simpler / faster for little one-liners that use variables like that.

It will still show up in red, I believe, which just means that that particular expression is in a different language than the default for that node (I think).

##### Share on other sites

@Michael1,

could you explain your first step to me? i've been trying to do something similair (random textures on instanced planes)

where did you put the "rand" and how does the switch read it?

thanks!

##### Share on other sites

I'll try and explain a much simplified version of my scene. For this I'm using the stamp function in the copy sop, if you haven't used this there's a tutorial on the sidefx website. http://www.sidefx.com/index.php?option=com...&Itemid=132 which is very useful for all sorts of things

So my simplified scene is this:

I have a grid, after this I have a copy node. The grid is plugged into the right hand input of the copy node so its a dotted line. Into the left hand input to the copy is a switch node. The switch node can now have lots of different inputs, for example mine has a sphere, box and tube.

What I now want it to do is randomly select either the sphere, box or tube and copy them onto each point of the grid.

To do this in the copy sop go to the Stamp folder. Tick "stamp inputs", in variable 1 write "randnum" and in value 1 write "rand(\$PT)".

Now go to the switch node, in the Select Input write " stamp("../copy1","randnum",0)"

Click the display/render on the copy node, you should have something like shown in the pictures below.

\$PT is the point number for each point on the grid, this ensures that the random function uses a different number each time round in radn(\$PT).

Similarly you could use \$CY as shown in the creep example above, hope that helps.

@Michael1,

could you explain your first step to me? i've been trying to do something similair (random textures on instanced planes)

where did you put the "rand" and how does the switch read it?

thanks!

##### Share on other sites

sorry inside the switch node, the Select Input reads : stamp("../copy1","randnum",0)*opninputs(".")

The first part returns a random number from the copy node between 0-1. And opninputs(".") is the number of inputs plugged into the switched, e.g. 3 in this case.

## 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.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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

×
×