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.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.
    newM = random.choice(["FOO.egg","BAR.egg"])
    m = loader.loadModel(newM)
  angleDegrees = task.time * 16.0
  angleRadians = angleDegrees * (pi / 180.0) * sin(angleRadians), -20.0 * cos(angleRadians), 3), 0, 0)
  return Task.cont
p3dApp.taskMgr.add(moveCam, "moveCam")

Thanks in advance.

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.


Thanks. Both solutions work well for me.