I’ve tried several ways I can think of to get this to work, but unfortunately haven’t been successful yet. I feel like I am ‘close’ but every time I get that feeling it seems to jinx it!
I have an APK with two activities – one to install and test-launch Python from the filesystem, which works perfectly with all the Panda libraries, but can’t draw anything, and the Native Activity which you wrote, which launches Python correctly but crashes out upon trying to start the renderer. If Python doesn’t render anything then pview can work as normal with the walking Panda.
So I modified config_android.cxx to reflect the change of APK location and I’ve tried a system() call from a version of pview.cxx’s main() function to launch Python. This sets all the environment variables and launches Python in one line (so the vars don’t get removed). My Python scripts do work when they are run from pview – Panda libraries can be imported, files can be written, etc, but as soon as test = ShowBase() is called, the Python script just exits (even in a try…except to catch all exceptions) and the pview.cxx program continues on to where it draws the walking Panda. The Android debugger outputs (amoungst other things):
I/DEBUG ( 2111): #00 pc 00028d44 /data/app-lib/com.android.python27-1/libpandagles.so
I/DEBUG ( 2111): #01 pc 00265cfb /data/app-lib/com.android.python27-1/libpanda.so (GraphicsWindow::set_properties_now(WindowProperties&)+110)
I/DEBUG ( 2111): #02 pc 00029ff9 /data/app-lib/com.android.python27-1/libpandagles.so
I/DEBUG ( 2111): #03 pc 00265c2b /data/app-lib/com.android.python27-1/libpanda.so (GraphicsWindow::process_events()+82)
I/DEBUG ( 2111): #04 pc 00029fbb /data/app-lib/com.android.python27-1/libpandagles.so
I/DEBUG ( 2111): #05 pc 002556eb /data/app-lib/com.android.python27-1/libpanda.so (GraphicsEngine::process_events(ov_set<PointerTo<GraphicsOutput>, IndirectLess<GraphicsOutput>, pvector<PointerTo<GraphicsOutput> > > const&, Thread*)+26)
I/DEBUG ( 2111): #06 pc 00255cb7 /data/app-lib/com.android.python27-1/libpanda.so (GraphicsEngine::WindowRenderer::do_windows(GraphicsEngine*, Thread*)+26)
I/DEBUG ( 2111): #07 pc 0025fa9f /data/app-lib/com.android.python27-1/libpanda.so (GraphicsEngine::open_windows()+70)
I/DEBUG ( 2111): #08 pc 0026037f /data/app-lib/com.android.python27-1/libpanda.so (GraphicsEngine::make_output(GraphicsPipe*, std::string const&, int, FrameBufferProperties const&, WindowProperties const&, int, GraphicsStateGuardian*, GraphicsOutput*)+1026)
I/DEBUG ( 2111): #09 pc 0025aaa9 /data/data/com.android.python27/files/python/lib/python2.7/lib-dynload/panda3d/core.so
I/DEBUG ( 2111): #10 pc 0008dec8 /data/app-lib/com.android.python27-1/libpython2.7.so (PyCFunction_Call+296)
I’ve tried putting the system() call in different locations in the C++ program in the hope that Python will find the viewer created by C++ (knowing this was unlikely), or maybe it could find the NativeActivity if launched straight away in pview’s main(). It never does
My Python script definitely is importing the Panda libraries correctly. These Python commands work when launched via pview. I tried using ctypes as suggested before to verify the DLLs are loaded, and it worked this time, from inside pview (it didn’t when I ran it from the shell).
from ctypes import CDLL
from panda3d.core import ConfigVariableString, loadPrcFileData, loadPrcFile
from direct.showbase.ShowBase import ShowBase
I’ve checked that plugin-path has been set correctly by writing it out to a file. It does point to all the Panda lib*.so libraries, and libpandagles.so is in there. ExecutionEnvironment.getDtoolName() always returns ‘unknown’ though, which is confusing, even though it knows where to find the library and the CDLL parts worked.
The Python script always gets to test = ShowBase(), but then completely quits and then the pview C++ program continues.
If you run the Python side via adb shell exactly the same as in pview, you can see Python all works, although it’ll fail when you try to do ‘test = ShowBase()’ with the usual claim that there is no valid graphics library to draw with (like I mentioned a while ago), instead of just quitting. That’s okay because I didn’t expect it to find/launch the NativeActivity if I’m launching Python from a PC via the shell, but I thought it might find it if I launched it via Panda’s NativeActivity & pview.
Another way I tried – I followed the Python embedding manual – if I include Python.h in pview, and try to embed Python code, Py_Initialize(); works, but Py_Finalize(); or any other Python-related command causes the main() function to not run at all. Meaning I can’t seem to run a Python script that way either. I’m still not sure if calling Python this way will make the renderer work though!
As usual, if you have any ideas I’d be very grateful!