Reparenting problem...

hey I have this problem with reparenting, so I have a player class.
Inside the class I have the code that loads and renders my player:

def initiatePlayer(self): # Initiates Player
        PlayerMesh = loader.loadModel(player_path)
        PlayerMesh.reparentTo(render)
        PlayerMesh.setPosHprScale(player_X, player_Y, player_Z, 0, 0, 0, 0.2, 0.2, 0.2)
        print 'Player initiated'

in a second function I have my camera setup but either reparenting to self.PlayerMesh or just PlayerMesh wont work, either “self” has not got the PlayerMesh attribute or it just says it doesnt exist.

def tpsCamera(self): # Initiates Player Camera
        base.disableMouse()
        lens_01 =  base.cam.node().getLens()
        lens_01.setFov(70)
        base.cam.node().setLens(lens_01)
        base.camera.reparentTo(self.PlayerMesh)
        base.camera.setPos(player_X,player_Y +10,2)
        print 'TPS Camera Initiated & Attached'

I couldn’t find my answer anymore in the manual, I guess my experience in python is decreasing since I couldn’t code that much in the exam period.

Probably I made some stupid error…

When you are creating the PlayerMesh variable, you’re making it a local variable - resulting in it going out of scope at the end of the function. So, store it in “self”:

        self.PlayerMesh = loader.loadModel(player_path)
        self.PlayerMesh.reparentTo(render)
        self.PlayerMesh.setPosHprScale(player_X, player_Y, player_Z, 0, 0, 0, 0.2, 0.2, 0.2) 

thanks the camera reparented perfectly :wink: