# Attribute error.

Hi, im currently working on a project in which i simulate planets orbits. I have not got very far as of yet. Spent several hours getting the maths together and got down to coding yesterday. An error was thrown up and i have spent hours both yesterday and today trying to figure out what is wrong. I am more a mathematician than a programmer, so it could be some stupid mistake like syntax.

Here is the code:

``````import direct.directbase.DirectStart
from direct.showbase import DirectObject
from panda3d.core import TextNode, Vec3, Vec4
from direct.interval.IntervalGlobal import *
from direct.gui.DirectGui import *
from direct.showbase.DirectObject import DirectObject
import sys

class mass(DirectObject):
def __init__(self):
base.setBackgroundColor(0, 0, 0)
camera.setPos ( 0, 10, 0 )
camera.setHpr ( 0, -90, 0 )

def createMass(self, name, mass, posX, posY, posZ, velX, velY, velZ):
self.name = name
self.mass = mass
self.posX = posX
self.posY = posY
self.posZ = posZ
self.velX = velX
self.velY = velY
self.velZ = velZ

def scaleRenderTexture(self, scale, tex):
self = loader.loadModel("models/sphere")
self.reparentTo(render)
texture = loader.loadTexture("models/" + tex)
self.setTexture(texture, 1)
self.setPos(self.posX, self.posY, self.posZ)
self.scale(scale)

def calcNextPos(self, x, v, a, dt):
return x + v*dt + 0.5*a*dt^2

def calcNextVel(self, v, a, a2, dt):
return v + 0.5*(a2+a)*dt

def calcA(self, m, a, b):
posA = getpos(a)
posB = getpos(b)
r = posA - posB
return (G*m)/(r^2)

class sun(mass):
def __init__(self):
self.createMass('Sun', 100, 0, 0, 0, 0, 0, 0)
self.scaleRenderTexture(10, 'sun.jpg')

class earth(mass):
def __init__(self):
earth.createMass('Earth', 1, 20, 0, 0, 0, 0, 0)
earth.scaleRenderTexture(1, 'earth.jpg')

sun = sun()
earth = earth()

run()``````

The error that i get is:

Any help people can offer would be much appreciated.

Thanks

Kayne

Sorry, messed up the image url, just copy and paste.

You are replacing the reference to “self”, so self is no longer referring to your instance of the mass/sun/earth class, but to only the model which you have loaded, so it no longer has any of the functions and attributes you have created.

``````   def scaleRenderTexture(self, scale, tex):
self = loader.loadModel("assets/models/box_01")``````

A possible solution is to store your model in an attribute of your class, like this:

``````   def scaleRenderTexture(self, scale, tex):
self.model = loader.loadModel("models/sphere")
self.model.reparentTo(render)
texture = loader.loadTexture("models/" + tex)
self.model.setTexture(texture, 1)
self.model.setPos(self.posX, self.posY, self.posZ)
self.model.setScale(scale)``````

Also one thing to watch out for when making subclasses, remember to call the init function of the class you are making a subclass of, otherwise any code in the init function of the parent class will not be run for the subclass. For example:

``````class mass(DirectObject):
def __init__(self):
DirectObject.__init__(self)
base.setBackgroundColor(0, 0, 0)
camera.setPos ( 0, 10, 0 )
camera.setHpr ( 0, -90, 0 )``````

```class sun(mass): def __init__(self): mass.__init__(self) self.createMass('Sun', 100, 0, 0, 0, 0, 0, 0) self.scaleRenderTexture(10, 'sun.jpg') ```

Wow. Thanks a lot, i really had no idea about half that stuff. Goes to show how much i have to learn.

Thanks

Kayne