The goal of this project is to provide Python language support as a scripting module for the Godot game engine.
Head to the Releases page and download the latest version for your platform.
To build the project from source, first checkout the repo or download the latest tarball.
On a fresh Ubuntu install, you will need to install these:
$ apt install build-essential scons python3 python3-pip curl git
$ pip3 install virtualenv --user
If you are using CPython as your backend, you will need additional libraries to build from source. The simplest way is to uncomment the main deb-src in /etc/apt/sources.list:
deb-src http://archive.ubuntu.com/ubuntu/ artful main
and instruct apt to install the needed packages:
$ apt update
$ apt build-dep python3.6
See the Python Developer's Guide for instructions on additional platforms.
With MacOS, you will need XCode installed and install the command line tools.
$ xcode-select --install
If you are using CPython as your backend, you will need openssl. To install with Homebrew:
$ brew install openssl
You will also need virtualenv for your python.
From your godot-python directory:
For Linux:
godot-python$ scons platform=x11-64 backend=cpython release
For Windows:
godot-python$ scons platform=windows-64 backend=cpython release
For MacOS, you will need to customize our cpp to use clang. Your final command will look like:
godot-python$ scons platform=osx-64 backend=cpython gdnative_parse_cpp="clang -E" release
Valid platforms are x11-64, x11-32, windows-64, windows-32 and osx-64. Check Travis or Appveyor links above to see the current status of your platform.
Valid backends are cpython, pypy.
This command will download the pinned version of the Godot GDNative wrapper library (defined in SConstruct and platform specific SCSub files). It will then download a pinned pypy release binary or checkout cpython, move to a pinned commit and build cpython from source. It will generate the CFFI bindings and compile the shared library for your platform. The output of this command is a zip file which are shared on the release page.
godot-python$ scons platform=<platform> backend=<backend> test
This will run pytests defined in tests/bindings inside the Godot environment. If not present, will download a precompiled Godot binary (defined in SConstruct and platform specific SCSub files) to and set the correct library path for the GDNative wrapper.
godot-python$ scons platform=<platform> backend=cpython example
This will run the converted pong example in examples/pong inside the Godot environment. If not present, will download a precompiled Godot binary (defined in SConstruct) to and set the correct library path for the GDNative wrapper.
If you have a pre-existing version of godot, you can instruct the build script to use that the static library and binary for building and tests.
godot-python$ scons platform=x11-64 backend=cpython godot_binary=../godot/bin/godot.x11.opt.64 gdnative_wrapper_lib=../godot/modules/include/libgdnative_wrapper_code.x11.opt.64.a
You check out all the build options in this file.
example:
# Explicit is better than implicit
from godot import exposed, export
from godot.bindings import Node2D, Vector2
@exposed
class Player(Node2D):
"""
This is the file's main class which will be made available to Godot. This
class must inherit from `godot.Node` or any of its children (i.g.
`godot.KinematicBody`).
Because Godot scripts only accept file paths, you can't have two `exposed` classes in the same file.
"""
# Exposed class can define some attributes as export(<type>) to achieve
# similar goal than GDSscript's `export` keyword
name = export(str)
# Can export property as well
@export(int)
@property
def age(self):
return self._age
@age.setter
def age(self, value):
self._age = value
# All methods are exposed to Godot
def talk(self, msg):
print("I'm saying %s" % msg)
def _ready(self):
# Don't confuse `__init__` with Godot's `_ready`!
self._age = 42
# Of course you can access property & methods defined in the parent
name = self.get_name()
print('%s position x=%s, y=%s' % (name, self.position.x, self.position.y))
...
class Helper:
"""
Othes classes are considered helpers and cannot be called from outside
Python. However they can be imported from another python module.
"""
...
The project is built with the awesome CFFI. Before that, both Micropython and Pybind11 have been tried, but each comes with its own drawback (basically API complexity and compatibility for Micropython, C++ craziness and output size for Pybind11) so they just couldn't compete with CFFI ;-)
CFFI connects with Godot C APIs: - GDnative for calling Godot functions - Pluginscript for registering callback function for Godot CFFI connects to Godot C
Map of the code:
pythonscript.[c|h]
: Godot Pluginscript entry point.cffi_bindings/api.h
&cffi_bindings/api_struct.h
: Exposed C api use in the language classes implementations.cffi_bindings/*.inc.py
: Python code that will be verbatim included in the pythonscript module.cffi_bindings/builtin_*.inc.py
: Python binding for Godot builtinscffi_bindings/embedding_init_code.inc.py
: Very first Python code that will be executed on module loading.cffi_bindings/mod_godot.inc.py
: Pythongodot
module code.cffi_bindings/mod_godot_bindings.inc.py
: Pythongodot.bindings
module code.cffi_bindings/cdef.gen.h
: C Godot's GDnative API ready to be used by the CFFI generator. This file is generated bytools/generate_gdnative_cffidefs.py
.cffi_bindings/pythonscriptcffi.cpp
: Pythonscript module output by the CFFI generator. This file is generated bycffi_bindings/generate.py
.