forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionInterval.py
More file actions
445 lines (386 loc) · 15.3 KB
/
FunctionInterval.py
File metadata and controls
445 lines (386 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
"""FunctionInterval module: contains the FunctionInterval class"""
__all__ = ['FunctionInterval', 'EventInterval', 'AcceptInterval', 'IgnoreInterval', 'ParentInterval', 'WrtParentInterval', 'PosInterval', 'HprInterval', 'ScaleInterval', 'PosHprInterval', 'HprScaleInterval', 'PosHprScaleInterval', 'Func', 'Wait']
from panda3d.core import *
from panda3d.direct import *
from direct.showbase.MessengerGlobal import *
from direct.directnotify.DirectNotifyGlobal import directNotify
from . import Interval
#############################################################
### ###
### See examples of function intervals in IntervalTest.py ###
### ###
#############################################################
class FunctionInterval(Interval.Interval):
# Name counter
functionIntervalNum = 1
# Keep a list of function intervals currently in memory for
# Control-C-Control-V redefining. These are just weakrefs so they
# should not cause any leaks.
if __debug__:
import weakref
FunctionIntervals = weakref.WeakKeyDictionary()
@classmethod
def replaceMethod(self, oldFunction, newFunction):
import types
count = 0
for ival in self.FunctionIntervals:
# print 'testing: ', ival.function, oldFunction
# Note: you can only replace methods currently
if type(ival.function) == types.MethodType:
if ival.function.__func__ == oldFunction:
# print 'found: ', ival.function, oldFunction
ival.function = types.MethodType(newFunction,
ival.function.__self__,
ival.function.__self__.__class__)
count += 1
return count
# create FunctionInterval DirectNotify category
notify = directNotify.newCategory('FunctionInterval')
# Class methods
def __init__(self, function, **kw):
"""__init__(function, name = None, openEnded = 1, extraArgs = [])
"""
name = kw.pop('name', None)
openEnded = kw.pop('openEnded', 1)
extraArgs = kw.pop('extraArgs', [])
# Record instance variables
self.function = function
# Create a unique name for the interval if necessary
if name is None:
name = self.makeUniqueName(function)
assert isinstance(name, str)
# Record any arguments
self.extraArgs = extraArgs
self.kw = kw
# Initialize superclass
# Set openEnded true if privInitialize after end time cause interval
# function to be called. If false, privInitialize calls have no effect
# Event, Accept, Ignore intervals default to openEnded = 0
# Parent, Pos, Hpr, etc intervals default to openEnded = 1
Interval.Interval.__init__(self, name, duration = 0.0, openEnded = openEnded)
# For rebinding, let's remember this function interval on the class
if __debug__:
self.FunctionIntervals[self] = 1
@staticmethod
def makeUniqueName(func, suffix = ''):
func_name = getattr(func, '__name__', None)
if func_name is None:
func_name = str(func)
name = 'Func-%s-%d' % (func_name, FunctionInterval.functionIntervalNum)
FunctionInterval.functionIntervalNum += 1
if suffix:
name = '%s-%s' % (name, str(suffix))
return name
def privInstant(self):
# Evaluate the function
self.function(*self.extraArgs, **self.kw)
# Print debug information
self.notify.debug(
'updateFunc() - %s: executing Function' % self.name)
### FunctionInterval subclass for throwing events ###
class EventInterval(FunctionInterval):
# Initialization
def __init__(self, event, sentArgs=[]):
"""__init__(event, sentArgs)
"""
def sendFunc(event = event, sentArgs = sentArgs):
messenger.send(event, sentArgs)
# Create function interval
FunctionInterval.__init__(self, sendFunc, name = event)
### FunctionInterval subclass for accepting hooks ###
class AcceptInterval(FunctionInterval):
# Initialization
def __init__(self, dirObj, event, function, name = None):
"""__init__(dirObj, event, function, name)
"""
def acceptFunc(dirObj = dirObj, event = event, function = function):
dirObj.accept(event, function)
# Determine name
if (name == None):
name = 'Accept-' + event
# Create function interval
FunctionInterval.__init__(self, acceptFunc, name = name)
### FunctionInterval subclass for ignoring events ###
class IgnoreInterval(FunctionInterval):
# Initialization
def __init__(self, dirObj, event, name = None):
"""__init__(dirObj, event, name)
"""
def ignoreFunc(dirObj = dirObj, event = event):
dirObj.ignore(event)
# Determine name
if (name == None):
name = 'Ignore-' + event
# Create function interval
FunctionInterval.__init__(self, ignoreFunc, name = name)
### Function Interval subclass for adjusting scene graph hierarchy ###
class ParentInterval(FunctionInterval):
# ParentInterval counter
parentIntervalNum = 1
# Initialization
def __init__(self, nodePath, parent, name = None):
"""__init__(nodePath, parent, name)
"""
def reparentFunc(nodePath = nodePath, parent = parent):
nodePath.reparentTo(parent)
# Determine name
if (name == None):
name = 'ParentInterval-%d' % ParentInterval.parentIntervalNum
ParentInterval.parentIntervalNum += 1
# Create function interval
FunctionInterval.__init__(self, reparentFunc, name = name)
### Function Interval subclass for adjusting scene graph hierarchy ###
class WrtParentInterval(FunctionInterval):
# WrtParentInterval counter
wrtParentIntervalNum = 1
# Initialization
def __init__(self, nodePath, parent, name = None):
"""__init__(nodePath, parent, name)
"""
def wrtReparentFunc(nodePath = nodePath, parent = parent):
nodePath.wrtReparentTo(parent)
# Determine name
if (name == None):
name = ('WrtParentInterval-%d' %
WrtParentInterval.wrtParentIntervalNum)
WrtParentInterval.wrtParentIntervalNum += 1
# Create function interval
FunctionInterval.__init__(self, wrtReparentFunc, name = name)
### Function Interval subclasses for instantaneous pose changes ###
class PosInterval(FunctionInterval):
# PosInterval counter
posIntervalNum = 1
# Initialization
def __init__(self, nodePath, pos, duration = 0.0,
name = None, other = None):
"""__init__(nodePath, pos, duration, name)
"""
# Create function
def posFunc(np = nodePath, pos = pos, other = other):
if other:
np.setPos(other, pos)
else:
np.setPos(pos)
# Determine name
if (name == None):
name = 'PosInterval-%d' % PosInterval.posIntervalNum
PosInterval.posIntervalNum += 1
# Create function interval
FunctionInterval.__init__(self, posFunc, name = name)
class HprInterval(FunctionInterval):
# HprInterval counter
hprIntervalNum = 1
# Initialization
def __init__(self, nodePath, hpr, duration = 0.0,
name = None, other = None):
"""__init__(nodePath, hpr, duration, name)
"""
# Create function
def hprFunc(np = nodePath, hpr = hpr, other = other):
if other:
np.setHpr(other, hpr)
else:
np.setHpr(hpr)
# Determine name
if (name == None):
name = 'HprInterval-%d' % HprInterval.hprIntervalNum
HprInterval.hprIntervalNum += 1
# Create function interval
FunctionInterval.__init__(self, hprFunc, name = name)
class ScaleInterval(FunctionInterval):
# ScaleInterval counter
scaleIntervalNum = 1
# Initialization
def __init__(self, nodePath, scale, duration = 0.0,
name = None, other = None):
"""__init__(nodePath, scale, duration, name)
"""
# Create function
def scaleFunc(np = nodePath, scale = scale, other = other):
if other:
np.setScale(other, scale)
else:
np.setScale(scale)
# Determine name
if (name == None):
name = 'ScaleInterval-%d' % ScaleInterval.scaleIntervalNum
ScaleInterval.scaleIntervalNum += 1
# Create function interval
FunctionInterval.__init__(self, scaleFunc, name = name)
class PosHprInterval(FunctionInterval):
# PosHprInterval counter
posHprIntervalNum = 1
# Initialization
def __init__(self, nodePath, pos, hpr, duration = 0.0,
name = None, other = None):
"""__init__(nodePath, pos, hpr, duration, name)
"""
# Create function
def posHprFunc(np = nodePath, pos = pos, hpr = hpr, other = other):
if other:
np.setPosHpr(other, pos, hpr)
else:
np.setPosHpr(pos, hpr)
# Determine name
if (name == None):
name = 'PosHprInterval-%d' % PosHprInterval.posHprIntervalNum
PosHprInterval.posHprIntervalNum += 1
# Create function interval
FunctionInterval.__init__(self, posHprFunc, name = name)
class HprScaleInterval(FunctionInterval):
# HprScaleInterval counter
hprScaleIntervalNum = 1
# Initialization
def __init__(self, nodePath, hpr, scale, duration = 0.0,
name = None, other = None):
"""__init__(nodePath, hpr, scale, duration, other, name)
"""
# Create function
def hprScaleFunc(np=nodePath, hpr=hpr, scale=scale,
other = other):
if other:
np.setHprScale(other, hpr, scale)
else:
np.setHprScale(hpr, scale)
# Determine name
if (name == None):
name = ('HprScale-%d' %
HprScaleInterval.hprScaleIntervalNum)
HprScaleInterval.hprScaleIntervalNum += 1
# Create function interval
FunctionInterval.__init__(self, hprScaleFunc, name = name)
class PosHprScaleInterval(FunctionInterval):
# PosHprScaleInterval counter
posHprScaleIntervalNum = 1
# Initialization
def __init__(self, nodePath, pos, hpr, scale, duration = 0.0,
name = None, other = None):
"""__init__(nodePath, pos, hpr, scale, duration, other, name)
"""
# Create function
def posHprScaleFunc(np=nodePath, pos=pos, hpr=hpr, scale=scale,
other = other):
if other:
np.setPosHprScale(other, pos, hpr, scale)
else:
np.setPosHprScale(pos, hpr, scale)
# Determine name
if (name == None):
name = ('PosHprScale-%d' %
PosHprScaleInterval.posHprScaleIntervalNum)
PosHprScaleInterval.posHprScaleIntervalNum += 1
# Create function interval
FunctionInterval.__init__(self, posHprScaleFunc, name = name)
class Func(FunctionInterval):
def __init__(self, *args, **kw):
function = args[0]
assert hasattr(function, '__call__')
extraArgs = args[1:]
kw['extraArgs'] = extraArgs
FunctionInterval.__init__(self, function, **kw)
class Wait(WaitInterval):
def __init__(self, duration):
WaitInterval.__init__(self, duration)
"""
SAMPLE CODE
from IntervalGlobal import *
i1 = Func(base.transitions.fadeOut)
i2 = Func(base.transitions.fadeIn)
def caughtIt():
print 'Caught here-is-an-event'
class DummyAcceptor(DirectObject):
pass
da = DummyAcceptor()
i3 = Func(da.accept, 'here-is-an-event', caughtIt)
i4 = Func(messenger.send, 'here-is-an-event')
i5 = Func(da.ignore, 'here-is-an-event')
# Using a function
def printDone():
print 'done'
i6 = Func(printDone)
# Create track
t1 = Sequence([
# Fade out
(0.0, i1),
# Fade in
(2.0, i2),
# Accept event
(4.0, i3),
# Throw it,
(5.0, i4),
# Ignore event
(6.0, i5),
# Throw event again and see if ignore worked
(7.0, i4),
# Print done
(8.0, i6)], name = 'demo')
# Play track
t1.play()
### Specifying interval start times during track construction ###
# Interval start time can be specified relative to three different points:
# PREVIOUS_END
# PREVIOUS_START
# TRACK_START
startTime = 0.0
def printStart():
global startTime
startTime = globalClock.getFrameTime()
print 'Start'
def printPreviousStart():
global startTime
currTime = globalClock.getFrameTime()
print 'PREVIOUS_END %0.2f' % (currTime - startTime)
def printPreviousEnd():
global startTime
currTime = globalClock.getFrameTime()
print 'PREVIOUS_END %0.2f' % (currTime - startTime)
def printTrackStart():
global startTime
currTime = globalClock.getFrameTime()
print 'TRACK_START %0.2f' % (currTime - startTime)
i1 = Func(printStart)
# Just to take time
i2 = LerpPosInterval(camera, 2.0, Point3(0, 10, 5))
# This will be relative to end of camera move
i3 = FunctionInterval(printPreviousEnd)
# Just to take time
i4 = LerpPosInterval(camera, 2.0, Point3(0, 0, 5))
# This will be relative to the start of the camera move
i5 = FunctionInterval(printPreviousStart)
# This will be relative to track start
i6 = FunctionInterval(printTrackStart)
# Create the track, if you don't specify offset type in tuple it defaults to
# relative to TRACK_START (first entry below)
t2 = Track([(0.0, i1), # i1 start at t = 0, duration = 0.0
(1.0, i2, TRACK_START), # i2 start at t = 1, duration = 2.0
(2.0, i3, PREVIOUS_END), # i3 start at t = 5, duration = 0.0
(1.0, i4, PREVIOUS_END), # i4 start at t = 6, duration = 2.0
(3.0, i5, PREVIOUS_START), # i5 start at t = 9, duration = 0.0
(10.0, i6, TRACK_START)], # i6 start at t = 10, duration = 0.0
name = 'startTimeDemo')
t2.play()
smiley = loader.loadModel('models/misc/smiley')
from direct.actor import Actor
donald = Actor.Actor()
donald.loadModel("phase_6/models/char/donald-wheel-1000")
donald.loadAnims({"steer":"phase_6/models/char/donald-wheel-wheel"})
donald.reparentTo(render)
seq = Sequence(Func(donald.setPos, 0, 0, 0),
donald.actorInterval('steer', duration=1.0),
donald.posInterval(1, Point3(0, 0, 1)),
Parallel(donald.actorInterval('steer', duration=1.0),
donald.posInterval(1, Point3(0, 0, 0)),
),
Wait(1.0),
Func(base.toggleWireframe),
Wait(1.0),
Parallel(donald.actorInterval('steer', duration=1.0),
donald.posInterval(1, Point3(0, 0, -1)),
Sequence(donald.hprInterval(1, Vec3(180, 0, 0)),
donald.hprInterval(1, Vec3(0, 0, 0)),
),
),
Func(base.toggleWireframe),
Func(messenger.send, 'hello'),
)
"""