Camera reposit error

First: sorry for bad english, but i am german.
2.:
This is my code:

x = 0
x1 = 0
y = 0
y1 = 0
z = 0
z1 = 0
class MyApp(ShowBase):
    def up(self):
        z = z + 0.25
        camera.setPosHpr(Vec3(x1,y1,z1),Vec3(x,y,z))
        z1  = z
    def __init__(self):
        self.accept("h",self.up)
app = MyApp()
app.run()

if i press “h”, there comes an error:

  File "C:\Panda3D-1.7.2\direct\showbase\EventManager.py", line 61, in eventLoop
Task
    self.doEvents()
  File "C:\Panda3D-1.7.2\direct\showbase\EventManager.py", line 55, in doEvents
    processFunc(self.eventQueue.dequeueEvent())
  File "C:\Panda3D-1.7.2\direct\showbase\EventManager.py", line 124, in processE
vent
    messenger.send(eventName)
  File "C:\Panda3D-1.7.2\direct\showbase\Messenger.py", line 388, in send
    self.__dispatch(acceptorDict, event, sentArgs, foundWatch)
  File "C:\Panda3D-1.7.2\direct\showbase\Messenger.py", line 473, in __dispatch
    method (*(extraArgs + sentArgs))
  File "script.py", line 20, in hoch
    z = z + 0.25
UnboundLocalError: local variable 'z' referenced before assignment
:task(error): Exception occurred in PythonTask eventManager
Traceback (most recent call last):
  File "script.py", line 48, in <module>
    app.run()
  File "C:\Panda3D-1.7.2\direct\showbase\ShowBase.py", line 2630, in run
    self.taskMgr.run()
  File "C:\Panda3D-1.7.2\direct\task\Task.py", line 502, in run
    self.step()
  File "C:\Panda3D-1.7.2\direct\task\Task.py", line 460, in step
    self.mgr.poll()
  File "C:\Panda3D-1.7.2\direct\showbase\EventManager.py", line 61, in eventLoop
Task
    self.doEvents()
  File "C:\Panda3D-1.7.2\direct\showbase\EventManager.py", line 55, in doEvents
    processFunc(self.eventQueue.dequeueEvent())
  File "C:\Panda3D-1.7.2\direct\showbase\EventManager.py", line 124, in processE
vent
    messenger.send(eventName)
  File "C:\Panda3D-1.7.2\direct\showbase\Messenger.py", line 388, in send
    self.__dispatch(acceptorDict, event, sentArgs, foundWatch)
  File "C:\Panda3D-1.7.2\direct\showbase\Messenger.py", line 473, in __dispatch
    method (*(extraArgs + sentArgs))
  File "script.py", line 20, in up
    z = z + 0.25
UnboundLocalError: local variable 'z' referenced before assignment

i have shorten the code.
i dont know how the error comes.
can anyone help me?

Python has pretty good error reporting. It is easy to find out where the error occured:

  File "script.py", line 20, in hoch
    z = z + 0.25
UnboundLocalError: local variable 'z' referenced before assignment 

So we know where the error is. And we also know what the error is: you assign to a variable which can not be assigned (yet).

Try reading the bad line slowly. first, the compiler sees “z =”. Now the compiler knows that you want to assign to a variable named z. Fine, it creates a local variable z, but so far this variable does not have a value yet!

Sidenote: The interpreter creates a local variable because you did not tell Python to use the global variable with the same name(“global z”). Neither is z an instance variable (“this.z”).

Now the interpreter moves on. Next it sees “= z + 0.25”. Ok, so he is told to compute “z + 0.25”, and then assign it to z. He look up the value of z, but z has no value yet. The interpreter does not know what to add to 0.25, and thus throws an exception.

I recommend reading some Python tutorials (and maybe introductions to coding) before attempting to dig into a complex software library like Panda3D.

Basically, as enn0x says, when you refer to “z” in “z + .25”, it’s going to look for a local variable by default. You’ll need to tell it to look for the global “z” variable, like so:

z = 0

class MyApp(ShowBase):
    def up(self):
        global z
        z = z + 0.25
        camera.setPosHpr(Vec3(x1,y1,z1),Vec3(x,y,z))

I’m not sure what the point of assigning z’s value to z1 is (“z1 = z”), but it might be a good idea to also declare z1 as global as well since it is a module-level variable (at the top of the script). My guess is that the z1 = z does absolutely nothing, because it’s assigning z’s value to the local z1 variable, not the global z1 variable.

This isn’t related to the problem, but it would probably be a good idea to store your Pos and HPR variables in a more organized and descriptive way, such as in vectors (Vec3), or even as variables in the class.

Could look something like this:

class MyApp(ShowBase):
    def __init__(self):
        self.pos = Vec3(0, 0, 0)
        self.hpr = Vec3(0, 0, 0)
        self.accept("h",self.up) 

    def up(self):
        self.hpr[2] += 0.25
        camera.setPosHpr(self.pos, self.hpr)
        self.pos[2] = self.hpr[2] #This is what you're doing with z1 = z, right?

app = MyApp()
app.run()