Model switching

Hi Folks,

I’d like to create an animation of various objects moving around in front of the camera, sometimes changing their identity (loading a new model).

I got a single model to move in front of the cam by just abusing a code example from the tutorial, but I cannot get models to switch within the task loop.

I’ll post the buggy code, maybe you can see what I am trying to do and give me a pointer to the solution:

from math import pi, sin, cos
from direct.showbase.ShowBase import ShowBase
from direct.task import Task 
p3dApp = ShowBase()
m = loader.loadModel("FOO.egg")
m.reparentTo(render)
m.setScale(15.0, 15.0, 15.0)
m.setHpr(m, 90.0,-90.0,0.0)

def moveCam(task):
  if task.time%10 < 0.01:
    scale = m.getScale()
    pos = m.getPos()
    hpr = m.getHpr()
    # Here i want to remove the old m and insert a new one.
    m.detachNode()
    newM = random.choice(["FOO.egg","BAR.egg"])
    m = loader.loadModel(newM)
    m.reparentTo(render)
    m.setPos(pos)
    m.setHpr(hpr)
    m.setScale(scale)
  angleDegrees = task.time * 16.0
  angleRadians = angleDegrees * (pi / 180.0)
  p3dApp.camera.setPos(20 * sin(angleRadians), -20.0 * cos(angleRadians), 3)
  p3dApp.camera.setHpr(angleDegrees, 0, 0)
  return Task.cont
 
p3dApp.taskMgr.add(moveCam, "moveCam")
p3dApp.run()

Thanks in advance.
Best
Robert

The problem is that reassigning m within moveTask doesn’t change the global definition of m, so it’s still the same original m at the next task iteration.

You could solve this with the command “global m” in your moveTask function, but it might make more sense to use a dummy node for this purpose, rather than reassigning m. Call the dummy node d. That way, you can attach the model to d, and remove it and then attach a new model to d later. You don’t have to change d at all, and you have have the task animate d instead of m.

David

Thanks. Both solutions work well for me.