BGCython: Cython Implementation for BGE / UPBGE

BGCython

See BGCython on GitHub

WARNING: Tested only on Windows.

BGCython is a Cython implementation for BGE, building extensions automatically at GE start whenever the source files are modified. It only needs a single line of code to compile all of your .pyx files in the given path to C extensions.

If you’re not aware of what Cython is, it basically allows you to write Python code, convert it to C code, allowing you to compile it to a binary extension, and still execute this code through the Python interpreter, but with the benefits from C code, being the speed the most relevant. BGCython brings a simple example file, showing a C extension running at the double of speed from the same pure Python code.

Using Cython in BGE is benefical in various ways, from executing heavy code faster to protecting your scripts by using compiled C extensions instead of your pure Python scripts.

Install

Clone / download the repository and run the blend file named install.blend. The blend contains some instructions, the most important being to enable the Blender console to check the progress of installation and other messages.

This blend will run two scripts:

  • install.py will run get-pip.py (to install pip on your Blender), then installs Cython through pip and copies package files into Blender’s Python library.
  • get-pip.py will download and install pip on your Blender, allowing to install new packages from inside Blender.

After the installation, a settings text file called settings.txt will/may run, and you can fill some fields with some custom paths if you want.

Windows

Now, you must get the GCC compiler. In case you’re using Windows, download and install MinGW through the web installer, and select only The GNU C++ Compiler to install. After the installation, the default installation path may be C:\MinGW. I recommend you to add C:\MinGW\bin to your system path to easily run it through command line. After you do that, run in the command prompt:

gcc --version

And if the result is something like:

gcc (MinGW.org GCC-6.3.0-1) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

You’re done. However, you can only add the path C:/MinGW/bin to settings.txt, on path_gcc value, like this:

'path_gcc' : 'C:/MinGW/bin'

And BGCython will be able to find it.

Linux

TODO

Using

Check the demo demos/benchmark/benchmark.blend to test the module. You may need to modify scripts/mod.pyx to make BGCython recreate the timestamps and compile the .pyx file, but it must do it, otherwise the installation may not have worked.

To use BGCython, all you need to do is call inside a initialization script (preferably in module level) the function:

bgcython.bgcythonize(current_path)

The recommended standard is a __init__.py on the top level of your scripts folder with the following code:

# Set to True on game release
release = False

if not release:
	
	from bge.logic import expandPath
	from pathlib import Path
	from bgcython import bgcythonize
	
	current_path = Path(expandPath('//')).resolve()
	
	bgcythonize(current_path)

With current_path being the path you want the module to look for .pyx scripts. It will look recursively on all folders on the given path, and create a timestamps.txt file on this path. After this first run, everytime you modify your .pyx files and run BGE, BGCython will rebuild the extensions. See the demos on this repository for further advice.

The .pyx files and timestamps.txt can be discarded on your release, you only need the compiled extensions.

2 Likes

Looks interesting! I will definitely have a try soon, nice initiative!

So if I understand correctly, the only link there is to Blender is the install.py script, ran from install.blend file.

After that point, your library is a build automation tool, independent from the BGE right?

Still a good tool! Just trying to understand the philosophy behind it :slight_smile:

(TBH I wanted to integrate something similar inside a framework project of mine, where I would provide a build tool for Cython sources, you beat me to it, but I feel like there is still room for improvement :stuck_out_tongue: )

Yes, its intent is to automate the work of creating C extensions, and is specially useful for anyone who wants to protect its Python source code using C extensions and don’t want to run a command line everytime to build them: just start BGE and you have your extensions built everytime.

I aimed at BGE and UPBGE, but it surely could be used at any Python project.

I saw that you used the get-pip.py script to fetch pip, I always find this method a bit overkill lol

Mostly because Blender (at least on windows), has a ensurepip module, that will install pip. The tricky part is finding the Python executable to run it from, but it can be done from the Blender console.

The Python executable is usually somewhere in Blender\2.79\bin\python.exe or something similar (not on windows right now, can’t remember from the top of my head).

Past this point you can just import pip and keep doing your business.

lol I didn’t know about ensurepip, I will update BGCython right now to use this instead of get-pip.py, thanks a lot for the info.

About the executable, it’s quite easy to get it, just iterate over Blender folder with os.walk and find a folder called bin (present in both Windows and Linux), then find a python executable inside this folder. Probably I will review this way of searching by using pathlib instead of os.walk, and I’m pretty sure I will put BGCython on PyPi to make the installation even easier, without the need to clone / download the repository.

1 Like

In our project, we fetched the Python executable using the glob module, which is pretty convenient :stuck_out_tongue:

1 Like

This is really interesting!

I had been wondering about - http://nuitka.net/

if we could use it as a interpreter