Making camera follow panda actor

I’m trying to make the camera follow the panda actor in 3rd person mode. This is the link to my code -> https://paste.pythondiscord.com/bedabeceke.py
For some reason, it keeps giving me this error

Known pipe types:
  CocoaGraphicsPipe
(all display modules loaded.)
2019-11-25 19:58:13.620 Python[10532:359092] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to (null)
Traceback (most recent call last):
  File "/Users/38167/PycharmProjects/viren/scene.py", line 39, in <module>
    app = MyApp()
  File "/Users/38167/PycharmProjects/viren/scene.py", line 28, in __init__
    self.taskMgr.add(self.camera, "camera")
  File "/Users/38167/PycharmProjects/viren/venv/lib/python3.7/site-packages/direct/task/Task.py", line 384, in add
    task = self.__setupTask(funcOrTask, name, priority, sort, extraArgs, taskChain, appendTask, owner, uponDeath)
  File "/Users/38167/PycharmProjects/viren/venv/lib/python3.7/site-packages/direct/task/Task.py", line 404, in __setupTask
    'add: Tried to add a task that was not a Task or a func')
  File "/Users/38167/PycharmProjects/viren/venv/lib/python3.7/site-packages/direct/directnotify/Notifier.py", line 130, in error
    raise exception(errorString)
Exception: add: Tried to add a task that was not a Task or a func

it would be a great help if anyone could figure out why this is happening, thanks!

Hi, welcome to the forums!

You have a name conflict: you named a variable self.camera, but you also created a method def camera, which is also stored by Python in self.camera. Therefore, the error says that you are trying to pass your camera object into taskMgr.add instead of the method.

You should simply rename one or the other. For example, you could rename your method to def camera_task().

Since the primary class in the program inherits from “ShowBase”, “self.camera” is presumably the default system camera (i.e. what might otherwise be accessed as “base.camera”). Thus it’s probably better to rename the method than the camera!

Just tried your solution and it works! Thanks for the help and happy coding!!! I still have one question though, when I did it my mouse no longer could look around, is there a way of preventing this?

The default mouse look is really only meant for basic debugging and it’s typical that users will call self.disableMouse() and instead implement their own mouse navigation.

That said, if you wish to control the camera in a way compatible with the built-in mouse navigation, then you can instead use self.trackball.node().setPos() instead. (Note that the coordinates are not quite the same way as they are when manipulating base.camera.)

Ok so I tried following your instructions as best as possible and ended up with this:

   def camera_task(self, task):
        self.trackball.node().setPos(self.pandaActor.getX(), self.pandaActor.getY() + 15, -4)
        self.camera.lookAt(self.pandaActor)
        return Task.cont

and yes I did have to manipulate the coordinates a bit.

So I guess what I’m asking is what did that do to the camera? When I ran the script it just kinda looked towards the panda’s direction but did not follow it which made seeing the panda actor hard.