This tool allows you to seamlessly mix your C++ and Python code for the Panda3D Game Engine.
It makes compiling your C++ code the matter of a single mouse-click.
interrogate
You can use the download-zip button, or clone this repository. Copy it to a suitable path in your project.
You can now start to write your C++ code and store it in the source/
directory.
Here's a simple example you can start with (save it as source/example.h
for example):
```cpp
BEGIN_PUBLISH // This exposes all functions in this block to python
inline int multiply(int a, int b) { return a * b; }
END_PUBLISH
class ExampleClass { PUBLISHED: // Exposes all functions in this scope, use instead of "public:" inline int get_answer() { return 42; }; };
```
After you wrote your C++ code, run python build.py
. It will ask you for
a module name, for this example we will choose "TestModule".
When the compilation finished, there should now be a TestModule.pyd
/ TestModule.so
(depending on your platform) generated.
Using your compiled module is straightforward:
```python import panda3d.core # Make sure you import this first before importing your module
import TestModule
print(TestModule.multiply(3, 4)) # prints 12
example = TestModule.ExampleClass() print(example.get_answer()) # prints 42
```
For compiling on Windows 32 bit:
For compiling on Windows 64 bit:
Please clean up your built directories after changing the configuration! You can
do so with passing --clean
in the command line.
Command line options are:
--optimize=N
to override the optimize option. This overrides the option set in the config.ini
--clean
to force a clean rebuildFurther adjustments can be made in the config.ini
file:
generate_pdb
to 0
or 1
to control whether a .pdb
file is generated.optimize
to change the optimization. This has to match the --optimize=
option of your Panda3D Build.require_lib_eigen
to 1
to require the Eigen 3 libraryrequire_lib_bullet
to 1
to require the Bullet libraryrequire_lib_freetype
to 1
to require the Freetype libraryverbose_igate
to 1
or 2
to get detailed interrogate output (1 = verbose, 2 = very verbose)If you want to include additional (external) libraries, you can create a
cmake file named additional_libs.cmake
in the folder of the module builder,
which will then get included during the build.
If you would like to include the protobuf library for example, your cmake file could look like this:
```cmake find_package(Protobuf REQUIRED) include_directories(${PROTOBUF_INCLUDE_DIRS}) set(LIBRARIES "${LIBRARIES};${PROTOBUF_LIBRARIES}")
```
Although the module runs if run via python, it fails when attempted to run via packaging:
bash
Traceback (most recent call last):
File "__main__", line 1, in <module>
File "importlib._bootstrap", line 991, in _find_and_load
File "importlib._bootstrap", line 975, in _find_and_load_unlocked
File "importlib._bootstrap", line 671, in _load_unlocked
File "importlib._bootstrap", line 827, in exec_module
File "panda3d.core", line 1, in <module>
File "imp", line 342, in load_dynamic
File "importlib._bootstrap", line 702, in _load
File "importlib._bootstrap", line 657, in _load_unlocked
File "importlib._bootstrap", line 556, in module_from_spec
File "importlib._bootstrap_external", line 1166, in create_module
File "importlib._bootstrap", line 219, in _call_with_frames_removed
ImportError: /proj/build/manylinux2010_x86_64/libpandaexpress.so.1.10: undefined symbol: _ZNK11GlobPattern14matches_substrEN9__gnu_cxx17__normal_iteratorIPKcSsEES4_S4_S4_
Repro steps: ```bash - docker run --rm -it --entrypoint /bin/bash ubuntu - apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -y install cmake curl git python3-venv python3-pip - curl https://www.panda3d.org/download/panda3d-1.10.10/panda3d1.10_1.10.10~focal_amd64.deb -o panda3d-1.10.10/panda3d1.10_1.10.10~focal_amd64.deb - apt-get -y install panda3d-1.10.10/panda3d1.10_1.10.10~focal_amd64.deb - git clone https://github.com/tobspr/P3DModuleBuilder.git /proj/P3DModuleBuilder - cd /proj/P3DModuleBuilder - rm -rf source/* - cat << EOF > source/example.h
BEGIN_PUBLISH // This exposes all functions in this block to python inline int multiply(int a, int b) { return a * b; } END_PUBLISH class ExampleClass { PUBLISHED: // Exposes all functions in this scope, use instead of "public:" inline int get_answer() { return 42; }; };
EOF - echo -e "\nmodule_name=TestModule\n" >> config.ini - python3 build.py - cd ../ - cat << EOF > s.py import panda3d.core # Make sure you import this first before importing your module from P3DModuleBuilder import TestModule print(TestModule.multiply(3, 4)) # prints 12 example = TestModule.ExampleClass() print(example.get_answer()) # prints 42 EOF - python3 s.py - echo -e "panda3d" >> requirements.txt - cat << EOF > setup.py import setuptools setuptools.setup( name="repro", version="0.1", options={ 'build_apps': { 'platforms': [ "manylinux2010_x86_64" ], 'console_apps': { 'repro': "s.py", } } } ) EOF - python3 setup.py build_apps - ./build/manylinux2010_x86_64/repro
To go with https://github.com/tobspr/P3DModuleBuilder/commit/ce92036c66ca5ae2b7a46f6e3481ecb889d02caa
This pull request is in keeping with the overall virtual environment/multiple sources requirements of heavy development.
When working with panda3d and modules developers may need or wish to try out features within a python virtual environment.
They may recompile panda from source (as I do) with new flags and then wish to have those dynamic libraries available within a virtual environment rather than a system library.
This change merely adds allowance for building (not installing) against a panda source/build location.
In my test code, I have the envvar LOCAL_PANDA_BUILD=~/src/panda3d/built
Tested with panda_LUI on Mac OSX. In this source base, the variable must come in as -DLOCAL_PANDA_BUILD:STRING=<value of envvar>
interrogate_module.cpp should contains :
PyObject *module = Dtool_PyModuleInitHelper(defs, "panda3d.lui");
actually is:
PyObject *module = Dtool_PyModuleInitHelper(defs, "lui");
I attempted to use P3DModuleBuilder with a C++ library that implements the Cubical Marching Squares algorithm, and ran into a compiler error:
``` Process error:
*** Error in ./cubical-marching-squares/include/vec3.h near line 31, column 16:
syntax error, unexpected KW_STATIC
Error parsing file: './cubical-marching-squares/include/cell.h' ```
I'll note that the CMS library compiles normally on its own. Would it be possible to 'trick' P3DModuleBuilder by copying over the compiled libCMS.a file?
The library in question is at: https://bitbucket.org/GRassovsky/cubical-marching-squares