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):
        #trololololo-lololo-lololoooooooo---
        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)
        
        
        self.loadModels()
        self.setupLights()
        self.startCarousel()
        
        
    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")
        self.lights1.reparentTo(self.carousel)
    
    #load the second texture
        self.lights2 = loader.loadModel("models/carousel_lights")
        self.lights2.setH(36)
        self.lights2.reparentTo(self.carousel)
        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.models[i].reparentTo(self.pandas[i])
        self.models[i].setY(.85)
        self.env = loader.loadModel("models/env")
        self.env.reparentTo(render)
        self.env.setScale(7)
      
      
    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))
        render.setLight(render.attachNewNode(directionalLight))
        render.setLight(render.attachNewNode(ambientLight))
    
    #Explicitly set the environment to not be lit
        self.env.setLightOff()
    
    def startCarousel(self):
            self.carouselSpin = self.carousel.hprInterval(20, Vec3(360, 0, 0))
            self.carouselSpin.loop()
    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.moves[i].loop()
    
        self.lightBlink = Sequence(
      Parallel(
        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
      Parallel(
        Func(self.lights1.setTexture, self.lightOffTex, 1),
        Func(self.lights2.setTexture, self.lightOnTex, 1)),
      Wait(1)   #Wait the number of second between the "()"
      )
        
    self.lightBlink.loop()
    
    
  
    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.lightBlink.loop()

“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^^