Name "self" is not defined

hi there once again.

i have to tell that i’m a little bit scare to post an other topic for nothing, because my issue was simple to correct, but it’s been a quite while (3 days in fact) i try to correct the folowing script (massive comment deleted)

import direct.directbase.DirectStart
from panda3d.core import Lerp
from panda3d.core import AmbientLight, DirectionalLight, LightAttrib
from panda3d.core import NodePath
from panda3d.core import Vec3, Vec4
from direct.interval.IntervalGlobal import *           #needed interval use
from direct.gui.DirectGui import *

#Importing math
from math import pi, sin

class World:
    def __init__(self):
        self.title = OnscreenText(text="Python Powered",
                                  style=1, fg=(1,1,1,1),
                                  pos=(0.87, -0.95), scale = .07)
        base.setBackgroundColor(.6, .6, 1) #setting back ground color
        base.disableMouse()                #disable all mouse
        camera.setPosHpr ( 0, -8, 2.5, 0 -9, 0)
    def loadModels(self):
        self.carousel = loader.loadModel("models/carousel_base")
        self.carousel.reparentTo(render)     #attach it to render
        self.lights1 = loader.loadModel("models/carousel_lights")
    #load the second texture
        self.lights2 = loader.loadModel("models/carousel_lights")
        self.lightOffTex = loader.loadTexture("models/carousel_lights_off.jpg")
        self.lightOnTex = loader.loadTexture("models/carousel_lights_on.jpg")
        self.pandas = [self.carousel.attachNewNode("panda"+str(i))
                   for i in range(4)]
        self.models = [loader.loadModel("models/carousel_panda")
                   for i in range(4)]
        self.moves = [0 for i in range(4)]
    for i in range(4):
        self.pandas[i].setPosHpr(0, 0, 1.3, i*90, 0, 0)
        self.env = loader.loadModel("models/env")
    def setupLights(self):
        ambientLight = AmbientLight("ambientLight")
        ambientLight.setColor(Vec4(.4, .4, .35, 1))
        directionalLight = DirectionalLight("directionalLight")
        directionalLight.setDirection(Vec3(0, 8, -2.5))
        directionalLight.setColor(Vec4(0.9, 0.8, 0.9, 1))
    #Explicitly set the environment to not be lit
    def startCarousel(self):
            self.carouselSpin = self.carousel.hprInterval(20, Vec3(360, 0, 0))
    for i in range(4):
        self.move[i] = LerpFunc(
                        self.oscilatePanda, #function to call
                        duration = 3,       #3 second duration
                        fromData = 0,       #starting value on Radians
                        toData = 2*pi,      #ending value 
                                            #(2pi radians = 360 degree)
                       extraArgs=[self.models[i], pi*(i%2)]
        self.lightBlink = Sequence(
        Func(self.lights1.setTexture, self.lightOnTex, 1),
        Func(self.lights2.setTexture, self.lightOffTex, 1)),
      Wait(1),   #Wait the number of second between the "()"
      #Then we will switch the textures at the same time
        Func(self.lights1.setTexture, self.lightOffTex, 1),
        Func(self.lights2.setTexture, self.lightOnTex, 1)),
      Wait(1)   #Wait the number of second between the "()"
    def oscilatePanda(self, rad, panda, offset):
        self.panda.setZ(sin(rad + offset) * .2)

w = World()
run()        #engage program

i d’ont know what going on, but i need to figure it out before taking the Gui tutorial, and then try to make my first game main menu

thanks for any help

Greeting, :slight_smile:

I don’t know if the indentation came across properly on the forum, but this line near the end is not indented into the “startCarousel” function:


“self” only makes sense inside of a class function because it is one of the arguments.

All your for loop statements have the same indentation problem as well.

i make this line in the “StartCarousel” function, without any success…:frowning:

you know, i probably know the language as well, (but not panda of course) and yet i still not understand what’s a identation problem…any guess? :question:

Unlike most languages, indentation in Python is actually part of the syntax and will affect how the code runs.

In your code here you can think of lines with no indentation as the “main” level, lines indented once (4 spaces) as the “class” level, and lines indented twice or more (8+ spaces) as the “function” level.

Here is an example of incorrect code:

class MyClass(object):
    def __init__(self):
        self.numbers = [1, 2, 3]
    for number in self.numbers:
        print number

Here is the same code corrected:

class MyClass(object):
    def __init__(self):
        self.numbers = [1, 2, 3]
        for number in self.numbers:
            print number

As i can see, evreything needed to be in the def function, understand now! thanks :smiley:

but i did this for all define section of my code, again, without a successfull result :frowning:

this one is more dificult then i tought…

EDIT: I finnaly made it!, but now, i have a very strange issue…
there’s only one panda who oscilate, strange yes, but the main problem has been solved, if you want to help me in this strange bug, that your fun if you don’t want to, i will not be mad :slight_smile:

identation has been the principal problem, now, i will keep looking of my ident

thanks for contribuating^^