(SOLVED)Trouble with Python code - Panda Tutorial

Hello,

I’m making the Panda3d tutorial’s and changing some things.

This is the base code of “Panda3D Manual: Loading and Animating the Panda Model”:

import direct.directbase.DirectStart
from direct.task import Task
from direct.actor import Actor
import math

#Load the first environment model
environ = loader.loadModel("models/environment")
environ.reparentTo(render)
environ.setScale(0.25,0.25,0.25)
environ.setPos(-8,42,0)

#Task to move the camera
def SpinCameraTask(task):
  angledegrees = task.time * 6.0
  angleradians = angledegrees * (math.pi / 180.0)
  base.camera.setPos(20*math.sin(angleradians),-20.0*math.cos(angleradians),3)
  base.camera.setHpr(angledegrees, 0, 0)
  return Task.cont

taskMgr.add(SpinCameraTask, "SpinCameraTask")

#Load the panda actor, and loop its animation
pandaActor = Actor.Actor("models/panda-model",{"walk":"models/panda-walk4"})
pandaActor.setScale(0.005,0.005,0.005)
pandaActor.reparentTo(render)
pandaActor.loop("walk")

run()

Of course, it’s work.

So, I tryed to change some things, nothing big, check the last lines:

import direct.directbase.DirectStart
from direct.task import Task
from direct.actor import Actor
import math

#carregando ambiente
ambiente = loader.loadModel("models/environment")
ambiente.setScale(0.25,0.25,0.25)
ambiente.setPos(-8,42,0)
ambiente.reparentTo(render)    
#fim carrega ambiente

#Task (rotina(?)) para mover a camera
def SpinCameraTask(task):
    angGraus = task.time *6.0
    angRadianos = angGraus * (math.pi / 180.0)
    base.camera.setPos(20.0*math.sin(angRadianos),
                       -20.0*math.cos(angRadianos),
                       3)
    base.camera.setHpr(angGraus,0,0)    
    return Task.cont
#fim Task Camera

taskMgr.add(SpinCameraTask,"CameraTask")

def AddPanda():
    pandaActor = Actor.Actor("models/panda-model",
                             {"walk":"models/panda-walk4"})
    pandaActor.setScale(0.005,0.005,0.005)    
    pandaActor.reparentTo(render)
    pandaActor.loop("walk")
    #print (pandaActor.getCurrentAnim())

AddPanda()
run()

And, of course, something wrong happened: My Actor isn’t walking.

Sorry if i’m making a stupdy question, but: Is the “Panda Actor” object destroyed after the method and I’m rendering only the model? I really dont understood…

Sorry about my poor english.

Thanks and Peace,

Amauri Silva

Yes, the actor is getting garbage collected automatically after the AddPanda function. You need to store it globally so there always exists an instance of it, so it won’t get garbage collected by Python.

Hello,

Thx a lot^^

Here is the modified code, with methods. I’m using a List http://www.python.org/doc/2.5.2/tut/node5.html#SECTION005140000000000000000 to handle the Actor object

import direct.directbase.DirectStart
from direct.task import Task
from direct.actor import Actor
import math

#we need a HERO!
anti_garbage_collector_monster = []

#carregando ambiente
def CarregaAmbiente():
    ambiente = loader.loadModel("models/environment")
    ambiente.setScale(0.25,0.25,0.25)
    ambiente.setPos(-8,42,0)
    ambiente.reparentTo(render)    
#fim carrega ambiente

#Task (rotina(?)) para mover a camera
def SpinCameraTask(task):
    angGraus = task.time *6.0
    angRadianos = angGraus * (math.pi / 180.0)
    base.camera.setPos(20.0*math.sin(angRadianos),
                       -20.0*math.cos(angRadianos),
                       3)
    base.camera.setHpr(angGraus,0,0)
    #print (task.time)
    return Task.cont
#fim Task Camera

def AddPandaActor ():
    pandaActor = Actor.Actor("models/panda-model",
                             {"walk":"models/panda-walk4"})
    pandaActor.setScale(0.005,0.005,0.005)
    pandaActor.loop("walk")
    pandaActor.reparentTo(render)
    anti_garbage_collector_monster.append(pandaActor)
    print (pandaActor.getCurrentAnim())

def InitGame():
    #Adiciona um cenario:
    CarregaAmbiente()
    #Task para rodar a camera:
    taskMgr.add(SpinCameraTask,"CamTask")    
    #add o actor:
    AddPandaActor()

InitGame()
run()

thx again,

Amauri Silva