I am editing Panda source code and i tried to return a list to use it in Python. I tried to return list and vector data type in c++ to get a list() in Python but it doesn’t work.
I think it is not possible to return list types and use a list in Python but nevertheless i wanted to ask.
If I use function void foo(); or string foo(); I can call it out of Python code but if I use vector foo(); or list foo(); Python even do not find this method.
I think I have to capsulate this class in C++ in a special class and then get every element on it’s own. But I wanted to know if there is a way because it would be much more nice for me
Thanks for help!
Right, returning a list to Python is a bit of a nuisance. If you want to return a list of ints or floats, you can return PTA_int or PTA_float, which is automatically understood by interrogate. And as you’ve observed, returning a string works correctly.
For certain other types, we have a special class that encapsulates a list, like NodePathCollection to return a list of NodePaths, or TextureCollection to render a list of Textures.
If you want to return some other kind of list, you can either write a class similar to TextureCollection, or you can write a function that returns a PyObject *, and use PyList_New() to create a new Python list, and populate it with PyList_SetItem(). Be careful with Python reference counts and such, of course.
Thanks! That is exactly i was searching for. But now i got a new problem. Creating and return list is no problem but list only accepts PyObject* types. I want to have a list of LVecBase3f. This should be no problem because you can return them and they get “magically” tranformed to Vec3. This is then of kind PyObject*. My question is now if i can transform LVecBase3f to Vec3 in C+±Code or how i can create instances of Vec3 in C+±Code.
I found this to create PyObject*:
PyObject* PyInstance_New(PyObject *class, PyObject *arg, PyObject *kw);
But i don’t know how to get the first argument which is the classtype of the instance i want to create.
The easiest way would be if I can easy convert LVecBase3f to Vec3 inside C++.
Thanks for your help!
IMPORT_THIS struct Dtool_PyTypedObject Dtool_LVecBase3f;
LVecBase3f *my_lvec = new LVecBase3f(0, 0, 0);
PyObject *vec3 = DTool_CreatePyInstance(my_lvec, Dtool_LVecBase3f, true, false);
You might be interested in this thread also, since it seems to relate.