Random value at random time

   10935   17   1
User Avatar
Member
7 posts
Joined: Feb. 2014
Offline
Hey,

I've beens struggling a little bit with this one. I'm trying to get the Global Seed of a scatter to change its value but at random time.

I tried floor(@Frame/5) just change the value every 5 frame for example.

I can't find a way todo the same with random timing.

Can someone help me ? Thanks !
User Avatar
Member
425 posts
Joined: Feb. 2012
Offline
you missing floor function which returns the largest integer less than or equal to a given number

floor(@Frame/5)
Edited by sepu - March 7, 2017 15:06:00
User Avatar
Member
7 posts
Joined: Feb. 2014
Offline
Yes that's what I said. I tried this function but it gives me random number every X frames. But X is a fixed number. I'm trying to get a random value of that.
User Avatar
Member
2042 posts
Joined: Sept. 2015
Offline
Not sure exactly what you want to do…but this will give a a random value everyframe…it could be changed if you only want the change every so many frames or time intervals.

import random
Result = random.random() * 10
return Result
Edited by BabaJ - March 7, 2017 15:36:48
User Avatar
Member
7 posts
Joined: Feb. 2014
Offline
I'll try to be more clear.

I want to animate the Seed value of a scatter.
I don't want it to change every frame (like with $FF) or every N frames (floor(@Frame/N)).

I want my seed to value to change at random interval, like at frames : 10, 15, 18, 45, 53 etc.
User Avatar
Member
2042 posts
Joined: Sept. 2015
Offline
In Python Source Editor:

import hou
import random

Hold_Value = 0
Frames_That_Randomize = []

Number_Of_Changes = int(random.random() * hou.playbar.playbackRange()[1])

def Randomize_Number_Of_Changes(int):
for Count in range(hou.session.Number_Of_Changes):
hou.session.Frames_That_Randomize.append(random.random() * hou.playbar.playbackRange()[1])

Randomize_Number_Of_Changes(Number_Of_Changes)

In Scatters Global Seed Parameter:

import random
Result = hou.session.Hold_Value
Desired_Seed_Range = 5

for Count in range(hou.session.Number_Of_Changes):
if( hou.frame() == int(hou.session.Frames_That_Randomize[Count])):
Result = random.random() * Desired_Seed_Range

hou.session.Hold_Value = Result

return hou.session.Hold_Value

Just be ‘warned’ that the code I'm showing you is basically making a type of ‘global’…sometimes doing things this way can result in having to do a lot of clicking to get your scene to reset at the beginning with values that can be used.

You know this is happening if you only ever get zero in your global seed paramter of the scatter node.

This is the reason I put the default of Result = 0.

And, because you asked for it to be random, everytime you open your file you will get a different range of frames that will randomize - if this is the behaviour you want.

Also, while the file is open and you want it to change with a ‘reset’ when you rewind to the beginning just go into Python Source Editor and click on Reload and Apply then reset on the playbar.

However, if this is too much randomness, you could just make how many frames in total that will end up being randomized, ie. say you only want it to randomize 5 different times although at different times ( random ), you could do that too. I think if you look at the code you should be able to figure out how to do that if that's your wish.

Thanks for the ‘challenge’…I don't think it's the most ‘elagant’…but the best I can do…maybe someone could show a more clever way to do this
User Avatar
Member
1743 posts
Joined: March 2012
Offline
One trick that seems to work: Scatter based on density on a line that represents the frame range, count the points that occur before a certain x value, and use that as the seed.

In the attached example, I put a parameter on a Null SOP that lets you control on average how many frames between the transitions (i.e. 1 over the average number of changes per frame). If the intervals vary a bit too much, you can turn on Relax Iterations on the Scatter SOP scattering on the line to something small.
Edited by neil_math_comp - March 7, 2017 17:30:53

Attachments:
ScatterChangingSeed.hip (71.1 KB)

Writing code for fun and profit since... 2005? Wow, I'm getting old.
https://www.youtube.com/channel/UC_HFmdvpe9U2G3OMNViKMEQ [www.youtube.com]
User Avatar
Member
7 posts
Joined: Feb. 2014
Offline
Huge thanks ! I'll take a look at both of your answer !

ndickson, it seems that you file doesn't work. I don't know why.
User Avatar
Member
1743 posts
Joined: March 2012
Offline
SpaceCraft
ndickson, it seems that you file doesn't work. I don't know why.
Could you be more specific? For example, does the file not load at all, or does the seed not change when you play the scene?

In case it's a licensing issue, here are an Indie (hiplc) and an Apprentice (hipnc) file.

Attachments:
ScatterChangingSeed.hiplc (67.3 KB)
ScatterChangingSeed.hipnc (67.3 KB)

Writing code for fun and profit since... 2005? Wow, I'm getting old.
https://www.youtube.com/channel/UC_HFmdvpe9U2G3OMNViKMEQ [www.youtube.com]
User Avatar
Member
2042 posts
Joined: Sept. 2015
Offline
Yeah..I had a look at your file too ndickson because I was interested in doing it a different way.

In your point wrangle node you don't do anything with P vector assigned in the while loop; Other than breaking when a condition is met. But the end result is that you just keep on looping through the for loop until it's finished with no ‘event’ happening. I'm assuming the P value assigned was meant to be @P or something else and to be used elswhere ?
vector P
is a local variable to the point wrangle and doesn't get used, so why is it being assigned a value?

