How can I make an object rotate back and forth randomly using expressions

How can I make an object rotate back and forth randomly using expressions

e.carter7
Explorer Explorer
2,410 Views
5 Replies
Message 1 of 6

How can I make an object rotate back and forth randomly using expressions

e.carter7
Explorer
Explorer

Hi there,

 

I (maybe stuipidly) decided to try to animate using the expression editor. I would like an object to rotate back and forth between two set points on its axis ideally at random.

 

if(Animation_Control.aniCon == 2){
	int $maxRot = rand(90, 110);
	int $minRot = rand(-10, 10);
	// 1 = postive rotation || 2 = negative
	int $rotDirect = 1;
	if(Drone_Head_Mid_Antener_Pivot.rotateZ <= $maxRot &&  $rotDirect == 1){
		Drone_Head_Mid_Antener_Pivot.rotateZ = time*3;
		
	} else if(Drone_Head_Mid_Antener_Pivot.rotateZ >= $maxRot &&  $rotDirect == 1){
		$rotDirect = 2;
		$maxRot = rand(90, 110);
	}else if(Drone_Head_Mid_Antener_Pivot.rotateZ >= $minRot && $rotDirect == 2){
		Drone_Head_Mid_Antener_Pivot.rotateZ = -time*3;
		
	} else if(Drone_Head_Mid_Antener_Pivot.rotateZ <= $minRot && $rotDirect == 2){
		$rotDirect = 1;
		$minRot = rand(-10, 10);
	}

I tryed to get this fucntionality working using this code. However when this runs it rotates round to to around 100 degrees and then stops doing nothing more. Does anyone have any advice to help with this?

Thanks in advance.

0 Likes
Accepted solutions (1)
2,411 Views
5 Replies
Replies (5)
Message 2 of 6

hamsterHamster
Advisor
Advisor
Accepted solution

Even putting your code into scene made no matters clearer what motion you are really after.

1) first line insists, that the entire code will work for aniCon == 2 - at this stage, irrelevant line;

2) there is no set initial value for the rotZ, occasionally it jumps out of if..elif..else ranges and stop working;

Instead of so many conditions you could use two layers of noise function:

$rnd1 = (noise(time)+1)/2* 100.0 ; // generate smooth noise 0..100
$rnd2 = noise(time+1)* 10.0 ; // generate smooth noise -10..10
Drone_Head_Mid_Antener_Pivot.rotateZ = $rnd1 + $rnd2; // layer both

If you want other wiggle speed, div/mult time param.

 


,,,_°(O__O)°_,,,
Maya2019.1 @ Windows10 & GeForce GTX1080Ti

If the post was helpful, click the
 ACCEPT SOLUTION  button, so others might find it much more easily.

0 Likes
Message 3 of 6

Kahylan
Advisor
Advisor

Hi!

 

It's not quite clear if by "ideally at random" you are referring to the angle between the values it moves or when it moves at all.

If you want to move your object from point 1 to point 2 and back at random intervals, which I assume you want because you don't actually feed your random value into your rotation. You could do something like this:

float $v1 = 50;
float $v2 = -50;
int $switch = rand(0,20);
if ($switch == 0){
Drone_Head_Mid_Antener_Pivot.rotateZ = $v1;
}
else if ($switch == 1){
Drone_Head_Mid_Antener_Pivot.rotateZ = $v2;
}

The higher you set the random range of $switch, the viewer times it will hit a trigger value and move.

 

But honestly, for something like this, both the effect that @hamsterHamster suggested and mine. It is far easier to achieve using an node based setup with an animated noise texture and a setRange node.

Rot_Object_via_Noise.png

 

Using the noises "ratio" attribute to drive the smoothness of the movement and the speed of the animation on the noises "time" attribute to control the frequency of the movement.

 

I hope this helps!

0 Likes
Message 4 of 6

e.carter7
Explorer
Explorer

Hi there,
First thanks both of you for the repsonse and sorry for the confusion caused by my post. I was tierd when I made it and did not include anywere near enough infomation for you to make use of. I had been trying to get this working for a long time at that point and I had reached the end of my rope. 

@hamsterHamster 
Thank you your solution gave me what I was looking for out of this and has helped me a lot. I had hoped my half baked knowledge in coding would help me here. Needless to say it did not. I have one more question. Do you have a link to the doumentation for the noise(and other) parameter? I have tried googling it and often just end up on this fourm again.

@Kahylan 
Thanks for responding, while your solution did not give me exactly what I was looking for it will be useful for what I have planned next. Your other solution looks interesting, how is it achived? Are there any guides I can read about it?

Thanks again both of you. If by accepting the soultion this topic is considered closed please just ignore my quesitons.

Message 5 of 6

hamsterHamster
Advisor
Advisor

Cool.

Those are kind of dumped UX-wise deep into Docs (at least in M2019), under the Useful Functions. 😬

You might wish to change the 2019 in the URL to the number of your version to get the most relevant doc edition.


,,,_°(O__O)°_,,,
Maya2019.1 @ Windows10 & GeForce GTX1080Ti

If the post was helpful, click the
 ACCEPT SOLUTION  button, so others might find it much more easily.

Message 6 of 6

Kahylan
Advisor
Advisor

The node based setup is rather simple. It's something I learned in a rigging class so I don't know any good online documentation, and I didn't find any on the fly right now. But I attached a short video on how I do it and I'll try to explain the basic principle here.

 

How it works is basically that you feed the color value of an animated noise at UV Coordinates (0/0) as your driving attribute. The value is between 0 and 1 (0  = Black, 1 = Black) so you need a setRange node, that stretches that range to what ever your minimum and your maximum point is (my example -60, 60) . The output is the rotation value of your object.

 

This has the advantage over an expression that you can more easily change around your noise attributes to achieve the effect you desire. Also, while the expression you need here is really simple and probably computes as fast as the node based solution, expressions tend to get quite heavy and slow the more you add to them, since it is basically code that you exectue with every frame. As soon as you start adding more features, node based solutions tend to be faster.

 

I hope that made sense^^