Toolkit to make C++ modules and extensions for the Panda3D engine

tobspr, updated 🕥 2023-02-21 08:29:26

Build Status

Panda3D Module Builder

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.

Features

  • Automatic Python bindings using interrogate
  • Works on Windows, Linux and Mac

Getting started

1. Clone this repository

You can use the download-zip button, or clone this repository. Copy it to a suitable path in your project.

2. Write your source code

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

ifndef EXAMPLE_H

define EXAMPLE_H

include "pandabase.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; }; };

endif EXAMPLE_H

```

3. Compile the module

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.

4. Use your module

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

```

Requirements

  • The Panda3D SDK (get it here)
  • CMake 2.6 or higher (get it here)
  • windows only: The thirdparty folder installed in the Panda3D sdk folder (See here)

For compiling on Windows 32 bit:

  • Visual Studio 2010/2015

For compiling on Windows 64 bit:

  • Visual Studio 2010/2015
  • Windows SDK 7.1 (be sure to tick the VC++ 64 bit compilers option)

Advanced configuration

Please clean up your built directories after changing the configuration! You can do so with passing --clean in the command line.

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 rebuild

config.ini

Further adjustments can be made in the config.ini file:

  • You can set generate_pdb to 0 or 1 to control whether a .pdb file is generated.
  • You can set optimize to change the optimization. This has to match the --optimize= option of your Panda3D Build.
  • You can set require_lib_eigen to 1 to require the Eigen 3 library
  • You can set require_lib_bullet to 1 to require the Bullet library
  • You can set require_lib_freetype to 1 to require the Freetype library
  • You can set verbose_igate to 1 or 2 to get detailed interrogate output (1 = verbose, 2 = very verbose)

Additional libaries

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}")

```

Issues

Can't package applications using modules

opened on 2021-11-19 21:54:08 by Clockwork-Muse

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

ifndef EXAMPLE_H

define EXAMPLE_H

include "pandabase.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; }; };

endif EXAMPLE_H

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

common: Fix suffix

opened on 2021-01-20 18:38:19 by rocketprogrammer

To go with https://github.com/tobspr/P3DModuleBuilder/commit/ce92036c66ca5ae2b7a46f6e3481ecb889d02caa

Development environment change

opened on 2018-02-27 14:11:38 by SkyLeach

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>

fix panda3d.lui.__name__

opened on 2018-02-22 05:03:20 by pmp-p

interrogate_module.cpp should contains :

PyObject *module = Dtool_PyModuleInitHelper(defs, "panda3d.lui");

actually is:

PyObject *module = Dtool_PyModuleInitHelper(defs, "lui");

Compile Error - Subdirectories not handled

opened on 2016-08-08 20:39:28 by zauberparacelsus

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