Michael1 Posted February 3, 2009 Share Posted February 3, 2009 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 Quote Link to comment Share on other sites More sharing options...
johner Posted February 3, 2009 Share Posted February 3, 2009 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 Quote Link to comment Share on other sites More sharing options...
Michael1 Posted February 3, 2009 Author Share Posted February 3, 2009 Thanks a lot, I'm reasonable at python, this works great. Quote Link to comment Share on other sites More sharing options...
johner Posted February 4, 2009 Share Posted February 4, 2009 (edited) 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 February 4, 2009 by johner Quote Link to comment Share on other sites More sharing options...
Michael1 Posted February 4, 2009 Author Share Posted February 4, 2009 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 Quote Link to comment Share on other sites More sharing options...
johner Posted February 4, 2009 Share Posted February 4, 2009 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). Quote Link to comment Share on other sites More sharing options...
artzor Posted February 5, 2009 Share Posted February 5, 2009 @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! Quote Link to comment Share on other sites More sharing options...
Michael1 Posted February 5, 2009 Author Share Posted February 5, 2009 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! Quote Link to comment Share on other sites More sharing options...
Michael1 Posted February 5, 2009 Author Share Posted February 5, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.