Accessing C++ Data Structures, Containers from Python

Hi all,

This is another basic question and the title says it all. In my initial experimentation with mixing C++ and Python, I’ve been able to access, from the python-side, int, float and char data types with success, but whenever I try and access a container like a vector, this is the error yielded:

AttributeError: 'genModule.TestClass' object has no attribute 'group_of_ints'

From this code, on the C++ side:

class TestClass{
	int firstTest;
	NodePath workingNp;
	std::vector<int> group_of_ints;
	void modifyFirstTestValue(int newVal, NodePath& sentNp)
		firstTest = newVal;
		workingNp = sentNp;
		NodePath dummy_node= sentNp.attach_new_node("Dummy Node Name");


	NodePath returnDummyNp(NodePath sentParentNp, LVecBase3* sentVec)
		NodePath dummy_node = sentParentNp.attach_new_node("fillMeUp");
		sentParentNp.set_pos(sentVec->get_x(), sentVec->get_y(), sentVec->get_z());
		return dummy_node;

The python side that uses this module:

print(instTest.group_of_ints)#<this yields the error...

I also noticed that I can’t play around with the vector container from the C++ side like this:


Yields this error from the C++ side:

  error: unknown type 'group_of_ints'



  error: syntax error, unexpected '.'


So how can this be resolved? Moreso, what ways are typically used to initiate containers, modify them, pass them (or their contents) back and forth between Python and C++ in Panda3D? It doesn’t necessarily have to be vector containers.

Thanks and sorry for these basic questions…

After a little bit of digging, I’ve found out that vectors aren’t supported:

So vector is implemented by wrapping it into a template class, which I’ve found is PointerToArray. Adding, removing, reading , etc, all pertinent methods are documented here:

One last question though, how would one access the data stored in the PointerToArray object from the python side, once it’s returned as a “block of raw data in a string”?

	string return_potential_list()
		return group_of_ints.get_data();


Is there any way to convert the returned string into a legible list of integers?


This worked for the conversion:

intListByteString=bytes(potentialListOfInts, "utf8")

#This command yields: [4, 0, 0, 0]

I hope this helps people who are just venturing into mixing C++ and Python somehow.



While I don’t have much experience on the C++ side of things, this looks wrong on the Python side. You are decoding the string as utf-8 which is for text. You are getting [4, 0, 0, 0] for the integer 4 because 32-bit integers are 4 bytes long and you’re converting each byte into a number. Try a large number or a negative number and you’ll see the issue more clearly.

I’d use the array module. Use typecode ‘i’ for signed ints (see the array module documentation for other types).

from array import array
potentialListOfInts = instTest.return_potential_list()
intList = array('i', potentialListOfInts)

Unless you need to do something that specifically requires a list, you can probably use the array as-is in the rest of your code. You can iterate, insert, append, etc. just like a list. Otherwise it has a tolist() method.


Hi, thanks for pointing that out. I actually took a second look and realized that I was doing multiple things incorrectly. The rest of the C++ works fine when I use the PointerToArray template in lieu of a vector container, but the string returned from get_data() isn’t even going to be properly recognized on the Python side, since it’s a C++ type and hence some conversion was needed:

//Correct C++:
	PyObject* return_potential_list()
		string stringObj = group_of_ints.get_data();
		const char* constPntr= stringObj.c_str();
		PyObject* gotString = PyBytes_FromString(constPntr);
		return gotString;

Then on the python side, following your clarification:

potentialListOfInts = instTest.return_potential_list()
intList = array('i', potentialListOfInts)

I tried with negative numbers and it works properly. Thank you so much for the clarification! :grinning_face_with_smiling_eyes:

It would theoretically be possible to tell interrogate to wrap the vector type, using a forcetype declaration, or by putting PUBLISHED: in the parser-inc/vector file.

If you want to return a bytes object from C++, use the vector_uchar type; that one is wrapped automatically as bytes by interrogate.