annotate flax/Timeline.py @ 122:2ed9bfd1dd0e

I totally wrecked Timeline so that it can run the show. (I hope it can I totally wrecked Timeline so that it can run the show. (I hope it can at least do that.) Sleepy...
author dmcc
date Fri, 13 Jun 2003 15:06:14 +0000
parents 490843093506
children 41c0ec6cd10a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
1 from TLUtility import make_attributes_from_args, dict_scale, dict_max, \
45b12307c695 Initial revision
drewp
parents:
diff changeset
2 DummyClass, last_less_than, first_greater_than
45b12307c695 Initial revision
drewp
parents:
diff changeset
3 from time import time
45b12307c695 Initial revision
drewp
parents:
diff changeset
4 from __future__ import division # "I'm sending you back to future!"
45b12307c695 Initial revision
drewp
parents:
diff changeset
5
45b12307c695 Initial revision
drewp
parents:
diff changeset
6 """
110
490843093506 all of this stuff is super rough and not well thought out yet.
dmcc
parents: 109
diff changeset
7 Quote of the Build (from Ghostbusters II)
490843093506 all of this stuff is super rough and not well thought out yet.
dmcc
parents: 109
diff changeset
8 Dana: Okay, but after dinner, I don't want you putting any of your old cheap
490843093506 all of this stuff is super rough and not well thought out yet.
dmcc
parents: 109
diff changeset
9 moves on me.
490843093506 all of this stuff is super rough and not well thought out yet.
dmcc
parents: 109
diff changeset
10 Peter: Ohhhh no! I've got all NEW cheap moves.
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
11 """
45b12307c695 Initial revision
drewp
parents:
diff changeset
12
45b12307c695 Initial revision
drewp
parents:
diff changeset
13 class MissingBlender(Exception):
45b12307c695 Initial revision
drewp
parents:
diff changeset
14 """Raised when a TimedEvent is missing a blender."""
45b12307c695 Initial revision
drewp
parents:
diff changeset
15 def __init__(self, timedevent):
45b12307c695 Initial revision
drewp
parents:
diff changeset
16 make_attributes_from_args('timedevent')
45b12307c695 Initial revision
drewp
parents:
diff changeset
17 Exception.__init__(self, "%r is missing a blender." % \
45b12307c695 Initial revision
drewp
parents:
diff changeset
18 self.timedevent)
45b12307c695 Initial revision
drewp
parents:
diff changeset
19
45b12307c695 Initial revision
drewp
parents:
diff changeset
20 # these are chosen so we can multiply by -1 to reverse the direction,
45b12307c695 Initial revision
drewp
parents:
diff changeset
21 # and multiply direction by the time difference to determine new times.
45b12307c695 Initial revision
drewp
parents:
diff changeset
22 FORWARD = 1
45b12307c695 Initial revision
drewp
parents:
diff changeset
23 BACKWARD = -1
45b12307c695 Initial revision
drewp
parents:
diff changeset
24
45b12307c695 Initial revision
drewp
parents:
diff changeset
25 class TimedEvent:
45b12307c695 Initial revision
drewp
parents:
diff changeset
26 """Container for a Frame which includes a time that it occurs at,
45b12307c695 Initial revision
drewp
parents:
diff changeset
27 and which blender occurs after it."""
122
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
28 def __init__(self, time, frame, blender=None, level=1.0):
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
29 make_attributes_from_args('time', 'frame')
45b12307c695 Initial revision
drewp
parents:
diff changeset
30 self.next_blender = blender
122
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
31 self.level = level
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
32 def __float__(self):
45b12307c695 Initial revision
drewp
parents:
diff changeset
33 return self.time
45b12307c695 Initial revision
drewp
parents:
diff changeset
34 def __cmp__(self, other):
45b12307c695 Initial revision
drewp
parents:
diff changeset
35 if type(other) in (float, int):
45b12307c695 Initial revision
drewp
parents:
diff changeset
36 return cmp(self.time, other)
45b12307c695 Initial revision
drewp
parents:
diff changeset
37 else:
45b12307c695 Initial revision
drewp
parents:
diff changeset
38 return cmp(self.time, other.time)
45b12307c695 Initial revision
drewp
parents:
diff changeset
39 def __repr__(self):
122
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
40 return "<TimedEvent %s at %.2f, time=%.2f, next blender=%s>" % \
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
41 (self.frame, self.level, self.time, self.next_blender)
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
42 def get_level(self):
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
43 return self.level
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
44
45b12307c695 Initial revision
drewp
parents:
diff changeset
45 class Blender:
45b12307c695 Initial revision
drewp
parents:
diff changeset
46 """Blenders are functions that merge the effects of two LevelFrames."""
45b12307c695 Initial revision
drewp
parents:
diff changeset
47 def __init__(self):
45b12307c695 Initial revision
drewp
parents:
diff changeset
48 pass
109
ec82e1eea3c8 - initial checkin: run Timeline.py for a good time
dmcc
parents: 0
diff changeset
49 def __call__(self, startframe, endframe, blendtime):
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
50 """Return a LevelFrame combining two LevelFrames (startframe and
45b12307c695 Initial revision
drewp
parents:
diff changeset
51 endframe). blendtime is how much of the blend should be performed
45b12307c695 Initial revision
drewp
parents:
diff changeset
52 and will be expressed as a percentage divided by 100, i.e. a float
109
ec82e1eea3c8 - initial checkin: run Timeline.py for a good time
dmcc
parents: 0
diff changeset
53 between 0.0 and 1.0.
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
54
45b12307c695 Initial revision
drewp
parents:
diff changeset
55 Very important note: Blenders will *not* be asked for values
45b12307c695 Initial revision
drewp
parents:
diff changeset
56 at end points (i.e. blendtime=0.0 and blendtime=1.0).
45b12307c695 Initial revision
drewp
parents:
diff changeset
57 The LevelFrames will be allowed to specify the values at
45b12307c695 Initial revision
drewp
parents:
diff changeset
58 those times. This is unfortunately for implemementation and
45b12307c695 Initial revision
drewp
parents:
diff changeset
59 simplicity purposes. In other words, if we didn't do this,
45b12307c695 Initial revision
drewp
parents:
diff changeset
60 we could have two blenders covering the same point in time and
45b12307c695 Initial revision
drewp
parents:
diff changeset
61 not know which one to ask for the value. Thus, this saves us
45b12307c695 Initial revision
drewp
parents:
diff changeset
62 a lot of messiness with minimal or no sacrifice."""
45b12307c695 Initial revision
drewp
parents:
diff changeset
63 pass
45b12307c695 Initial revision
drewp
parents:
diff changeset
64 def __str__(self):
45b12307c695 Initial revision
drewp
parents:
diff changeset
65 """As a default, we'll just return the name of the class. Subclasses
45b12307c695 Initial revision
drewp
parents:
diff changeset
66 can add parameters on if they want."""
45b12307c695 Initial revision
drewp
parents:
diff changeset
67 return str(self.__class__)
45b12307c695 Initial revision
drewp
parents:
diff changeset
68 def linear_blend(self, startframe, endframe, blendtime):
45b12307c695 Initial revision
drewp
parents:
diff changeset
69 """Utility function to help you produce linear combinations of two
45b12307c695 Initial revision
drewp
parents:
diff changeset
70 blends. blendtime is the percent/100 that the blend should
45b12307c695 Initial revision
drewp
parents:
diff changeset
71 completed. In other words, 0.25 means it should be 0.75 * startframe +
45b12307c695 Initial revision
drewp
parents:
diff changeset
72 0.25 * endframe. This function is included since many blenders are
45b12307c695 Initial revision
drewp
parents:
diff changeset
73 just functions on the percentage and still combine start and end frames
45b12307c695 Initial revision
drewp
parents:
diff changeset
74 in this way."""
122
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
75 return {startframe : (1.0 - blendtime), endframe : blendtime}
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
76
45b12307c695 Initial revision
drewp
parents:
diff changeset
77 class InstantEnd(Blender):
45b12307c695 Initial revision
drewp
parents:
diff changeset
78 """Instant change from startframe to endframe at the end. In other words,
45b12307c695 Initial revision
drewp
parents:
diff changeset
79 the value returned will be the startframe all the way until the very end
45b12307c695 Initial revision
drewp
parents:
diff changeset
80 of the blend."""
109
ec82e1eea3c8 - initial checkin: run Timeline.py for a good time
dmcc
parents: 0
diff changeset
81 def __call__(self, startframe, endframe, blendtime):
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
82 # "What!?" you say, "Why don't you care about blendtime?"
45b12307c695 Initial revision
drewp
parents:
diff changeset
83 # This is because Blenders never be asked for blenders at the endpoints
45b12307c695 Initial revision
drewp
parents:
diff changeset
84 # (after all, they wouldn't be blenders if they were). Please see
45b12307c695 Initial revision
drewp
parents:
diff changeset
85 # 'Very important note' in Blender.__doc__
122
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
86 return {startframe : 1.0}
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
87
45b12307c695 Initial revision
drewp
parents:
diff changeset
88 class InstantStart(Blender):
45b12307c695 Initial revision
drewp
parents:
diff changeset
89 """Instant change from startframe to endframe at the beginning. In other
45b12307c695 Initial revision
drewp
parents:
diff changeset
90 words, the value returned will be the startframe at the very beginning
45b12307c695 Initial revision
drewp
parents:
diff changeset
91 and then be endframe at all times afterwards."""
109
ec82e1eea3c8 - initial checkin: run Timeline.py for a good time
dmcc
parents: 0
diff changeset
92 def __call__(self, startframe, endframe, blendtime):
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
93 # "What!?" you say, "Why don't you care about blendtime?"
45b12307c695 Initial revision
drewp
parents:
diff changeset
94 # This is because Blenders never be asked for blenders at the endpoints
45b12307c695 Initial revision
drewp
parents:
diff changeset
95 # (after all, they wouldn't be blenders if they were). Please see
45b12307c695 Initial revision
drewp
parents:
diff changeset
96 # 'Very important note' in Blender.__doc__
122
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
97 return {endframe : 1.0}
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
98
45b12307c695 Initial revision
drewp
parents:
diff changeset
99 class LinearBlender(Blender):
45b12307c695 Initial revision
drewp
parents:
diff changeset
100 """Linear fade from one frame to another"""
109
ec82e1eea3c8 - initial checkin: run Timeline.py for a good time
dmcc
parents: 0
diff changeset
101 def __call__(self, startframe, endframe, blendtime):
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
102 return self.linear_blend(startframe, endframe, blendtime)
45b12307c695 Initial revision
drewp
parents:
diff changeset
103
45b12307c695 Initial revision
drewp
parents:
diff changeset
104 class ExponentialBlender(Blender):
45b12307c695 Initial revision
drewp
parents:
diff changeset
105 """Exponential fade fron one frame to another. You get to specify
45b12307c695 Initial revision
drewp
parents:
diff changeset
106 the exponent. If my math is correct, exponent=1 means the same thing
45b12307c695 Initial revision
drewp
parents:
diff changeset
107 as LinearBlender."""
45b12307c695 Initial revision
drewp
parents:
diff changeset
108 def __init__(self, exponent):
45b12307c695 Initial revision
drewp
parents:
diff changeset
109 self.exponent = exponent
109
ec82e1eea3c8 - initial checkin: run Timeline.py for a good time
dmcc
parents: 0
diff changeset
110 def __call__(self, startframe, endframe, blendtime):
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
111 blendtime = blendtime ** self.exponent
45b12307c695 Initial revision
drewp
parents:
diff changeset
112 return self.linear_blend(startframe, endframe, blendtime)
45b12307c695 Initial revision
drewp
parents:
diff changeset
113
45b12307c695 Initial revision
drewp
parents:
diff changeset
114 # 17:02:53 drewp: this makes a big difference for the SmoothBlender
45b12307c695 Initial revision
drewp
parents:
diff changeset
115 # (-x*x*(x-1.5)*2) function
45b12307c695 Initial revision
drewp
parents:
diff changeset
116 class SmoothBlender(Blender):
45b12307c695 Initial revision
drewp
parents:
diff changeset
117 """Drew's "Smoove" Blender function. Hopefully he'll document and
45b12307c695 Initial revision
drewp
parents:
diff changeset
118 parametrize it."""
109
ec82e1eea3c8 - initial checkin: run Timeline.py for a good time
dmcc
parents: 0
diff changeset
119 def __call__(self, startframe, endframe, blendtime):
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
120 blendtime = (-1 * blendtime) * blendtime * (blendtime - 1.5) * 2
45b12307c695 Initial revision
drewp
parents:
diff changeset
121 return self.linear_blend(startframe, endframe, blendtime)
45b12307c695 Initial revision
drewp
parents:
diff changeset
122
45b12307c695 Initial revision
drewp
parents:
diff changeset
123 class TimelineTrack:
45b12307c695 Initial revision
drewp
parents:
diff changeset
124 """TimelineTrack is a single track in a Timeline. It consists of a
45b12307c695 Initial revision
drewp
parents:
diff changeset
125 list of TimedEvents and a name. Length is automatically the location
45b12307c695 Initial revision
drewp
parents:
diff changeset
126 of the last TimedEvent. To extend the Timeline past that, add an
122
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
127 EmptyTimedEvent (which doesn't exist :-/)."""
109
ec82e1eea3c8 - initial checkin: run Timeline.py for a good time
dmcc
parents: 0
diff changeset
128 def __init__(self, name, *timedevents):
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
129 self.name = name
109
ec82e1eea3c8 - initial checkin: run Timeline.py for a good time
dmcc
parents: 0
diff changeset
130 self.events = list(timedevents)
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
131 self.events.sort()
45b12307c695 Initial revision
drewp
parents:
diff changeset
132 def __str__(self):
45b12307c695 Initial revision
drewp
parents:
diff changeset
133 return "<TimelineTrack with events: %r>" % self.events
45b12307c695 Initial revision
drewp
parents:
diff changeset
134 def has_events(self):
45b12307c695 Initial revision
drewp
parents:
diff changeset
135 """Whether the TimelineTrack has anything in it. In general,
45b12307c695 Initial revision
drewp
parents:
diff changeset
136 empty level Tracks should be avoided. However, empty function tracks
45b12307c695 Initial revision
drewp
parents:
diff changeset
137 might be common."""
45b12307c695 Initial revision
drewp
parents:
diff changeset
138 return len(self.events)
45b12307c695 Initial revision
drewp
parents:
diff changeset
139 def length(self):
45b12307c695 Initial revision
drewp
parents:
diff changeset
140 """Returns the length of this track in pseudosecond time units.
45b12307c695 Initial revision
drewp
parents:
diff changeset
141 This is done by finding the position of the last TimedEvent."""
45b12307c695 Initial revision
drewp
parents:
diff changeset
142 return float(self.events[-1])
45b12307c695 Initial revision
drewp
parents:
diff changeset
143 def get(self, key, direction=FORWARD):
45b12307c695 Initial revision
drewp
parents:
diff changeset
144 """Returns the event at a specific time key. If there is no event
45b12307c695 Initial revision
drewp
parents:
diff changeset
145 at that time, a search will be performed in direction. Also note
45b12307c695 Initial revision
drewp
parents:
diff changeset
146 that if there are multiple events at one time, only the first will
45b12307c695 Initial revision
drewp
parents:
diff changeset
147 be returned. (Probably first in order of adding.) This is not
45b12307c695 Initial revision
drewp
parents:
diff changeset
148 a problem at the present since this method is intended for LevelFrames,
45b12307c695 Initial revision
drewp
parents:
diff changeset
149 which must exist at unique times."""
45b12307c695 Initial revision
drewp
parents:
diff changeset
150 if direction == BACKWARD:
45b12307c695 Initial revision
drewp
parents:
diff changeset
151 func = last_less_than
45b12307c695 Initial revision
drewp
parents:
diff changeset
152 else:
45b12307c695 Initial revision
drewp
parents:
diff changeset
153 func = first_greater_than
45b12307c695 Initial revision
drewp
parents:
diff changeset
154
45b12307c695 Initial revision
drewp
parents:
diff changeset
155 return func(self.events, key)
45b12307c695 Initial revision
drewp
parents:
diff changeset
156 def get_range(self, i, j, direction=FORWARD):
45b12307c695 Initial revision
drewp
parents:
diff changeset
157 """Returns all events between i and j, exclusively. If direction
45b12307c695 Initial revision
drewp
parents:
diff changeset
158 is FORWARD, j will be included. If direction is BACKWARD, i will
45b12307c695 Initial revision
drewp
parents:
diff changeset
159 be included. This is because this is used to find FunctionFrames
45b12307c695 Initial revision
drewp
parents:
diff changeset
160 and we assume that any function frames at the start point (which
45b12307c695 Initial revision
drewp
parents:
diff changeset
161 could be i or j) have been processed."""
110
490843093506 all of this stuff is super rough and not well thought out yet.
dmcc
parents: 109
diff changeset
162 return [e for e in self.events if e >= i and e <= j]
490843093506 all of this stuff is super rough and not well thought out yet.
dmcc
parents: 109
diff changeset
163
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
164 if direction == FORWARD:
45b12307c695 Initial revision
drewp
parents:
diff changeset
165 return [e for e in self.events if e > i and e <= j]
45b12307c695 Initial revision
drewp
parents:
diff changeset
166 else:
45b12307c695 Initial revision
drewp
parents:
diff changeset
167 return [e for e in self.events if e >= i and e < j]
45b12307c695 Initial revision
drewp
parents:
diff changeset
168 def __getitem__(self, key):
45b12307c695 Initial revision
drewp
parents:
diff changeset
169 """Returns the event at or after a specific time key.
45b12307c695 Initial revision
drewp
parents:
diff changeset
170 For example: timeline[3] will get the first event at time 3.
45b12307c695 Initial revision
drewp
parents:
diff changeset
171
45b12307c695 Initial revision
drewp
parents:
diff changeset
172 If you want to get all events at time 3, you are in trouble, but
45b12307c695 Initial revision
drewp
parents:
diff changeset
173 you could achieve it with something like:
45b12307c695 Initial revision
drewp
parents:
diff changeset
174 timeline.get_range(2.99, 3.01, FORWARD)
45b12307c695 Initial revision
drewp
parents:
diff changeset
175 This is hopefully a bogus problem, since you can't have multiple
45b12307c695 Initial revision
drewp
parents:
diff changeset
176 LevelFrames at the same time."""
45b12307c695 Initial revision
drewp
parents:
diff changeset
177 return self.get(key, direction=FORWARD)
45b12307c695 Initial revision
drewp
parents:
diff changeset
178 def get_surrounding_frames(self, time):
45b12307c695 Initial revision
drewp
parents:
diff changeset
179 """Returns frames before and after a specific time. This returns
45b12307c695 Initial revision
drewp
parents:
diff changeset
180 a 2-tuple: (previousframe, nextframe). If you have chosen the exact
45b12307c695 Initial revision
drewp
parents:
diff changeset
181 time of a frame, it will be both previousframe and nextframe."""
45b12307c695 Initial revision
drewp
parents:
diff changeset
182 return self.get(time, direction=BACKWARD), \
45b12307c695 Initial revision
drewp
parents:
diff changeset
183 self.get(time, direction=FORWARD)
45b12307c695 Initial revision
drewp
parents:
diff changeset
184 def get_levels_at_time(self, time):
45b12307c695 Initial revision
drewp
parents:
diff changeset
185 """Returns a LevelFrame with the levels of this track at that time."""
45b12307c695 Initial revision
drewp
parents:
diff changeset
186 before, after = self.get_surrounding_frames(time)
45b12307c695 Initial revision
drewp
parents:
diff changeset
187
109
ec82e1eea3c8 - initial checkin: run Timeline.py for a good time
dmcc
parents: 0
diff changeset
188 if before == after:
122
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
189 return {before.frame : 1.0}
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
190 else: # we have a blended value
45b12307c695 Initial revision
drewp
parents:
diff changeset
191 diff = after.time - before.time
45b12307c695 Initial revision
drewp
parents:
diff changeset
192 elapsed = time - before.time
45b12307c695 Initial revision
drewp
parents:
diff changeset
193 percent = elapsed / diff
45b12307c695 Initial revision
drewp
parents:
diff changeset
194 if not before.next_blender:
45b12307c695 Initial revision
drewp
parents:
diff changeset
195 raise MissingBlender, before
109
ec82e1eea3c8 - initial checkin: run Timeline.py for a good time
dmcc
parents: 0
diff changeset
196 return before.next_blender(before.frame, after.frame, percent)
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
197
45b12307c695 Initial revision
drewp
parents:
diff changeset
198 class Timeline:
122
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
199 def __init__(self, tracks, rate=1, direction=FORWARD):
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
200 """
122
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
201 Most/all of this is old:
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
202
45b12307c695 Initial revision
drewp
parents:
diff changeset
203 You can have multiple FunctionFrames at the same time. Their
45b12307c695 Initial revision
drewp
parents:
diff changeset
204 order is important though, since FunctionFrames will be applied
45b12307c695 Initial revision
drewp
parents:
diff changeset
205 in the order seen in this list. blenders is a list of Blenders.
45b12307c695 Initial revision
drewp
parents:
diff changeset
206 rate is the rate of playback. If set to 1, 1 unit inside the
45b12307c695 Initial revision
drewp
parents:
diff changeset
207 Timeline will be 1 second. direction is the initial direction.
45b12307c695 Initial revision
drewp
parents:
diff changeset
208 If you want to do have looping, place a LoopFunction at the end of
45b12307c695 Initial revision
drewp
parents:
diff changeset
209 the Timeline. Timelines don't have a set length. Their length
45b12307c695 Initial revision
drewp
parents:
diff changeset
210 is bounded by their last frame. You can put an EmptyFrame at
45b12307c695 Initial revision
drewp
parents:
diff changeset
211 some time if you want to extend a Timeline."""
45b12307c695 Initial revision
drewp
parents:
diff changeset
212
109
ec82e1eea3c8 - initial checkin: run Timeline.py for a good time
dmcc
parents: 0
diff changeset
213 make_attributes_from_args('tracks', 'rate', 'direction')
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
214 self.current_time = 0
45b12307c695 Initial revision
drewp
parents:
diff changeset
215 self.last_clock_time = None
45b12307c695 Initial revision
drewp
parents:
diff changeset
216 self.stopped = 1
45b12307c695 Initial revision
drewp
parents:
diff changeset
217 def length(self):
45b12307c695 Initial revision
drewp
parents:
diff changeset
218 """Length of the timeline in pseudoseconds. This is determined by
45b12307c695 Initial revision
drewp
parents:
diff changeset
219 finding the length of the longest track."""
45b12307c695 Initial revision
drewp
parents:
diff changeset
220 track_lengths = [track.length() for track in self.tracks]
45b12307c695 Initial revision
drewp
parents:
diff changeset
221 return max(track_lengths)
45b12307c695 Initial revision
drewp
parents:
diff changeset
222 def play(self):
45b12307c695 Initial revision
drewp
parents:
diff changeset
223 """Activates the timeline. Future calls to tick() will advance the
45b12307c695 Initial revision
drewp
parents:
diff changeset
224 timeline in the appropriate direction."""
45b12307c695 Initial revision
drewp
parents:
diff changeset
225 self.stopped = 0
45b12307c695 Initial revision
drewp
parents:
diff changeset
226 def stop(self):
45b12307c695 Initial revision
drewp
parents:
diff changeset
227 """The timeline will no longer continue in either direction, no
45b12307c695 Initial revision
drewp
parents:
diff changeset
228 FunctionFrames will be activated."""
45b12307c695 Initial revision
drewp
parents:
diff changeset
229 self.stopped = 1
45b12307c695 Initial revision
drewp
parents:
diff changeset
230 self.last_clock_time = None
45b12307c695 Initial revision
drewp
parents:
diff changeset
231 def reset(self):
45b12307c695 Initial revision
drewp
parents:
diff changeset
232 """Resets the timeline to 0. Does not change the stoppedness of the
45b12307c695 Initial revision
drewp
parents:
diff changeset
233 timeline."""
45b12307c695 Initial revision
drewp
parents:
diff changeset
234 self.current_time = 0
45b12307c695 Initial revision
drewp
parents:
diff changeset
235 def tick(self):
45b12307c695 Initial revision
drewp
parents:
diff changeset
236 """Updates the current_time and runs any FunctionFrames that the cursor
45b12307c695 Initial revision
drewp
parents:
diff changeset
237 passed over. This call is ignored if the timeline is stopped."""
45b12307c695 Initial revision
drewp
parents:
diff changeset
238 if self.stopped:
45b12307c695 Initial revision
drewp
parents:
diff changeset
239 return
45b12307c695 Initial revision
drewp
parents:
diff changeset
240
45b12307c695 Initial revision
drewp
parents:
diff changeset
241 last_time = self.current_time
45b12307c695 Initial revision
drewp
parents:
diff changeset
242 last_clock = self.last_clock_time
45b12307c695 Initial revision
drewp
parents:
diff changeset
243
45b12307c695 Initial revision
drewp
parents:
diff changeset
244 # first, determine new time
45b12307c695 Initial revision
drewp
parents:
diff changeset
245 clock_time = time()
45b12307c695 Initial revision
drewp
parents:
diff changeset
246 if last_clock is None:
45b12307c695 Initial revision
drewp
parents:
diff changeset
247 last_clock = clock_time
45b12307c695 Initial revision
drewp
parents:
diff changeset
248 diff = clock_time - last_clock
45b12307c695 Initial revision
drewp
parents:
diff changeset
249 new_time = (self.direction * self.rate * diff) + last_time
45b12307c695 Initial revision
drewp
parents:
diff changeset
250
45b12307c695 Initial revision
drewp
parents:
diff changeset
251 # update the time
45b12307c695 Initial revision
drewp
parents:
diff changeset
252 self.last_clock_time = clock_time
45b12307c695 Initial revision
drewp
parents:
diff changeset
253 self.current_time = new_time
45b12307c695 Initial revision
drewp
parents:
diff changeset
254
110
490843093506 all of this stuff is super rough and not well thought out yet.
dmcc
parents: 109
diff changeset
255 # now we make sure we're in bounds (we don't do this before, since it
490843093506 all of this stuff is super rough and not well thought out yet.
dmcc
parents: 109
diff changeset
256 # can cause us to skip events that are at boundaries.
490843093506 all of this stuff is super rough and not well thought out yet.
dmcc
parents: 109
diff changeset
257 self.current_time = max(self.current_time, 0)
490843093506 all of this stuff is super rough and not well thought out yet.
dmcc
parents: 109
diff changeset
258 self.current_time = min(self.current_time, self.length())
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
259 def reverse_direction(self):
45b12307c695 Initial revision
drewp
parents:
diff changeset
260 """Reverses the direction of play for this node"""
45b12307c695 Initial revision
drewp
parents:
diff changeset
261 self.direction = self.direction * -1
45b12307c695 Initial revision
drewp
parents:
diff changeset
262 def set_direction(self, direction):
45b12307c695 Initial revision
drewp
parents:
diff changeset
263 """Sets the direction of playback."""
45b12307c695 Initial revision
drewp
parents:
diff changeset
264 self.direction = direction
45b12307c695 Initial revision
drewp
parents:
diff changeset
265 def set_rate(self, new_rate):
45b12307c695 Initial revision
drewp
parents:
diff changeset
266 """Sets the rate of playback"""
45b12307c695 Initial revision
drewp
parents:
diff changeset
267 self.rate = new_rate
45b12307c695 Initial revision
drewp
parents:
diff changeset
268 def set_time(self, new_time):
45b12307c695 Initial revision
drewp
parents:
diff changeset
269 """Set the time to a new time."""
45b12307c695 Initial revision
drewp
parents:
diff changeset
270 self.current_time = new_time
45b12307c695 Initial revision
drewp
parents:
diff changeset
271 def get_levels(self):
45b12307c695 Initial revision
drewp
parents:
diff changeset
272 """Return the current levels from this timeline. This is done by
45b12307c695 Initial revision
drewp
parents:
diff changeset
273 adding all the non-functional tracks together."""
122
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
274 levels = [t.get_levels_at_time(self.current_time)
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
275 for t in self.tracks]
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
276 return dict_max(*levels)
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
277
45b12307c695 Initial revision
drewp
parents:
diff changeset
278 if __name__ == '__main__':
109
ec82e1eea3c8 - initial checkin: run Timeline.py for a good time
dmcc
parents: 0
diff changeset
279 T = TimedEvent
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
280
109
ec82e1eea3c8 - initial checkin: run Timeline.py for a good time
dmcc
parents: 0
diff changeset
281 linear = LinearBlender()
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
282 quad = ExponentialBlender(2)
45b12307c695 Initial revision
drewp
parents:
diff changeset
283 invquad = ExponentialBlender(0.5)
45b12307c695 Initial revision
drewp
parents:
diff changeset
284 smoove = SmoothBlender()
45b12307c695 Initial revision
drewp
parents:
diff changeset
285
109
ec82e1eea3c8 - initial checkin: run Timeline.py for a good time
dmcc
parents: 0
diff changeset
286 track1 = TimelineTrack('lights',
122
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
287 T(0, 'red', blender=linear),
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
288 T(5, 'blue', blender=quad),
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
289 T(10, 'red', blender=smoove),
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
290 T(15, 'blue')) # last TimedEvent doesn't need a blender
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
291
122
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
292 tl = Timeline([track1])
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
293
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
294 tl.play()
45b12307c695 Initial revision
drewp
parents:
diff changeset
295
45b12307c695 Initial revision
drewp
parents:
diff changeset
296 import Tix
45b12307c695 Initial revision
drewp
parents:
diff changeset
297 root = Tix.Tk()
45b12307c695 Initial revision
drewp
parents:
diff changeset
298 colorscalesframe = Tix.Frame(root)
45b12307c695 Initial revision
drewp
parents:
diff changeset
299 scalevars = {}
45b12307c695 Initial revision
drewp
parents:
diff changeset
300 # wow, this works out so well, it's almost like I planned it!
45b12307c695 Initial revision
drewp
parents:
diff changeset
301 # (actually, it's probably just Tk being as cool as it usually is)
45b12307c695 Initial revision
drewp
parents:
diff changeset
302 # ps. if this code ever turns into mainstream code for flax, I'll be
45b12307c695 Initial revision
drewp
parents:
diff changeset
303 # pissed (reason: we need to use classes, not this hacked crap!)
45b12307c695 Initial revision
drewp
parents:
diff changeset
304 colors = 'red', 'blue', 'green', 'yellow', 'purple'
45b12307c695 Initial revision
drewp
parents:
diff changeset
305 for color in colors:
45b12307c695 Initial revision
drewp
parents:
diff changeset
306 sv = Tix.DoubleVar()
45b12307c695 Initial revision
drewp
parents:
diff changeset
307 scalevars[color] = sv
122
2ed9bfd1dd0e I totally wrecked Timeline so that it can run the show. (I hope it can
dmcc
parents: 110
diff changeset
308 scale = Tix.Scale(colorscalesframe, from_=1, to_=0, res=0.01, bg=color,
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
309 variable=sv)
45b12307c695 Initial revision
drewp
parents:
diff changeset
310 scale.pack(side=Tix.LEFT)
45b12307c695 Initial revision
drewp
parents:
diff changeset
311
45b12307c695 Initial revision
drewp
parents:
diff changeset
312 def set_timeline_time(time):
45b12307c695 Initial revision
drewp
parents:
diff changeset
313 tl.set_time(float(time))
45b12307c695 Initial revision
drewp
parents:
diff changeset
314 # print 'set_timeline_time', time
45b12307c695 Initial revision
drewp
parents:
diff changeset
315
45b12307c695 Initial revision
drewp
parents:
diff changeset
316 def update_scales():
45b12307c695 Initial revision
drewp
parents:
diff changeset
317 levels = tl.get_levels()
45b12307c695 Initial revision
drewp
parents:
diff changeset
318 for color in colors:
45b12307c695 Initial revision
drewp
parents:
diff changeset
319 scalevars[color].set(levels.get(color, 0))
45b12307c695 Initial revision
drewp
parents:
diff changeset
320
45b12307c695 Initial revision
drewp
parents:
diff changeset
321 colorscalesframe.pack()
109
ec82e1eea3c8 - initial checkin: run Timeline.py for a good time
dmcc
parents: 0
diff changeset
322 time_scale = Tix.Scale(root, from_=0, to_=track1.length(),
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
323 orient=Tix.HORIZONTAL, res=0.01, command=set_timeline_time)
45b12307c695 Initial revision
drewp
parents:
diff changeset
324 time_scale.pack(side=Tix.BOTTOM, fill=Tix.X, expand=1)
45b12307c695 Initial revision
drewp
parents:
diff changeset
325
45b12307c695 Initial revision
drewp
parents:
diff changeset
326 def play_tl():
45b12307c695 Initial revision
drewp
parents:
diff changeset
327 tl.tick()
45b12307c695 Initial revision
drewp
parents:
diff changeset
328 update_scales()
45b12307c695 Initial revision
drewp
parents:
diff changeset
329 time_scale.set(tl.current_time)
45b12307c695 Initial revision
drewp
parents:
diff changeset
330 # print 'time_scale.set', tl.current_time
45b12307c695 Initial revision
drewp
parents:
diff changeset
331 root.after(10, play_tl)
45b12307c695 Initial revision
drewp
parents:
diff changeset
332
45b12307c695 Initial revision
drewp
parents:
diff changeset
333 controlwindow = Tix.Toplevel()
45b12307c695 Initial revision
drewp
parents:
diff changeset
334 Tix.Button(controlwindow, text='Stop',
45b12307c695 Initial revision
drewp
parents:
diff changeset
335 command=lambda: tl.stop()).pack(side=Tix.LEFT)
45b12307c695 Initial revision
drewp
parents:
diff changeset
336 Tix.Button(controlwindow, text='Play',
45b12307c695 Initial revision
drewp
parents:
diff changeset
337 command=lambda: tl.play()).pack(side=Tix.LEFT)
45b12307c695 Initial revision
drewp
parents:
diff changeset
338 Tix.Button(controlwindow, text='Reset',
45b12307c695 Initial revision
drewp
parents:
diff changeset
339 command=lambda: time_scale.set(0)).pack(side=Tix.LEFT)
45b12307c695 Initial revision
drewp
parents:
diff changeset
340 Tix.Button(controlwindow, text='Flip directions',
45b12307c695 Initial revision
drewp
parents:
diff changeset
341 command=lambda: tl.reverse_direction()).pack(side=Tix.LEFT)
45b12307c695 Initial revision
drewp
parents:
diff changeset
342 Tix.Button(controlwindow, text='1/2x',
45b12307c695 Initial revision
drewp
parents:
diff changeset
343 command=lambda: tl.set_rate(0.5 * tl.rate)).pack(side=Tix.LEFT)
45b12307c695 Initial revision
drewp
parents:
diff changeset
344 Tix.Button(controlwindow, text='2x',
45b12307c695 Initial revision
drewp
parents:
diff changeset
345 command=lambda: tl.set_rate(2 * tl.rate)).pack(side=Tix.LEFT)
45b12307c695 Initial revision
drewp
parents:
diff changeset
346
45b12307c695 Initial revision
drewp
parents:
diff changeset
347 root.after(100, play_tl)
45b12307c695 Initial revision
drewp
parents:
diff changeset
348
45b12307c695 Initial revision
drewp
parents:
diff changeset
349 # Timeline.set_time = trace(Timeline.set_time)
45b12307c695 Initial revision
drewp
parents:
diff changeset
350
45b12307c695 Initial revision
drewp
parents:
diff changeset
351 Tix.mainloop()