rookie mistake

Hi. blown away by panda3d’s potential. just awesome. and its great for me to use while getting to grips with python. been a very fun month.

anyway, i am having a little difficulty creating a range of cubes (linked to bullet nodes) using a loop and then referencing them after creating them.

he creating i can do, but how do i reference these cube and do things like getPos/setPos them using a for loop?

even when i try and print them using the loop i just get none, none, none, etc

any help or direction appreciated :slight_smile:

the code snips i am using are:

 def makeBlocks(self):
          numBlocks = 5 + int(random.random()*5)
              for i in range(numBlocks):
              self.blocks.append(self.makeCube(i))
          for i in range(numBlocks):
              print (self.blocks)
             

and the code for makeCube(i) is

def makeCube(self,num):

          shape = BulletBoxShape(Vec3(0.5,0.5,0.5))
          nameNP = self.worldNP.attachNewNode(BulletRigidBodyNode("cubeX"))
          nameNP.node().setMass(1.0)
          nameNP.node().addShape(shape)
          nameNP.setPos(-4+(random.random()*8), -1,10)
          self.world.attachRigidBody(nameNP.node())
          nameCube = loader.loadModel("box.egg")
          nameCube.reparentTo(nameNP)
          nameCube.setColor(random.random(),random.random(),random.random())
          newTexture = loader.loadTexture("whiteTexture.png")
          nameCube.setTexture(newTexture,1)
          nameCube.setPos(-0.5,-0.5,-0.5)
          nameNP.setScale(0.5)

I believe your problem is that makeCube isn’t returning anything. Try adding

return nameNP

as the last line of the function.

After that your print loop should be fine, and you should also be able to do things like

self.blocks[0].getPos()

Cheers,
–jonah

thanks for the replies.

Johan that solves the problem.

Luna, it’s there inside the 1st loop. self.blocks.append(self.makeCube(i))