<GeeXLab Rootard Guide/>
GeeXLab and Python
Last update: 2018.12.10 par JeGX
>> Back <<
The
Python language is one of the two languages currently available in GeeXLab (the other language being Lua).
Python is available in all versions of GeeXLab (Windows, Linux, MacOS, Raspberry Pi and Tinker Board) via a plugin. This is the big difference with the Lua
that is integrated into the core of GeeXLab. The Python plugin is automatically loaded by GeeXLab if it is present in the plugins directory.
Python is a very powerful programming language. It is totally object oriented and allows you to write concise code. Python is a mature language and has a large
community. One of the big advantages of Python (especially compared to Lua) is its standard library which is really complete. Just to give you an example,
it is possible, with a standard Python installation, to quickly code a database access, a TCP / IP server or to unzip a file. I think it's this great
wealth of features that appealed to me.
To my taste, one of the big drawbacks of Python is its very rigid syntax and strict indentation. But once past this point, the Python is just as good to use
as the Lua.
It is quite possible to use Python and Lua together in the same GeeXLab demo. These two languages are not mutually exclusive.
Every language is a tool. And GeeXLab is a toolbox. One can very well imagine a demo in which one of the initialization scripts
must go to retrieve data files on a web server. This feature can be easily programmed with a Python script, while the rest of the scripts
are in Lua. There is no constraint. A demo can have three INIT scripts (two in Lua and one in Python), a FRAME script in Lua (or Python) and
a TERMINATE script in Python.
Just to get things straight, here are some lines of Python:
a = 5
b = 2
c = a + b
d = 0
if (c > 5):
d = 1
else:
d = -1
s = "This is a"
s += " string"
x = 0
for i in range(0, 10):
x = x + 1
def myKoolFunc(a, b, c):
# indentation!!!
# Do something useful with a, b and c:
sum = a+b+c
avg = sum/3
return sum, avg
x, y = myKoolFunc(1, 2, 3)
Python exists in two major versions: Python 2.x and Python 3.x. In its current version, GeeXLab is limited to Python 2.x. But the support of Python 3.x has
already been tested and maybe this year or certainly next year (so 2019), the plugin Python 3.x will become a priority because the branch 2.x of Python
will arrive at the end of life in 2020. After 2020, the Python plugin 2.x will still be available and the new GeeXLab functions will be added but
the Python libraries that are used to compile the plugin will no longer be updated.
Update (10/12/2018): GeeXLab 0.26+ and Python 3
Python 3 support is available from GeeXLab 0.26+. GeeXLab now has 3 scripting languages: Lua, Python 2 and Python 3. Even though Python 2 and
Python 3 are both Python, they are still available as two different plugins.
It is possible to have Python 2 and Python 3 in the same demo, in the same way that it is possible to have Lua and Python in the same demo.
GeeXLab 0.26+ introduces two new values for the
language attribute of the
script node:
PYTHON_2 and
PYTHON_3.
Here are some links:
Simple example of GeeXLab demo in Python
And finally, here's an example of a GeeXLab demo that uses Python. The demo is composed of three files: the main XML, the INIT script and the FRAME script.
For comparison, the same Lua demo is
HERE.
The main XML file: main.xml
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<glsl_hacker>
<window name="win3d01" title="Hello World!" width="700" height="200" />
<script name="init_scene" run_mode="INIT" language="PYTHON_2" filename="init.py" />
<script name="update_scene" run_mode="FRAME" language="PYTHON_2" filename="frame.py" />
</glsl_hacker>
The INIT script: init.py
import gh_utils
import gh_renderer
font = gh_utils.font_create("Tahoma", 14)
The FRAME script: frame.py
gh_renderer.clear_color_buffer(0.4, 0.4, 0.4, 1.0)
x = 10
y = 20
gh_utils.font_render(font, x, y, 1.0, 1.0, 0.0, 1.0, "Hello World from GeeXLab!")
x = 10
y = 40
elapsed_time = gh_utils.get_elapsed_time()
gh_utils.font_render(font, x, y, 1.0, 1.0, 1.0, 1.0, "Elapsed time: " + str(elapsed_time))