Digistruct Posted September 30, 2016 Share Posted September 30, 2016 Hey guys, I've recently started learning Houdini and after going through a bunch of tutorial hours I wanted to try and create an HDA. Just for practice I decided to do a simple yet effective rock/stone creator. After creating all of my nodes and starting to do my top level parameters I wanted to drive the stones randomization seed through a button. It's just a box, scattered a thousand points and voronoi with cell grouping turned on. Now, what I'm unable to figure out is how I can have a button parameter generate a random number within a range that feeds into the cell group and chooses one of the voronoi'd pieces. I've tried looking for code snippets that could help me but so far I've not been able to produce anything working. Any help would be greatly apprieciated! Thanks a bunch, Jack Quote Link to comment Share on other sites More sharing options...
shawn_kearney Posted October 2, 2016 Share Posted October 2, 2016 (edited) Before I attempt to answer you here, I want you to understand I am not a houdini expert or anything, so there is probably a better way to do this. But the problem intrigues me and was waiting on someone else to come along with a suggestion. You'll never have access to a truly random sequence, it just isn't really possible (or really even necessary). The easiest way to accomplish this would be to simply add one to the previous seed. This could be something really simple, like literally counting by one every time you press the button. I suppose you could store the previous seed into a detail attribute and then just add one when you press the button and feed the result into the seed and re-write the attrib for next time. You could do something more fancy liek using one random number to call on an a position within array of random numbers, but so long as you don't have a a variable that is always changing in the background the seed will always be the same every time you press the button, so there isn't any added benefit to simply adding to the seed. For what you're after, though, this should be sufficient. Typically when people want simple and quick random seeds, they do something like take the current time and convert that into a seed. I am not sure if VEX has access to system time, but python does using the time module. If I'm understanding it correctly and if it will work in Houdini, time.time() should return a unique float every time the button is pressed by counting the time in seconds since the epoch, which is 1/1/1970 on linux systems for example. Be aware that this may not work if the clock is set weird. I'll have to try it out later. Edited October 2, 2016 by shawn_kearney Quote Link to comment Share on other sites More sharing options...
shawn_kearney Posted October 3, 2016 Share Posted October 3, 2016 (edited) Ok. I think I have something here. I am not sure how to use buttons yet, though if you do, you should be able to implement this. I'm sure that there is some better way, but what this will do is execute a python script that gets the system time whenever the control value changes and uses it to generate a random number. This random number is then copied into the input geometry's as a detail attribute. It works by using system time, so no matter what the slider's value you'll always get a unique random number. I can't guarantee I did everything right, so I don't know if it will spontaneously generate, but as far as I can tell, it does work. All you need to do is attach a button to the control attribute within the subnet. ETA - actually, this may not work so well. Every time the file is re-opened, the value changes. random_values.hiplc Edited October 3, 2016 by shawn_kearney Quote Link to comment Share on other sites More sharing options...
Digistruct Posted October 3, 2016 Author Share Posted October 3, 2016 Hey Shawn, You might be no expert, but you clearly know more than I do - so thanks for helping out! I'll try this solution as soon as I'm able and then report back, worst case scenario I'll just keep looking! I never would've imagined this to be as complex as it seems to be haha, but it's always good with challenges! Thanks again, Jack Quote Link to comment Share on other sites More sharing options...
Digistruct Posted October 3, 2016 Author Share Posted October 3, 2016 Tried it out and it worked in the fashion that it gives me a random value back (thanks a bunch)! But it also seems like you were right about it changing at every restart - which is a problem.. . I'll just have to keep googling and scouring through forum posts, I'll let you know if I find any solution! Quote Link to comment Share on other sites More sharing options...
shawn_kearney Posted October 3, 2016 Share Posted October 3, 2016 (edited) The issue is that I don't really know how to save something kind of "outside" the network. So even if you just added one, it would revert to zero once the network is evaluated (essentially the same thing here). You COULD save a file cache between the attrib transfer and the generator script for every time the button is pressed, and reload every time it is released. But that seems like kind of a silly solution to just store a number. If there is any way to save data outside the network then when the button is pressed you could write the random number there and just have the network read it back.... Which, makes me think you could just use random.seed(time.time()) within the callback script using python for the button. Unfortunately, I don't know anything about how to use callback, so I don't know if this would work. Edited October 3, 2016 by shawn_kearney Quote Link to comment Share on other sites More sharing options...
Atom Posted October 4, 2016 Share Posted October 4, 2016 (edited) Here is a line of code you can place in the Callback field of a button. Make a def inside your python node called myButtonClickProcessor. Remember to switch the callback expression language from hScript to Python or this line of code will not work. exec(hou.node("/obj/path_to_my_python_node").parm('python').eval());myButtonClickProcessor() Edited October 4, 2016 by Atom Quote Link to comment Share on other sites More sharing options...
Digistruct Posted October 4, 2016 Author Share Posted October 4, 2016 Huge thanks for the help, guys! 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.