Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

Scripted RPM of an object - script controller

Scripted RPM of an object - script controller

mk
Explorer Explorer
1,019 Views
5 Replies
Message 1 of 6

Scripted RPM of an object - script controller

mk
Explorer
Explorer

I want to animate the RPM of an machine whitout the use of keys. So i started with an script controller on the  Y Rotation. I added an attribute holder in the stack of the object. And with the parameter editor i added an parameter to set the RPM of the object.

 

The script i used in the script controller of the Y-rotation:

rotationValue = (rpm*360)/1500

F*Degtorad(rotationValue)

 

This worked perfect for me! But now i want the object to gradually start the first 25 frames till it reach the set RPM. So i animated the RPM in the attribute holder. But this gives an strange effect in the rotation of the object.

 

I hope you have an tip how i can solve this problem?

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

leeminardi
Mentor
Mentor

As I understand your goal you would like to have a script control the angle of rotation of an object based on time starting from a stationary orientation and speeding up until it reach a limit value which is held indefinitly.  The expression you now have will increase the rotational speed linearly from frame 0.  At frame 25 you would like the angular rotation to be equal to:

(rpm * 3600 / 1500)

and remain at that speed.  Here, rpm has a fixed value set by the user.

 

In addition to wanting the speed to increase gradually do you also want it to deaccelerate as the speed reaches frame 25?  There are a variety of expressions that could be used to achieve this.  I would consider using a sine function to increase then decrease the rate of change at which the object increases its angular velocity.  This progression is similar to how the tides change in their cycle.  An if statement in the script would use the sine function for the first 25 frames and after that the rotation would be constant (the change in angle per frame would be the same).

 

Is this what you wanr?

lee.minardi
0 Likes
Message 3 of 6

mk
Explorer
Explorer

Tanks for your time!

 

I would like to be able to determine the rpm and then ramp the speed up 0-100 then hold that speed for 1400 frames and then ramp down from 1500-1600.

What happens now is the ramp up works sort of alright and then the constant speed is all good.
The problem is with frame 1501.

If its turning with 5 rpm, the total rotation at 1500 will be 1800 degrees.
At 1501 if the rpm is 4.9 the total rotation is 1765.176

This will lead to a negativ rotation because this will continue until for several frames.

 

Hope you can help me with this problem?

0 Likes
Message 4 of 6

leeminardi
Mentor
Mentor

I used Excel to help me create a script for the rotating object task you posted.  You have 4 events in the rotation.

1. Initial stationary frame (I assume F = 0).

2. Frame at which rotation reaches constant angular velocity ( I assume F=25)

3. Frame at which rotation starts to slow down (F= 85)

4. Frame at which rotation stops (I'll assume an instantaneous stop for this example.)

If the rpm = 100 and there are 30 FPS then the angle displacement per frame after the constant speeed is reached is 20°/sec.  If we assume a gradual speeding up of rotation from rest then we can say:

 angle = a * F^2 where a is the coefficient needed to get to a set angle at the specified frame.

If the shaft rotated from rest at a constant velocity it would reach an angle of 600° by frame 25.  SInce we want to start slowly  we can set a goal of something less than 600 for frame 25. I'll assume 300°.

Therefore:

a = 300/25^2 = 0.48

So, for the first 25 frames we have

angle = 0.48 * F^2

From frame 25 to 85 there's constant speed so the angle increase linearly with the slope of the curve equal to the speed.

After frame 85 the angle remains constant.

The equation gets a little tricky to slow the rotation down graually so for now I will just assume an abrupt stop as you can see in the attached file.

Here's the rotation angle graphed in Excel.

leeminardi_0-1660656980906.png

The script in Max. I've included some format statements to help in the debugging process.  They of can be commented out or deleted.

leeminardi_1-1660657092527.png

degPerSec = rpm *360 / (60 * 30)
format "\nDeg/sec = %" degPerSec
a = 300.0 / 25^2
format "\na = %" a
if (F <= 25) then
(ang = degToRad(a * F^2)
)
else
(
if (F <= 85) then
   (ang = degToRad((F - 25) * degPerSec + 300)
    )
else
(ang = degToRad(60 * degPerSec + 300)
)
)
format "\n\nF = %,   ang = %" F ang
ang

 

As for your statement about "negative" rotation, there is no real positive or negative rotation!  The perception of rotation is a function of the perceived difference from one frame to another. If the teapot were initially pointing straight up (12 o'clock) then rotated to a position of 11 o'clock (i.e., 30° CCW) you would perceive this to be a CCW rotation. However, if the teapot were rotate by 330° CCW thus pointing to 1 o'clock you would perceive it as a clockwise rotation of 30°.   Which is it?  A 30° CW rotation or a CCW rotation of 330°?  Your eyes are more comfortable seeing it as a CW rotation.  The wheels on stage coaches in old western movies sometimes appear to be rotating backwards because of this effect. 

 

 

lee.minardi
0 Likes
Message 5 of 6

rdmLK4HZ
Explorer
Explorer
Accepted solution

Leeminardi thanks for your extensive reply.
The script you suggest is one we also tried to implement. The disadvantage is that you can only start and stop it once. 
Eventually I figured if I could calculate the rotation for each frame and add them together into the total rotation it would solve my problem.

What I did is the following:

- I added a Attribution Holder (AH) to a dummy

- Assigned two floats with the Parameter Editor named: RPM and StartAngle

- In the Motion tab I added a float script controller on the X rotation
- Added 3 variables: Input, rotationPerFrame and totalRotation

- the variable Input is Assigned as Controller to the Custom Attributes

- the script used is:

for i = 0 to F do
(
rotationPerFrame = (at time i (Input.RPM*360/1500))
totalRotation += rotationPerFrame
)


DegTorad(totalRotation + Input.StartAngle)

 

That's it.
If you now make keys for the RPM it will start and stop whenever you want en how many times you want.

 

I hope my explanation is clear enough.

I've attached a max 2018 file with an example

0 Likes
Message 6 of 6

kyronLNJPB
Contributor
Contributor

Thanks so much for laying this out. Exactly what I needed!

Just an addendum for others like me who are complete beginners with scripting, don't forget to create the variables b and x as well in the script controller for the script to work. Had me stumped for 10mins heh.  

0 Likes