Expression for choosing specific numbers at random

   1237   5   1
User Avatar
Member
7 posts
Joined: Aug. 2019
Offline
Hello,
I'm fairly new to Houdini, and haven't messed with the coding aspect of it.
I'm trying to scatter a bunch of objects and randomly rotate each one individually. I'm close, but I want the objects to be rotated exactly 90 degrees in one direction or the other. The most intuitive way I can imagine to do that is to input an expression in the randomize min/max value parameters that randomly chooses either .5 or -0.5.
Anyone familiar with how I would do this?

Attachments:
help.png (635.6 KB)

User Avatar
Member
34 posts
Joined: Sept. 2013
Offline
tec2
Hello,
I'm fairly new to Houdini, and haven't messed with the coding aspect of it.
I'm trying to scatter a bunch of objects and randomly rotate each one individually. I'm close, but I want the objects to be rotated exactly 90 degrees in one direction or the other. The most intuitive way I can imagine to do that is to input an expression in the randomize min/max value parameters that randomly chooses either .5 or -0.5.
Anyone familiar with how I would do this?


I am not sure if I understood your question correctly, for what I understand you want to rotate some of your objects 90degs up and others 90degs down.If so you can do this by making a selection and manipulating the Normals accordingly .

Attachments:
Obj_Rotate.hip (113.2 KB)

User Avatar
Member
1743 posts
Joined: May 2006
Offline
@orient is a quaternion, a 4-dimensional vector. Don't try and understand the numbers, you'll only give yourself a headache.

The scatter and align sop can do this for you. Under the 'rotation around normals' section you can set a range and a snap value. So if you set the range to 0 and 180, with a snap of 180, then your objects can only face left or right.

The default mode for it is to both scatter the points and setup the orient attribute, but you don't have to. You can scatter points first with a scatter sop, connect it to the second input of scatter and align, and put it into 'add attributes to existing point cloud' mode. This works as before, but I was confused at first when it wouldn't apply any rotation.

The clue is in the section name, 'rotate around normals'. The first case works because it can query the geometry for normals. The second case looks if the scattered points have normals, it doesn't, will give up. If you force normals onto the geometry first with a normals sop, the scatter sop will inherit it, then the scatter and align will work properly.

See attached hip.
Edited by mestela - Sept. 11, 2023 19:00:59

Attachments:
sops_scatter_and_align_face_left_or_right.hip (1.9 MB)
left_right.PNG (270.1 KB)

http://www.tokeru.com/cgwiki [www.tokeru.com]
https://www.patreon.com/mattestela [www.patreon.com]
User Avatar
Member
7 posts
Joined: Aug. 2019
Offline
Thanks for the replies!
Looking back at my original question, I realized that I forgot to say that I'm looking to rotate either 90 or -90 degrees on the z and x axis only. The y axis can have whatever random rotation.
Thinking logically, I would have liked to write a code that: (select random float between 1 and 2 -> round to nearest whole -> set 1 to call a value of 90 and 2 calls a value of -90.)

I will look into the nodes that you mentioned. Definitely seems like a step forwards.
User Avatar
Staff
360 posts
Joined: Feb. 2008
Offline
create a point wrangle and add this code to it:

vector angles = set(rand(@ptnum) > 0.5 ? 90 : -90, rand(@ptnum+123) * 360, rand(@ptnum+456) > 0.5 ? 90 : -90);
matrix3 xform = maketransform(0, angles);
p@orient = quaternion(xform);


if you want some VEX to select a random number from a list of specific numbers, you can do:

float numbers[] = {1.0, 5.0, 3.5, 17, 32, 2.5, 3.14};
@number = numbers[floor(rand(@ptnum) * len(numbers))];

string strings[] = {"hello", "world", "foo", "bar"};
s@string = strings[floor(rand(@ptnum + 123) * len(strings))];

int ints[] = {1,4,7,2,9,24,5};
i@int = ints[floor(rand(@ptnum + 456) * len(ints))];
etc

When selecting a random value between 2 numbers, I find it simpler to do it inline:
@value = rand(@ptnum) > 0.5 ? value1 : value2;

which is the same as
if (@ptnum > 0.5) @value = value1;
else @value = value2;
Edited by npetit - Sept. 11, 2023 23:34:16
User Avatar
Member
7 posts
Joined: Aug. 2019
Offline
Following up on the leads you showed me:
The scatteralign node had the functionality closest to what I was looking for. It might be a little hokey, and I wasn't able to exactly follow the logic I wanted to, but I got something that works:

Create three different point groups that are each rotated differently, avoid overlap with the relax points node (separating them based on their radius), and scattering the meshes among those points.

I'm still curious about the original issue in the title though XD. If anyone knows how to write an expression that can randomly select specific numbers, I'd love to know!
Edited by DG IT - Sept. 11, 2023 22:41:30

Attachments:
solution.png (636.1 KB)

  • Quick Links