Get position of object

I’m am trying to create asteroids that head toward the Earth, but I am having difficulty getting the position of the Earth. Here are some parts of my code:

class SinglePlayer(DirectObject.DirectObject):
    # constructor
    def __init__(self, name, main):
        self.main = main
        #Load Earth
        earthnode=Planet(image="models/earth_1k_tex.jpg", pos=Vec3(2.0* ORBITSCALE, 0, 0), name = "earth", scale = (20 * SIZESCALE), parent=orbit_root_earth, day=(2*DAYSCALE), year=(YEARSCALE), environment=self)
class Planet(DirectObject.DirectObject):
    def __init__(self, image, pos, name, scale, parent, day, year, environment):
        self.environment = environment
        self.parent = parent
        self.planet = loader.loadModelCopy("models/planet_sphere")
        self.planet_tex = loader.loadTexture(image)
        self.planet.setTexture(self.planet_tex, 1)
        self.planet.setScale(scale) = name
class Asteroid(DirectObject.DirectObject):
    def __init__(self, pos = None, environment=None, speed=0):
        model = "models/asteroid1.egg"
        self.env = environment
        self.asteroid = loader.loadModel(model)
        myNormalMapTex = loader.loadTexture('models/craternormal.png') 
        self.asteroid.setTexture(myNormalMapTex, 0)
       #make asteroid head toward earth
        self.loop = self.asteroid.posInterval(20.0,earthnode.getPos()) #Vec3(0,0,0)

When I run the program, it crashes and says ‘earthnode’ is undefined.

earthnode is not defined in the scope of Asteroid.init() It doesn’t know what you mean by “earthnode” at that point. It looks like it’s only defined in SinglePlayer.init() (I’m assuming the code formatting screwed up and you’re not actually trying to create earthnode as a module level variable) When SinglePlayer.init() finishes, the local earthnode reference is lost. It looks like you have another reference to the actual object in the “planets” list. Perhaps you could look it up through there.

Try reading up on python scope rules if you’re still confused.