I did some experimenting and it looks like it actually passes one list argument, not an argument list where you would use *args.
Modification to Anon’s code:
import direct.directbase.DirectStart
from pandac.PandaModules import *
def myFunction(models):
for model in models:
model.reparentTo(render)
print "Models finished loading"
loader.loadModel(['smiley', 'frowney', 'jack', 'teapot'], callback = myFunction)
run()
Or if you like, here is some uncommented and probably hard to understand code I put together last night with two versions of a “fade and load” function.
asyncLoadWithFade is the asynchronous version in which I used an EventGroup to to ensure things happen in the correct order. stLoadWithFade does it the regular way, but suffers some of the problems discussed above. With many/large models it can lag enough that the second fade is skipped entirely.
from direct.showbase.ShowBase import CardMaker, Vec4
from direct.showbase.EventGroup import EventGroup
def asyncLoadWithFade(filelist):
cm = CardMaker('BlackScreenCard')
cm.setFrameFullscreenQuad()
fsq = render2d.attachNewNode( cm.generate() )
fsq.setColor(Vec4(0,0,0,1))
fsq.setTransparency(1)
fii = fsq.colorScaleInterval(3, Vec4(1,1,1,1), Vec4(1,1,1,0))
foi = fsq.colorScaleInterval(3, Vec4(1,1,1,0), Vec4(1,1,1,1))
fii.setDoneEvent('FadeInDone')
foi.setDoneEvent('FadeOutDone')
base._fadeEG = EventGroup('FadeAndLoad', ('ModelsLoaded', 'FadeInDone'), 'FadeReady')
def dowork():
print "dowork called"
for node in base._nodes:
if node:
node.reparentTo(render)
foi.start()
def cleanup():
print "cleanup called"
base._fadeEG.destroy()
del base._fadeEG
del base._nodes
fsq.removeNode()
def saveNodes(nodes):
print "saveNodes called"
base._nodes = nodes
messenger.send('ModelsLoaded')
base.acceptOnce('FadeReady', dowork)
base.acceptOnce('FadeOutDone', cleanup)
fii.start()
loader.loadModel(filelist, okMissing=True, callback=saveNodes)
def stLoadWithFade(filelist):
cm = CardMaker('BlackScreenCard')
cm.setFrameFullscreenQuad()
fsq = render2d.attachNewNode( cm.generate() )
fsq.setColor(Vec4(0,0,0,1))
fsq.setTransparency(1)
fii = fsq.colorScaleInterval(3, Vec4(1,1,1,1), Vec4(1,1,1,0))
foi = fsq.colorScaleInterval(3, Vec4(1,1,1,0), Vec4(1,1,1,1))
fii.setDoneEvent('FadeInDone')
foi.setDoneEvent('FadeOutDone')
def dowork():
print "dowork called"
for node in loader.loadModel(filelist, okMissing=True):
if node:
node.reparentTo(render)
foi.start()
def cleanup():
print "cleanup called"
fsq.removeNode()
base.acceptOnce('FadeInDone', dowork)
base.acceptOnce('FadeOutDone', cleanup)
fii.start()
Note: This only contains the functions. If you want to try it out you’ll have to write some code to call them.