Rotation expression requred - if possible

Rotation expression requred - if possible

Anonymous
Not applicable
3,172 Views
17 Replies
Message 1 of 18

Rotation expression requred - if possible

Anonymous
Not applicable

Hi to all - this is part of an overall project in hand at the moment.

I'll simplify the animation problem I have.

I have an object, say a cube, in a scene with 200 frames.
Around the Y axis, I need to have the cube rotate 45 deg clockwise between frames 0 and 60 then rotate 45 degrees anticlockwise between frames 140 and 200. (back to it's starting pos)
The object is stationary between frames 61 and 139.
A 'Y rotation' Float Expression or a Script Expression would be what I am looking for - I can then use the expression to control another object in the scene.
Is this possible?
Thanks for any assistance

Accepted solutions (4)
3,173 Views
17 Replies
Replies (17)
Message 2 of 18

leeminardi
Mentor
Mentor
Accepted solution

The following assumes the in/out transition default is linear.

select $Box001
with animate on
(	
at time 60  rotate $ (angleaxis 45.000 [0,1,0])
at time 140 rotate $ (angleaxis 0.000 [0,1,0])
at time 200 rotate $ (angleaxis -45.000 [0,1,0])
)
lee.minardi
Message 3 of 18

Anonymous
Not applicable

Lee

Yes this worked perfectly. I opened a new Maxscript, pasted your solution, evaluated and it keyed in the frames.

Play just as I had hoped. Thank you

Is it possible to perform this, by adding commands in a Rotational Script.

If too difficult let me know and I will close off as solved

Thanks Lee

 

 

0 Likes
Message 4 of 18

leeminardi
Mentor
Mentor

I don't understand what you mean by "by adding commands in a Rotational Script.".   Please explain where this script would reside.

lee.minardi
0 Likes
Message 5 of 18

Anonymous
Not applicable

Thanks Lee

Selecting The Box
Motion Tab
Parameters
Assign Controller
Transform: Pos Rot Scale
Then I assume you would select the Rotation Euler XYZ, then assign a Transform Script whereby a script would be inserted.

I inserted your commands but they are not not in the right format.

thanks

0 Likes
Message 6 of 18

leeminardi
Mentor
Mentor
Accepted solution

 I think the following expression will do what you want as a Float Expression for the Y rotation of the object.

if(F < 60,degToRad( F / 60. * 45.),if(F<140,degToRad(45),if(F<200,degToRad(-45./60.*F+150),0)))

image.png 

lee.minardi
Message 7 of 18

Anonymous
Not applicable

Lee,

Perfect. 

I didn't know that I could nest the If statements that way.

I have been using various formulas within the Float Expression for ages, without any success.

I will study your solution closely.

Have a top day and thanks for your time and assistance.

Cheers

Angmur

 

0 Likes
Message 8 of 18

leeminardi
Mentor
Mentor

Angmur,

 

You are welcome.

 

I find I use transform scripts more often these days as it gives me more flexibility in defining variables and working with matrices.  You might find these youtube tutorials I made of interest.

Hexapod

Scissor Mechanism

 

Lee

 

lee.minardi
0 Likes
Message 9 of 18

Anonymous
Not applicable

Thanks Lee

Will definitely look at those videos.

In short, I spent some hours working out that expression of yours. Opened up a new world for me

Was stumped on how you got the value of 150 and noticed it only worked for the set angle and frames - I think that the 150 value was he culprit - but no way of knowing.

 

In the end I managed to modify your expression so that any angle, start and stop frame could be added as a value.

- may be improved but it definitely works

 

if(F < RotFrame, degToRad( F / RotFrame * Angle),
if(F < LstFrame, degToRad(Angle),
if(F < BkFrame, degToRad(Angle / (BkFrame - LstFrame) * (BkFrame - F)), 0)))


Angle = (45)
F = Frame Number
RotFrame = Frame where object rotates to  = (60)
BkFrame = Frame at which it rotates back from to 0  = (200)
LstFrame = Last Stationary Frame  = (140)

 

thanking you again Lee

ps was going to upload the max file but can't see how.

Cheers

 

 

 

0 Likes
Message 10 of 18

leeminardi
Mentor
Mentor
Accepted solution

@Anonymous  glad you got it working the way you like and can customize it.

 

You asked where the 150 came from.  If we look at the angle of the box from frame 140 to 200 we have a straight line with an angle or 45 at F=140 and 0 at F=200.  Looking at the equation of a line we have y = mx + b

The slope m for this interval is -45/60

Solving for b we have:

b = y -mx 

substituting when F = 200, angle = 0 we get:

b = 0 - (-45/60) * 200

b = 150 

 

Your third if statement does this. The sign in your equation works out correctly because you have (BkFrame - F) rather than a negative slope and (F - BkFrame)

 

To attach a Max file to a post just drag and drop the file to the "Attachments" section near the end of the Reply to Message screen.

 

lee.minardi
0 Likes
Message 11 of 18

Anonymous
Not applicable

Thanks Lee

much appreciated for your info and time

Cheers

 

0 Likes
Message 12 of 18

denisT.MaxDoctor
Advisor
Advisor

What is the reason to practice with Float expressions if you can do the same with the Float script controller, but much easier? Or is it the idea to use exactly an "old school"? some kind of vintage solution ... 😉

0 Likes
Message 13 of 18

Anonymous
Not applicable

I am well aware that it could have been solved using the float script controller.
At the age of 74+, I am not about to start learning maxscripting. These projects I do are just a passing interest.
Thanks for the lack of encouragement in solving my problem with the knowledge I have.
Yes maybe Old school, but remember the old school instigated the new school you profess to know.

0 Likes
Message 14 of 18

denisT.MaxDoctor
Advisor
Advisor

so the fact of the matter is that in this case the script is much more simple than expression ...

... and I know what I'm saying, because I also programmed in languages ​​such as ALGOL, COBOL, and FORTRAN 😉

0 Likes
Message 15 of 18

denisT.MaxDoctor
Advisor
Advisor

let's do the same using MAX Script and Script controller:

 

delete objects
target = point name:#target box:on cross:on axistripod:on wirecolor:orange
c = target.rotation.controller[3].controller = float_script()
ss = @"z =
(
	if F < 0 or F > 200 then 0
	else if F < 60 then 45.0 * F/60. 
	else if F > 140 then 45.0 * (1 - (F - 140)/60.) 
	else 45
)
degtorad z"

c.setexpression ss

 

all key times and rotation values ​​are hard-coded in this case...
but any expression-driven control makes sense if we need to parameterize our function. so let's do it:

 

delete objects
target = point name:#target box:on cross:on axistripod:on wirecolor:green
c = target.rotation.controller[3].controller = float_script()

c.addconstant #start_time 0f
c.addconstant #end_time 200f
c.addconstant #first_key 60f
c.addconstant #second_key 140f
c.addconstant #angle 45.0

ss = @"z =
(
	if F < start_time or F > end_time then 0
	else if F < first_key then angle * F/(first_key - start_time) 
	else if F > second_key then angle * (1 - (F - second_key)/(end_time - second_key)) 
	else angle
)
degtorad z"

c.setexpression ss

 

I'm not saying this cannot be done with the Float Expression control, but the Script gives you a more intuitive view...

Message 16 of 18

leeminardi
Mentor
Mentor

@Anonymous

Thank you for jumping in here. As a novice Maxscript user I appreciate your input.  I usually don’t look at the Programming forum as I am a novice when it comes to programming in Max but saw this post had not been responded to so I thought I would give it a go.  I too am Old School with my primary programming experience coming from FORTRAN and PDP assembly language, and more recently VBA.

 

The reason I provided an expression controller is because:

  1. That is what the OP asked for.
  2. Only a scalar variable was being calculated.
  3. It is a relatively simple calculation that can be written in one statement with little knowledge of statement syntax and no knowledge of Maxscript.
  4. The function syntax is handily provided via the Function List button in the in the Expression Controller dialog window.
  5. The expression syntax is similar to that in Excel which adds to its ease of understanding.

I disagree that “…in this case the script is much more simple than expression…”.  The first 3 lines of your code

delete objects

target = point name:#target box:on cross:on axistripod:on wirecolor:orange

c = target.rotation.controller[3].controller = float_script()

ss = @"z =

are not obvious to the non-Maxscript write. Why did you start with a delete object? I get the purpose of the target =, and C =, statements but would not know the syntax nor content for them without an example.  

What is the meaning of @”z  =  and then why is c.setexpression ss needed at the end? 

 

I guess because the OP posted his question in the programming forum (rather than the Rigging and Animation forum) there is an assumption of a certain level of programming knowledge with Maxscripts that would make your suggested solution look "simple".  It is simple but requires much more knowledge to write than the expression solution.

 

Again, thank you for your input.  It is appreciated. I've learned some useful features I will be able to use in the future.   

 

lee.minardi
0 Likes
Message 17 of 18

denisT.MaxDoctor
Advisor
Advisor
delete objects
target = point name:#target box:on cross:on axistripod:on wirecolor:orange
c = target.rotation.controller[3].controller = float_script()

 


by this header I am starting a new scene (programmatically) with which we will use as an example. This is standard practice. This part of the code is not related to the controller's expression itself.

 

 


What are the main benefits of using a Script controller vs Expression controller?

A script controller can be advanced at any time, while an expression controller is very limited in functionality. For example, a matrix algebra is problematic for expressions, and more or less supported by MAXScript.

 

Typically, expression controllers are faster than script controllers. Therefore, if expressions are sufficient in functionality and used many many times in the scene, the using of expression controllers is a good idea.

 

0 Likes
Message 18 of 18

Anonymous
Not applicable
Accepted solution

Thank you Lee

 

0 Likes