Seems to me this is where the problem lies.

int nframechanges = `$RFEND-$RFSTART`;
int npointsless = 0;
int npointstotal = npoints(1);
for (int i = 0; i < nframechanges+1; ++i) {
int pt = addpoint(geoself(),{0,0,0});
setpointattrib(geoself(),"seed",pt,npointsless);
while (npointsless < npointstotal) {
vector P = point(1,"P",npointsless);
if (P.x >= i+1) {
break;
}
++npointsless;
}
}
Edited by BabaJ - March 8, 2017 13:40:27
User Avatar
Member
1743 posts
Joined: March 2012
Offline
BabaJ
you don't do anything with P vector assigned in the while loop; Other than breaking when a condition is met.
The purpose of the while loop is to update npointsless. It's just counting the number of points whose x coordinate is less than i+1, (i.e. the number of seed changes that occur before the next frame), so it exits the loop once some P.x is >= i+1. The only attribute written-to by that Attribute Wrangle is “seed”. The positions of the nframechanges+1 added points are not used, so I kept them all at zero. You could set them to set(i,0,0) instead of {0,0,0} if you like, but it's not necessary.

The file works correctly for me in 16.0.539. Does it not work for you?
Writing code for fun and profit since... 2005? Wow, I'm getting old.
https://www.youtube.com/channel/UC_HFmdvpe9U2G3OMNViKMEQ [www.youtube.com]
User Avatar
Member
7 posts
Joined: Feb. 2014
Offline
I'm on H15, not H16. the seed doesn't change at all. I get error when opening the file (I wasn't able to see them before). Apparantly it can't recognize some vex stuff (vex_inplace, vex_selectiongroup, and Mat operator)

In any case, someone helped me to do it with a small wrangle. This is the thing to put in an Attribute Wrangle with Detail mode.

float s = ch(“seed”);
int min = int(ch(“min”));
int max = int(ch(“max”));
float r = rand(s);

int period = int(fit01(r, min, max));
f@seed = 0;
while (@Frame > period) {
f@seed += 1;
r = rand((s+.31456) * f@seed);
period += int(fit01(r, min, max));
}

And you put the f@seed attribute to where you want it. It's not a random value (it just add 1) but it works.

Thanks for the help, I'm still looking at your scene to learn other way to do it !
Edited by SpaceCraft - March 8, 2017 13:55:25
User Avatar
Member
2042 posts
Joined: Sept. 2015
Offline
Ay..my bad…I overlooked the…++npointsless;

…Actually, because I don't typically use the break statement…one of those old school things I picked up long ago where it was suggested using break generally is not considered good coding practice…not that I hold that to be true, but as a beginner just sort of adopted the practice of not using it if I can help it.

As such I forget how break works…looking at your code I assumed wrongly that you were only breaking from the if statement…which is absurd to think that since it is only depending on a single comparison.

Ok..yeah..so I had the default open file from forum set to H15 and it doesn't work. But selected H16 and it does work.

Thanks for this example.
Edited by BabaJ - March 8, 2017 14:08:32
User Avatar
Member
7803 posts
Joined: Sept. 2011
Offline
Why not use CHOPs for something like this? It is straightforward and very controllable with instant feedback. And it won't leave your scene with hidden scripts in your session module that another artist may be stuck with trying to decipher.
Edited by jsmack - March 9, 2017 02:28:03

Attachments:
random_emission_example.zip (57.6 KB)

User Avatar
Member
7 posts
Joined: Feb. 2014
Offline
Thanks Jsmack, I'm not very confortable with Chops yet, I tried to have a curve like this without success earlier.

It does the trick perfectly as well !
User Avatar
Member
2042 posts
Joined: Sept. 2015
Offline
….If your ok with ‘fixed’ random values, instead of chops…

you could just make a simple list in the global seed parameter itself.

No other external sources like chops, vex nodes or like in my original example, python source editor.

Making changes is simple too for more or less frames and values…just delete or copy/paste.
Edited by BabaJ - March 9, 2017 10:11:44

Attachments:
Manual Random List.hiplc (49.6 KB)

User Avatar
Member
7 posts
Joined: Feb. 2014
Offline
Well, in that particular case, since I'm dealing with Seed value, just changing the value, no matter how (random or fixed increment) will give me a random scatter result. So the vex code I gave with fixed random value is doing the trick.

The chop solution give a visual result wich is usefull and I think I should look into chops a bit more.

I thought about doing a manual list, but it needed to be a procedural thing.

thanks for all of your answers
User Avatar
Member
2042 posts
Joined: Sept. 2015
Offline
Manual List not procedural?

The list is defined by the sequence of frames.

Also, it is self contained in the paramater so nodes before and after are also procedural.

But if what you are using with the vex is working as you like…that's all that matters

And yes chops is very good for that. Giving a visual feedback.

It is definitely worthwhile to learn chops…you can actually ‘art-direct’ many different elements just by looking in the MotionFx window with your different channels without ever having to look at your scene view.

And chops doesn't limit you to not using code like expressions or vex. Actually after you get to know chops you will likely combine both.
  • Quick Links