NameError: name 'World' is not defined

hi to evreyone :slight_smile:

i finnaly maded to create my frist window in python with panda3d

as a newbie, i follow the solar sys tutorial

the first step work great: i create my first windows!

the second step run bad:

here’s my code:

import direct.directbase.DirectStart
from panda3d.core import *
from direct.gui.DirectGui import *

import sys

class World:
    def __init__(self):
        
        
        self.title=OnscreenText(
            text="python rock!! - but really",
            style=1, fg = (1,1,1,1), pos = (0.8, -0.95), scale = .07)
        
        base.setBackgroundColor(0, 0, 0,)
        
        base.disableMouse()
        
        camera.setPos (0, 0, 45) 
        
        camera.Hpr (0, -90, 0)
        
        
    w = World()
    
    run()

(delete my commentary, because normaly, i put alot of comment)

i run my program, the black window appear, but not the text

and it my core monitor tell me that my first core is running like a full speed lamborgini (100%) and my three other core are just chilling (i think you get what im trying to say)

and the python shell tell me this:

Known pipe types:
  wglGraphicsPipe
(all display modules loaded.)
DirectStart: Starting the game.
NameError: name 'World' is not defined

i do exactly like the tut-step2 script (in fact, the correct scipt was just beside!

what’s wrong with the lambo? :open_mouth:

:question: :question: :question:

import direct.directbase.DirectStart
from panda3d.core import *
from direct.gui.DirectGui import *

import sys

class World:
    def __init__(self):
       
       
        self.title=OnscreenText(
            text="python rock!! - but really",
            style=1, fg = (1,1,1,1), pos = (0.8, -0.95), scale = .07)
       
        base.setBackgroundColor(0, 0, 0,)
       
        base.disableMouse()
       
        camera.setPos (0, 0, 45)
       
        camera.setHpr (0, -90, 0)
       
       
w = World()

run()

You trouble in python syntax. For a python spaces and tabulation are meaning symbols. Also you use camera.Hpr instead camera.setHpr

Please use more reasonable topic title next time.
People ask even simpler questions, no need to mention you are a newbie each time.
In my opinion the Manual should be the first in your list for learning Panda3d: panda3d.org/manual/index.php/Main_Page
Just an opinion.

… Shouldn’t you make your class a child of showbase or something to use camera or do base.camera (base.cam)?
I mean it works, but why is it base.camera everywhere else?

i correct this line, and it does nothing, and the shell give me the same awnser at the “World” class is not defined…

you’re right. Now, you know that i’m a… (d’ont needed to repeat it now^^)

and i will change the topic title soon as posible also.

from my part, i d’ont know, but i ran the original script and it run perfectly

here’s the original code (with comment):

# Author: Shao Zhang and Phil Saltzman
# Last Updated: 4/19/2005
#
# This tutorial is intended as a initial panda scripting lesson going over
# display initialization, loading models, placing objects, and the scene graph.
#
# Step 2: After initializing panda, we define a class called World. We put
# all of our code in a class to provide a convenient way to keep track of
# all of the variables our project will use, and in later tutorials to handle
# keyboard input.
# The code contained in the __init__ method is executed when we instantiate
# the class (at the end of this file).  Inside __init__ we will first change
# the background color of the window.  We then disable the mouse-based camera
# control and set the camera position.

import direct.directbase.DirectStart  #Initialize Panda and create a window
from panda3d.core import *     #Contains most of Panda's modules
from direct.gui.DirectGui import *    #Imports Gui objects we use for putting
                                      #text on the screen
import sys

class World:                          #Our main class
  def __init__(self):                 #The initialization method caused when a
                                      #world object is created
  
    #Create some text overlayed on our screen.
    #We will use similar commands in all of our tutorials to create titles and
    #instruction guides. 
    self.title = OnscreenText(
      text="Panda3D: Tutorial 1 - Solar System",
      style=1, fg=(1,1,1,1), pos=(0.8,-0.95), scale=.07)
  
    #Make the background color black (R=0, G=0, B=0)
    #instead of the default grey
    base.setBackgroundColor(0, 0, 0)
    
    #By default, the mouse controls the camera. Often, we disable that so that
    #the camera can be placed manually (if we don't do this, our placement
    #commands will be overridden by the mouse control)
    base.disableMouse()
    
    #Set the camera position (x, y, z)
    camera.setPos ( 0, 0, 45 )
    
    #Set the camera orientation (heading, pitch, roll) in degrees
    camera.setHpr ( 0, -90, 0 )
#end class world

#Now that our class is defined, we create an instance of it.
#Doing so calls the __init__ method set up above
w = World()
#As usual - run() must be called before anything can be shown on screen
run()

and my code is just at the top.

i was trying during 30 minute to find what going wrong, the debugger tell me it’s some where near my lines who set up the text

but evreything is similar to the original copy…

the code ninth posted works fine. maybe you should do a byte-wise compare between yours and his code.

strange…it work

i will do what you just say…

…

wow! problem solved here’s my “micro” error:

base.setBackgroundColor(0, 0, 0)

correct code:

base.setBackgroundColor(0, 0, 0,) 

you was right Thomas

thanks to him, ninth, and Anon by helping solving this problem, hope it will help many other new people to panda.

cheers,

continuing my work :slight_smile:

Actually i think the error was in your wrong indentation in first place. Where you said w = World() was the class World’s namespace. Correct is:

class World:
    def __init__(self):
        pass
        # foo bar

w = World()
run()

builtin.camera is nothing else than builtin.base.camera.
Both base and camera, which is an attribute of base, are set up when instancing ShowBase. The fact that camera is available without the prepended base, too, is most probably either pure convenience or it has a historical meaning.