one more snippset on mopath and simple line drawing.
toss in a few points, call the getNodepath() function and you can use it right away.
please note: motion path with several thousand points will take AGES to load. it’s fine up to a fewhundret but it starts to slow down a lot. i worked around that in the line-class by splitting it into single segments but i wasnt able to get a continous motion path this way. so keep the number of points down. below 500 is usualy fine.
seems like number of points exponentially slow it. using 9000 points you can wait several minuts, while 1000 still load within a few seconds.
it’s based on the person’s code who onec wrote the “river-by-spline” thing unfortunatley his name was nowhere to be found.
anyway. here it is for future generations:
from panda3d.egg import EggData, EggVertexPool, EggVertex, EggGroup, EggLine , loadEggData , EggNurbsCurve
from panda3d.core import Point3D, NodePath
class createNurbsCurve():
def __init__(self):
self.data = EggData()
self.vtxPool = EggVertexPool('mopath')
self.data.addChild(self.vtxPool)
self.eggGroup = EggGroup('group')
self.data.addChild(self.eggGroup)
self.myverts=[]
def addPoint(self,pos):
eggVtx = EggVertex()
eggVtx.setPos(Point3D(pos[0],pos[1],pos[2]))
self.myverts.append(eggVtx)
self.vtxPool.addVertex(eggVtx)
def getNodepath(self):
myCurve=EggNurbsCurve()
myCurve.setup(3, len(self.myverts) +3)
myCurve.setCurveType(1)
for i in self.myverts:
myCurve.addVertex(i)
self.eggGroup.addChild(myCurve)
#self.data.writeEgg(Filename('test.egg'))
return NodePath(loadEggData(self.data))
class createLine():
def __init__(self):
self.data = EggData()
self.vtxPool = EggVertexPool('line')
self.data.addChild(self.vtxPool)
self.eggGroup = EggGroup('group')
self.data.addChild(self.eggGroup)
self.myverts=[]
def addPoint(self,pos):
eggVtx = EggVertex()
eggVtx.setPos(Point3D(pos[0],pos[1],pos[2]))
self.myverts.append(eggVtx)
self.vtxPool.addVertex(eggVtx)
def getNodepath(self):
for i in range(len(self.myverts)):
if not i%500:
if i:
myline.addVertex(self.myverts[i])
print i,len(self.myverts)
myline=EggLine()
self.eggGroup.addChild(myline)
myline.addVertex(self.myverts[i])
#self.data.writeEgg(Filename('test.egg'))
return NodePath(loadEggData(self.data))
##testing
if __name__ == '__main__':
from direct.directbase import DirectStart
from math import sin,cos
from direct.directutil.Mopath import Mopath
from direct.interval.MopathInterval import *
from panda3d.core import NodePath
myCurve=createNurbsCurve()
myLine = createLine()
for i in range(100):
myCurve.addPoint((cos(i/3.),i,sin(i/3.)))
myLine.addPoint((cos(i/3.),i,sin(i/3.)) )
lineNode = myLine.getNodepath()
curveNode = myCurve.getNodepath()
lineNode.reparentTo(render)
myMopath = Mopath()
myMopath.loadNodePath(curveNode)
myMopath.fFaceForward = True
myCube = loader.loadModel("yup-axis")
myCube.reparentTo(render)
myInterval = MopathInterval(myMopath, myCube, duration=10 ,name = "Name")
myInterval.start()
run()
[edit] fixed silly mistake