annotate flax/Timeline.py @ 135:5670f66845ce

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