# Random selections with animation nodes

Me again.

Have progressed with my project and now have a group of independent items (quarter-coloured spheres) and now wish to have an animation that rotates spheres at random through one radian.

I've put together animation node trees (started with a loop subprogram before realising the error!) and now have one that responds to the frame position and should select from a random list of my group objects.

I have a node string that tests to see if a sphere has already been rotated (it should skip that object if it has) and still need to adjust frame data to get the frequency of selection/rotation correct.

However, the problem i have is that all spheres rotate in unison, there is no attempt to select them at random. I've tried everything but can't get it to behave - what's the obvious thing i'm missing?

Firstly, huge thanks to Bfoot and Omar for all the effort you've put into my education - just wish i'd been a more worthy student!! This stuff is a real challenge for me.

Attached is a screenshot of my latest node tree: AN array - v2.blend

It works wonderfully with the interpolation i wanted etc - except all spheres rotate at the same time. My difficulty is that the invoke subprogram node seems to be passing the entire list to the subprogram and the rotation calculation then works on all of them at the same time. What i need is for the subprogram to pass a single object to the rotation calcs, they rotate it and then passes on to the next object, and so on.

My inclination is to nest another subprogram with the first so that the first loop passes an object to the second loop for rotating, which then passes back the results to the first loop. The first loop then moves on to the next object and so on through the list. Does that make sense? I've tried all sorts of nesting structures but none of them work.

I know i'm leaning on you guys a lot but i'm almost there and just need the final key.

Thanks again, it's all a massive help.

• You want the spheres to start rotating at random times and stay at their final orientation? Apr 13 '18 at 18:03
• Omar, I think that's what he wants. I can't help him with his rotation issue (comments below) but I believe you could easily. Apr 14 '18 at 10:26

# Euler Mixing

First, I want to tell how to mix between two eulers (Animate one euler into the other). The mixing factor will range between zero and one where zero means the first euler and one means the second euler and any factor in between zero and one is a linear interpolation between the two. It is clear that the factor for our animation will be the time, but lets worry about that later. The equation for the mixing (linearly interpolate) between two eulers $e_1$ and $e_2$ using the factor $\alpha$ is:

$$e = (1-\alpha)e_1 + \alpha e_2$$

Which is easily implemented in Animation Nodes as follows:

Now if I use the output as the rotation of an object and gradually increases the factor, I will get an animation like this where the $y$ value goes from zero to 180 gradually:

Now, if I want to utilize this to create an animation, I would use the current frame as the factor, but since the frame will change from zero to one in $\frac{1}{24}$ of the second, which is very fast. To slow it down, I will divide the the current frame by 24, that way, it will reach the maximum value (1) when the frame number becomes 24, because $\frac{24}{24} = 1$, so you see that the dominator of the division is actually the duration of the animation in frames. I will also add a minimum node to make sure the factor don't exceed one:

Now what if I want the animation to start at a certain frame? To achieve that, we have to make sure that the factor of mixing is zero for all the frames that preceeds the required start frame. To do that, we can subtract the start frame from the current time, making all the frames before it negative, if we then clamp the output to zero, all negative numbers will be zero and we would have achieved what we wanted, here is an example where we set the start frame to be 24:

# Animating Objects

We are now ready to animate the list of objects you have. Lets say we have this setup:

If we want to animate the objects, we will multiply their matrices by a rotation matrix with an animated euler (Euler we computed in the foregoing section), like this:

If we want the animations to start at different frames, all we have to do is subtract those frames just as we learned before:

Since we clamped the factor, the objects will stay at their final orientation after the duration of the animation is finished.

If we want to change the rate of change of the animation, we can evaluate the factor at some interpolation having the same derivative as the one we want, for instance, to create an ease-in-out effect, we can evaluate the factor at a sinusoidal interpolation:

• Omar, that is a really helpful explanation, thank you. I shall have to spend a day or two getting my head around both this and the response from Bfoot, you've both been very supportive. One minor question - on your node set up, how would i ease the rotation both into and out of the motion? Many thanks. Apr 15 '18 at 16:54
• Omar, i've spent a bit of time on this - still more to be done! - but have a quick comment. As mentioned above, i have a random array of objects that i would like to select at random to rotate so that each reveals a separate image. I will not, therefore, be using matrices or object instances as my array is already in place. Just got to work out how to pass the euler data to a loop that selects random objects. Thanks again. Apr 15 '18 at 18:02
• @StephenSamuels I added an extra example for easing. If you already have the objects and are not using matrices, then use the Eulers directly in the Object Transforms Output Node. There is no need for the instancer or the matrices. Is this what you are asking about? Apr 15 '18 at 18:26

You DO need a loop in there somewhere if you are going to rotate the spheres individually and not all at once. One problem you had is you put your output for the rotations from the objects before they were randomly selected.

And if you want to randomly select and change the random selection, you want to put your changing frames into the seed of the random generator.

When I tried the above it seemed to work for me (if that's what you're looking for). To reverse the rotations and start again, just change the Math function under the Already Rotated frame from Add to Subtract.

[EDIT] So without knowing geometry very well I looked at the looping and made a couple of changes (image below).

1. It's a good idea to save initial transformations before manipulating objects through code. This allows you to always get back to your objects original positions.

The animate euler node had an unused slot for time. This was needed in order to figure out how to interpolate the rotation. You had no input so I created a parameter in the loop to pass the frame values. Now when you play the timeline the spheres will rotate 180.

There is still the problem of being jerky. The smooth in/out is being applied to the animation overall the 100 frames. So the first few movements and last few are smooth. But the middle is linear and jerky. I'll look into this.

• Bfoot, again you come to the rescue! I was really struggling with how to combine frame driven processes and list loops. Will need to get my head around what's going on here - like why use the frame count as the random seed? - but you've certainly moved me on. One thing - in order to get 180 degrees of rotation I had to multiply the pi constant by 10! Baffled by that! I also want to ease the rotation and thought sinusoidal on the animate euler node would do it but it just goes haywire. Really appreciate your help with my education - huge thanks! Apr 14 '18 at 9:37
• Here's how I believe most random nodes work. They will produce a random list based on a seed value but it will be the SAME list through out the execution of the node tree. The random function is mathematical and uses the seed to produce the random output. So if you want to have the random list change over time, you need to change the seed and using the frame value is great for that. If the random values are changing too fast (every frame) then place a math node between the frame and random nodes to divide the frames by a value, e.g., frames/10 will change the seed every ten frames. Apr 14 '18 at 10:24
• Thanks Bfoot, really appreciate your support with this. Will wrap the cold towel back around my head and wrestle with it some more!! Apr 15 '18 at 16:56

Well after a bunch more internet searching and head scratching, i've finally got the answer! And it's very simple - just proves there's beauty in economy when it comes to animation nodes, use as few as you can.

Thanks immensely for all the help and support received, it's added greatly to my knowledge even if we didn't get to the right answer.

Final tree shown below: