demoAnimation.py

demoAnimation.py
1 '''
2  Demonstrates simple animation.
3 '''
4 import os
5 import MaxPlus
6 
7 print "Hello World Animation"
8 doRedraw = True
9 ticks = 160
10 
11 class PlaybackSpeed(object):
12  '''Enumerates the allowed values that can be passed to the
13  method Interface::SetPlaybackSpeed(int).'''
14  X1_4 = -4 # quarter time playback speed
15  X1_2 = -2 # half time playback speed
16  X1 = 1 # real time playback speed
17  X2 = 2 # double time playback speed
18  X4 = 4 # quadruple time playback speed
19 
20 def CreateSphere():
21  theSphere = MaxPlus.Factory.CreateGeomObject(MaxPlus.ClassIds.Sphere)
22  node = MaxPlus.Factory.CreateNode(theSphere)
23  return node
24 
25 def PrintInterval(interval):
26  '''This function prints an Interval instance nicely with identifiable values. i.e. 'Interval [0,100]'
27  It prints the values in terms of frames, instead of ticks.
28  Each frame contains 160 ticks. In the SDK, when Intervals are passed around, they
29  are not passed as the number of frames, but instead the frames multiplied by ticks (i.e. 160)'''
30  start = interval.Start() / 160
31  end = interval.End() / 160
32  print "Current Animation Range: [%d,%d]" % (start,end)
33 
34 def SetAnimationRanges():
35  '''Changes the animation range from the default of 100 frames to 200 frames'''
36  anim = MaxPlus.Animation
37  PrintInterval(anim.GetAnimRange())
38  # Intervals come in units of ticks. Each frame is 160 ticks.
39  newFrames = 200 * 160
40  newRange = MaxPlus.Interval(0,newFrames)
41  anim.SetRange(newRange)
42  # The animation slider now shows 200 frames
43  PrintInterval(anim.GetAnimRange())
44 
45 def AnimateTransform(sphere):
46  '''Moves the sphere around to demonstrate animation'''
47  # select the sphere so we will see the keyframes in the timeslider
48  sphere.Select()
49  anim = MaxPlus.Animation
50  # Turn on the AutoKey button
51  anim.SetAnimateButtonState(True)
52 
53  # Now create some keyframes
54  # Advanced the time slider to frame 30
55  anim.SetTime(30*ticks,doRedraw)
56  # Move the sphere, this creates a keyframe
57  sphere.Move( MaxPlus.Point3(50,0,0) )
58 
59  anim.SetTime(60*ticks,doRedraw)
60  sphere.Move( MaxPlus.Point3(50,50,0) )
61 
62  anim.SetTime(90*ticks,doRedraw)
63  sphere.Move( MaxPlus.Point3(-50,50,0) )
64 
65  anim.SetTime(120*ticks,doRedraw)
66  sphere.Move( MaxPlus.Point3(-50,0,0) )
67 
68  anim.SetTime(150*ticks,doRedraw)
69  sphere.Move( MaxPlus.Point3(-50,-50,0) )
70 
71  anim.SetTime(180*ticks,doRedraw)
72  sphere.Move( MaxPlus.Point3(50,-50,0) ) # moves back to the origin
73 
74  # Turn off the AutoKey button
75  anim.SetAnimateButtonState(False)
76 
77 
78 def PlaybackAnimation(sphere):
79  '''There are two StartPlayback methods. One takes a boolean value,
80  and the other takes an integer.
81  The function that takes a boolean value immediately returns.
82  Thus execution is delayed until after the python script is completed.
83  The function that takes an integer (1 for TRUE) does not return
84  until all playback is completed.
85  Attempting to call anim.StartPlayback with the boolean overload
86  will produce confusing results and is best avoided.'''
87  anim = MaxPlus.Animation
88  # The timeslider was last set to 180. But the total time range is 200
89  # This will play back only 20 frames worth of animation and then stop at frame 200
90  print "Playing back Animation first time"
91  anim.SetPlaybackLoop(False)
92  OldStyle = 1
93  # The old style plays back the animation and does not immediately return
94  anim.StartPlayback(OldStyle) # ends at frame 200
95 
96  print "Playing back Animation second time"
97  # now rewind the time slider back to frame 0
98  anim.SetTime(0*ticks,doRedraw)
99  # and now play back the entire sequence from frame 0 to 200.
100  anim.StartPlayback(OldStyle)
101 
102  # Change the playback speed
103  print "Playing back Animation third time"
104  anim.SetTime(0*ticks,doRedraw)
105  anim.SetPlaybackSpeed(PlaybackSpeed.X4)
106  anim.StartPlayback(OldStyle)
107 
108 def DemoAnimation():
110  sphere = CreateSphere()
111  SetAnimationRanges()
112  AnimateTransform(sphere)
113  PlaybackAnimation(sphere)
114 
115 DemoAnimation()