demoAnimation.py

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