Get Camera Position

Hello,

Is it possible to get, say, just the x coordinate of camera position, as the only method I am seeing returns a point with three cooridinates…

As base.camera.getPos() returns Point3( 20, 50, 40)

I am just interested in the 20 or the x coordinate and do not know how I would parse a point.

Thanks so much,
Gary

Never-mind, wow it’s base.camera.getX()

not

base.camera.getPosX()

silly on both accounts

Also see http://www.panda3d.org/reference/python/classpanda3d_1_1core_1_1LVecBase3f.php and note that you could use base.camera.getPos().getX() or base.camera.getPos()[0] just as well.

David

Thanks drwr, that’s helpful.

How did you know to look in the LVecBase3f class?

I was looking at camera, base, etc. to try and find that method that I was sure was somewhere.

First, you have to know that base.camera is actually an instance of class NodePath, which you can determine from Python:

>>> base.camera.__class__
<type 'libpanda.NodePath'>

Armed with that information, you can look up NodePath in the class reference and find:

And you can follow the link on LPoint3f to discover that it inherits from LVecBase3f, which is where getX() is defined.

Or, you can use Python’s built-in help utility:

>>> help(base.camera.getPos())
Help on Point3 object:

class Point3(VBase3)
 |  // Filename: lpoint3_src.h
 |  // Created by:  drose (25Sep99)
 |  //
 |  ////////////////////////////////////////////////////////////////////
 |  //
 |  // PANDA 3D SOFTWARE
 |  // Copyright (c) Carnegie Mellon University.  All rights reserved.
 |  //
 |  // All use of this software is subject to the terms of the revised BSD
 |  // license.  You should have received a copy of this license along
 |  // with this source code in a file named "LICENSE."
 |  //
 |  ////////////////////////////////////////////////////////////////////
 |  ////////////////////////////////////////////////////////////////////
 |  //       Class : LPoint3
(pages and pages of text snipped)

this describes all of the interfaces on LPoint3, including the inherited interfaces from the base class.

One point that is helpful to know a priori is that the vector classes are renamed from C++ to Python in a way contrary to the rest of the class renaming convention: the C++ classes LPoint3f, LVector3f, and LVecBase3f are called Point3, Vec3, and VBase3 in Python, respectively.

David

Thanks for breaking it down David.

Tried the help a few times, in terminal / python and got;

>>> help(base.camera.getPos())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'base' is not defined

[/code]

also;

>>> base.camera.__class__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'base' is not defined

I ran the Panda script upon installation…

“base” is a global that is defined when you import DirectStart (or otherwise construct a ShowBase object).

The biggest advantage of a language like Python is that you can run it interactively: load up your Panda application in an IDE, and then type the above commands at the Python prompt after your application is loaded. “base” will exist in that context.

Or, if you’re not ready to embrace an IDE yet, you could run your application with “python -i myapp.py”. Press control-C to break to a Python prompt after it’s loaded. (If that interrupts the application instead, then you’ll have to omit the run() call in your myapp.py, and type it yourself when you want to start the app running. And there won’t be a way to break to a prompt, so that’s a strong reason to switch to an IDE.)

Or, you could simply start Python as you are above, and type “from direct.directbase.DirectStart” at the python prompt before you query these values. But that’s not really taking advantage of the power of Python. :slight_smile:

David