Sending Data structures from C++ to Python

I’m working with the source code to add in some pathfinding functionality for a project I am currently working on. I’m trying to send simply an array from C++ to Python. I’m assuming it’d be best to transfer it into an instance of a List or Tuple but I wouldn’t know how to go about doing that in Panda.

Any help is greatly appreciatted!!

Bert

I also have a similar quests for passing array from python into c++. My Array is array for Vectors3’s. Timeisaparallax what is type of your array?

One thing we commonly do is make a C++ class that provides a get_num_elements() and get_element() method. Then you can iterate through the items on the Python side, one at a time, and build up your list over there. For instance, see NodePathCollection, which is returned by NodePath::find_all_matches(), or TextureCollection, which is returned by NodePath::find_all_textures().

If you are doing your development on the very latest Panda (straight from the CVS server), you can then define a MAKE_SEQ macro within your collection object, which automatically turns your get_num_elements() and get_element() method into a synthesized get_elements() method, which returns a true Python list. See the aforementioned classes for examples of this as well.

Furthermore, if your class defines both an operator [] method, and a size() method, then the class will automatically be turned into an iterable object on Python (again, as demonstrated by the aforementioned classes). This is again with the very latest Panda only.

Finally, you can write your own method that returns a PyObject *, then use the appropriate Python calls as documented in the Python/C++ API doc pages on python.org, to create and return a Python list directly. But this is the hard way.

David

So now I’ve realized that I’m not sure how Python handles the class objects that it recieves from C++. My goal will be to have two classes. One a master graph for a scene that will be used for pathing, the second class will be a link to that master graph that needs a reference to it to be able to do A*. I’m wondering how python handles objects returns versus pointer returns from methods? I’m trying to avoid making copies of the master graph and instead I would like to solely reference the data in it.

My goal, due to time contraints would be to be able to do this without having to make PyObject’s in C++. Is this possible?

-Bert

First, I’m assuming you’re using Panda’s native interrogate to instrument your code, and not some other system.

In general, if a function returns a pointer or a reference, then this is returned to Python by reference. A new Python wrapper object is created, which contains internally a pointer to the original C++ object. If, on the other hand, a function returns a concrete instance, then the C++ copy constructor is invoked. A new Python wrapper object is created, which contains internally a pointer to a copy of the original C++ object.

There’s one more convenience. If the object inherits from ReferenceCount, then Python will automatically manage the C++ reference count for the object. If it does not, then Python will have to assume that the object will persist for at least as long as the Python wrapper object persists.

David