forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMopath.py
More file actions
190 lines (166 loc) · 6.85 KB
/
Mopath.py
File metadata and controls
190 lines (166 loc) · 6.85 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
from direct.showbase.DirectObject import DirectObject
from direct.directtools.DirectGeometry import *
from panda3d.core import NodePath, LineSegs
class Mopath(DirectObject):
nameIndex = 1
def __init__(self, name = None, fluid = 1, objectToLoad = None, upVectorNodePath = None, reverseUpVector = False):
if (name == None):
name = 'mopath%d' % self.nameIndex
self.nameIndex = self.nameIndex + 1
self.name = name
self.fluid = fluid
self.tPoint = Point3(0)
self.posPoint = Point3(0)
self.hprPoint = Point3(0)
self.tangentVec = Vec3(0)
self.fFaceForward = 0
self.faceForwardDelta = None
self.faceForwardNode = None
self.timeScale = 1
self.upVectorNodePath = upVectorNodePath
self.reverseUpVector = reverseUpVector
self.reset()
if isinstance( objectToLoad, NodePath ):
self.loadNodePath( objectToLoad )
elif isinstance( objectToLoad, str ):
self.loadFile( objectToLoad )
elif objectToLoad is not None:
print "Mopath: Unable to load object '%s', objectToLoad must be a file name string or a NodePath" % objectToLoad
def getMaxT(self):
return self.maxT * self.timeScale
def loadFile(self, filename, fReset = 1):
nodePath = loader.loadModel(filename)
if nodePath:
self.loadNodePath(nodePath)
nodePath.removeNode()
else:
print 'Mopath: no data in file: %s' % filename
def loadNodePath(self, nodePath, fReset = 1):
if fReset:
self.reset()
self.__extractCurves(nodePath)
if (self.tNurbsCurve != []):
self.maxT = self.tNurbsCurve[-1].getMaxT()
elif (self.xyzNurbsCurve != None):
self.maxT = self.xyzNurbsCurve.getMaxT()
elif (self.hprNurbsCurve != None):
self.maxT = self.hprNurbsCurve.getMaxT()
else:
print 'Mopath: no valid curves in nodePath: %s' % nodePath
def reset(self):
self.maxT = 0.0
self.loop = 0
self.xyzNurbsCurve = None
self.hprNurbsCurve = None
self.tNurbsCurve = []
self.node = None
def __extractCurves(self, nodePath):
node = nodePath.node()
if isinstance(node, ParametricCurve):
if node.getCurveType() == PCTXYZ:
self.xyzNurbsCurve = node
elif node.getCurveType() == PCTHPR:
self.hprNurbsCurve = node
elif node.getCurveType() == PCTNONE:
if (self.xyzNurbsCurve == None):
self.xyzNurbsCurve = node
else:
print 'Mopath: got a PCT_NONE curve and an XYZ Curve in nodePath: %s' % nodePath
elif (node.getCurveType() == PCTT):
self.tNurbsCurve.append(node)
else:
# Iterate over children if any
for child in nodePath.getChildren():
self.__extractCurves(child)
def calcTime(self, tIn):
return self.__calcTime(tIn, self.tNurbsCurve)
def __calcTime(self, tIn, tCurveList):
if tCurveList:
tCurveList[-1].getPoint(tIn, self.tPoint)
return self.__calcTime(self.tPoint[0], tCurveList[:-1])
else:
return tIn
def getFinalState(self):
pos = Point3(0)
if (self.xyzNurbsCurve != None):
self.xyzNurbsCurve.getPoint(self.maxT, pos)
hpr = Point3(0)
if (self.hprNurbsCurve != None):
self.hprNurbsCurve.getPoint(self.maxT, hpr)
return (pos, hpr)
def goTo(self, node, time):
if (self.xyzNurbsCurve == None) and (self.hprNurbsCurve == None):
print 'Mopath: Mopath has no curves'
return
time /= self.timeScale
self.playbackTime = self.calcTime(CLAMP(time, 0.0, self.maxT))
if (self.xyzNurbsCurve != None):
self.xyzNurbsCurve.getPoint(self.playbackTime, self.posPoint)
if self.fluid:
node.setFluidPos(self.posPoint)
else:
node.setPos(self.posPoint)
if (self.hprNurbsCurve != None):
self.hprNurbsCurve.getPoint(self.playbackTime, self.hprPoint)
node.setHpr(self.hprPoint)
elif (self.fFaceForward and (self.xyzNurbsCurve != None)):
if self.faceForwardDelta:
# Look at a point a bit ahead in parametric time.
t = min(self.playbackTime + self.faceForwardDelta, self.xyzNurbsCurve.getMaxT())
lookPoint = Point3()
self.xyzNurbsCurve.getPoint(t, lookPoint)
if self.faceForwardNode:
self.faceForwardNode.setPos(lookPoint)
else:
self.xyzNurbsCurve.getTangent(self.playbackTime, self.tangentVec)
lookPoint = self.posPoint + self.tangentVec
# use the self.upVectorNodePath position if it exists to
# create an up vector for lookAt
if (self.upVectorNodePath is None):
node.lookAt(lookPoint)
else:
if (self.reverseUpVector == False):
node.lookAt(lookPoint,
self.upVectorNodePath.getPos() - self.posPoint)
else:
node.lookAt(lookPoint,
self.posPoint - self.upVectorNodePath.getPos())
def play(self, node, time = 0.0, loop = 0):
if (self.xyzNurbsCurve == None) and (self.hprNurbsCurve == None):
print 'Mopath: Mopath has no curves'
return
self.node = node
self.loop = loop
self.stop()
t = taskMgr.add(self.__playTask, self.name + '-play')
t.currentTime = time
t.lastTime = globalClock.getFrameTime()
def stop(self):
taskMgr.remove(self.name + '-play')
def __playTask(self, task):
time = globalClock.getFrameTime()
dTime = time - task.lastTime
task.lastTime = time
if (self.loop):
cTime = (task.currentTime + dTime) % self.getMaxT()
else:
cTime = task.currentTime + dTime
if ((self.loop == 0) and (cTime > self.getMaxT())):
self.stop()
messenger.send(self.name + '-done')
self.node = None
return task.done
self.goTo(self.node, cTime)
task.currentTime = cTime
return task.cont
def draw(self, subdiv = 1000):
""" Draws a quick and cheesy visualization of the Mopath using
LineSegs. Returns the NodePath representing the drawing. """
ls = LineSegs('mopath')
p = Point3()
for ti in range(subdiv):
t = float(ti) / float(subdiv) * self.maxT
tp = self.calcTime(t)
self.xyzNurbsCurve.getPoint(tp, p)
ls.drawTo(p)
return NodePath(ls.create())