Panda3D: name 'self' is not defined || name 'worldNP' is not defined

158 views Asked by At

I was trying to follow Panda3D's documentation towards their Bullet Physics. So far, their tutorial worked good at first, so I wanted to try their code example for character movement. When I replicated and modified their code to my workspace, things did not go well. With the statement, "self.attachNewNode(playerNode)", it tells me that at first, self was not defined. I decided to remove it and get an error that says worldNP is not defined.

Upon investigating about worldNP, I found that no manual declaration was done from Panda3D's documentation. It seems that self.worldNP is predefined in Panda3D, similarly to their base class. I believe that I may have missed some sort of import statement and have an object initialize with worldNP as part of their constructor. In addition, their mentioning from the manual is only towards Panda3D's Bullets Physics.

As for the code, this is as far as I got:

   import direct.directbase.DirectStart
   from panda3d.core import Vec3
   from panda3d.bullet import BulletWorld
   from panda3d.bullet import BulletPlaneShape
   from panda3d.bullet import BulletRigidBodyNode
   from panda3d.bullet import BulletBoxShape
   from panda3d.bullet import BulletCharacterControllerNode
   from panda3d.bullet import BulletCapsuleShape
   from panda3d.bullet import ZUp

   base.cam.setPos(0, -10, 0)
   base.cam.lookAt(0, 0, 0)

   # World
   world = BulletWorld()
   world.setGravity(Vec3(0, 0, -9.81))

   height = 1.75
   radius = 0.4
   shape = BulletCapsuleShape(radius, height - 2*radius, ZUp)

   playerNode = BulletCharacterControllerNode(shape, 0.4, 'Player')
   playerNP = self.worldNP.attachNewNode(playerNode)  # This is where self.worldNP is being mentioned
   playerNP.setPos(-2, 0, 14)
   playerNP.setH(45)
   playerNP.setCollideMask(BitMask32.allOn())

   world.attachCharacter(playerNP.node())

   def update(task):
       dt = globalClock.getDt()
       world.doPhysics(dt)
       world.doPhysics(dt)
       return task.cont

   taskMgr.add(update, 'update')
   base.run()

Note: I understand that 'import direct.directbase.DirectStart' is deprecated. Although, I don't believe this is the culprit for my problem on worldNP.

1

There are 1 answers

0
rdb On

This is a fairly basic question about Python, I do suggest you work through some Python tutorials since the Panda3D manual assumes basic Python knowledge.

The Panda3D manual in that example assumes that you have put your code in a class and defined worldNP as a member thereof, probably containing a node that sits at the root of your scene. In your case, perhaps replacing it with render (the root of the default scene) is sufficient.