Skip to main content

OpenGL Mathematics library for Python

Project description

PyGLM

OpenGL Mathematics (GLM) library for Python

GLSL + Optional features + Python = PyGLM
A mathematics library for graphics programming.

PyGLM is a Python extension written in C++.
By using GLM by G-Truc under the hood, it manages to bring glm's features to Python.  
Some features are unsupported (such as most unstable extensions).
If you encounter any issues or want to request a feature, please create an issue on the issue tracker.

For a complete reference of the types and functions, please take a look at the wiki.

Tiny Documentation

Why PyGLM?

Besides the obvious - being mostly compatible with GLM - PyGLM offers a variety of features for vector and matrix manipulation.
It has a lot of possible use cases, including 3D-Graphics (OpenGL, DirectX, ...), Physics and more.

At the same time, it has great performance, usually being a lot faster than numpy! (see end of page)
(depending on the individual function)

Installation

PyGLM supports Windows, Linux, MacOS and other operating systems with either x86 (32-bit) or x64 (64-bit) architecture,
running Python 3.5 or higher. (Prior versions of Python - such as Python 2 - were supported up to PyGLM version 0.4.8b1)

It can be installed from the PyPI using pip:

pip install PyGLM
# please make sure to install "PyGLM" and not "glm", which is a different module

And finally imported and used:

import glm

Using PyGLM

PyGLM's syntax is very similar to the original GLM's syntax.
There is no need to import anything but glm, as it already contains the entire package.

For more information, take a look at the wiki.

License requirements

Please make sure to include the license for GLM in your project when you use PyGLM!
(this is especially relevant for binary distributions, e.g. *.exe)

You can do so by copying the COPYING file (or it's contents) to your project.

Differences to glm

Instead of using double colons (::) for namespaces, periods (.) are used, so
glm::vec2 becomes glm.vec2.

PyGLM supports the buffer protocol, meaning its compitible to other objects that support the buffer protocol,
such as bytes or numpy.array
(for example you can convert a glm matrix to a numpy array and vice versa).
PyGLM is also capable of interpreting iterables (such as tuples) as vectors, so e.g. the following equasion is possible:

result = glm.vec2(1) * (2, 3)

Note: This feature might not or only partially be available in PyGLM versions prior to 2.0.0

PyGLM doesn't support precision qualifiers. All types use the default precision (packed_highp).

If a glm function normally accepts float and double arguments, the higher precision (double) is used.

There is no way to set preprocessor definitions (macros).
If - for example - you need to use the left handed coordinate system, you have to use *LH, so
glm.perspective becomes glm.perspectiveLH.

All types are initialized by default to avoid memory access violations.
(i.e. the macro GLM_FORCE_CTOR_INIT is defined)

In case you need the size of a PyGLM datatype, you can use

glm.sizeof(<type>)

The function glm.identity requires a matrix type as it's argument.

The function glm.frexp(x, exp) returns a tuple (m, e), if the input arguments are numerical.
This function may issue a UserWarning. You can silence this warning using glm.silence(1).

The function glm.value_ptr(x) returns a ctypes pointer of the respective type.
I.e. if the datatype of x is float, then a c_float pointer will be returned.
Likewise the reverse-functions (such as make_vec2(ptr)) will take a ctypes pointer as their argument
and return (in this case) a 2 component vector of the pointers underlying type.

glm.silence(ID) can be used to silence specific warnings.
Supplying an id of 0 will silence all warnings.

FAQ

How to pass the matrices generated by PyGLM to OpenGL functions?

You will find an overview on the [Passing data to external libs] page.

Types and functions are not available after installing from the PyPI using pip install glm

Most likely you've installed glm, a JSON parser and not PyGLM (or a very early version of PyGLM).  
The correct install command is:

pip install PyGLM

Why is <experimental extension name here> not supported?

I prefer not to add too many experimental extensions to PyGLM, especially as they might change or be removed in the future and it is simply too much effort for me to keep up with all that.  
If you need a specific experimental extension, feel free to submit a feature request on the issue tracker.  
I try adding them on a one-by-one basis.

Why are Python versions prior to 3.5 no longer supported?

Starting with version 0.5.0b1 I decided to use C++ to build PyGLM, using glm under the hood - which requires C++ 11 or upwards.  
Only Python versions 3.5+ support C++ 11, thus I was forced to stop supporting older versions.  
The last version to support Python 2 and <3.5 is 0.4.8b1.

Short example

>>> import glm
>>> v = glm.vec3()
>>> v.x = 7
>>> print(v.xxy)
vec3(            7,            7,            0 )

>>> m = glm.mat4()
>>> print(m)
[            1 |            0 |            0 |            0 ]
[            0 |            1 |            0 |            0 ]
[            0 |            0 |            1 |            0 ]
[            0 |            0 |            0 |            1 ]

>>> v = glm.vec4(1, 2, 3, 4)
>>> print(v + (8, 7, 6, 5))
vec4(            9,            9,            9,            9 )

PyGLM in action

Wanna see what PyGLM can do?
Take a look at the examples from the popular LearnOpenGL tutorials by Joey De Vries running in Python using PyGLM.
LearnOpenGL

Speed comparison to numpy

The following is the output generated by test/PyGLM vs Numpy.py

Evaluating performance of PyGLM compared to NumPy.

Running on platform 'win32'.

Python version:
3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)]

Comparing the following module versions:
PyGLM (DEFAULT) version 2.5.2
 vs
NumPy version 1.21.3
________________________________________________________________________________

The following table shows information about a task to be achieved and the time
it took when using the given module. Lower time is better.
Each task is repeated ten times per module, only showing the best (i.e. lowest)
value.


+----------------------------------------+------------+------------+-----------+
| Description                            | PyGLM time | NumPy time |     ratio |
+----------------------------------------+------------+------------+-----------+
| 3 component vector creation            |            |            |           |
| (100,000 times)                        |        8ms |       21ms |     2.69x |
+----------------------------------------+------------+------------+-----------+
| 3 component vector creation with       |            |            |           |
| custom components                      |            |            |           |
| (50,000 times)                         |        8ms |       34ms |     4.32x |
+----------------------------------------+------------+------------+-----------+
| dot product                            |            |            |           |
| (50,000 times)                         |        4ms |       49ms |    10.93x |
+----------------------------------------+------------+------------+-----------+
| cross product                          |            |            |           |
| (25,000 times)                         |        2ms |      548ms |   234.34x |
+----------------------------------------+------------+------------+-----------+
| L2-Norm of 3 component vector          |            |            |           |
| (100,000 times)                        |        7ms |      310ms |    44.49x |
+----------------------------------------+------------+------------+-----------+
| 4x4 matrix creation                    |            |            |           |
| (50,000 times)                         |        5ms |       11ms |     2.32x |
+----------------------------------------+------------+------------+-----------+
| 4x4 identity matrix creation           |            |            |           |
| (100,000 times)                        |        7ms |      176ms |    24.05x |
+----------------------------------------+------------+------------+-----------+
| 4x4 matrix transposition               |            |            |           |
| (50,000 times)                         |        5ms |       32ms |     6.19x |
+----------------------------------------+------------+------------+-----------+
| 4x4 multiplicative inverse             |            |            |           |
| (50,000 times)                         |        4ms |     1925ms |   470.77x |
+----------------------------------------+------------+------------+-----------+
| 3 component vector addition            |            |            |           |
| (100,000 times)                        |        5ms |       38ms |     7.17x |
+----------------------------------------+------------+------------+-----------+
| 4x4 matrix multiplication              |            |            |           |
| (100,000 times)                        |        7ms |       39ms |     5.36x |
+----------------------------------------+------------+------------+-----------+
| 4x4 matrix x vector multiplication     |            |            |           |
| (100,000 times)                        |        6ms |      116ms |    20.01x |
+----------------------------------------+------------+------------+-----------+
| TOTAL                                  |      0.07s |      3.30s |    47.78x |
+----------------------------------------+------------+------------+-----------+

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

PyGLM-2.7.1.tar.gz (4.6 MB view hashes)

Uploaded Source

Built Distributions

PyGLM-2.7.1-cp312-cp312-win_arm64.whl (1.2 MB view hashes)

Uploaded CPython 3.12 Windows ARM64

PyGLM-2.7.1-cp312-cp312-win_amd64.whl (1.6 MB view hashes)

Uploaded CPython 3.12 Windows x86-64

PyGLM-2.7.1-cp312-cp312-win32.whl (1.3 MB view hashes)

Uploaded CPython 3.12 Windows x86

PyGLM-2.7.1-cp312-cp312-musllinux_1_1_x86_64.whl (12.1 MB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

PyGLM-2.7.1-cp312-cp312-musllinux_1_1_s390x.whl (12.1 MB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ s390x

PyGLM-2.7.1-cp312-cp312-musllinux_1_1_i686.whl (10.8 MB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ i686

PyGLM-2.7.1-cp312-cp312-musllinux_1_1_aarch64.whl (11.5 MB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ ARM64

PyGLM-2.7.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (10.6 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.27+ x86-64 manylinux: glibc 2.28+ x86-64

PyGLM-2.7.1-cp312-cp312-manylinux_2_27_s390x.manylinux_2_28_s390x.whl (10.7 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.27+ s390x manylinux: glibc 2.28+ s390x

PyGLM-2.7.1-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (10.1 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.27+ ARM64 manylinux: glibc 2.28+ ARM64

PyGLM-2.7.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.1 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

PyGLM-2.7.1-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl (11.1 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ s390x

PyGLM-2.7.1-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl (10.0 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ i686

PyGLM-2.7.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (10.5 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

PyGLM-2.7.1-cp312-cp312-macosx_11_0_arm64.whl (1.4 MB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

PyGLM-2.7.1-cp312-cp312-macosx_10_9_x86_64.whl (1.7 MB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

PyGLM-2.7.1-cp311-cp311-win_arm64.whl (1.2 MB view hashes)

Uploaded CPython 3.11 Windows ARM64

PyGLM-2.7.1-cp311-cp311-win_amd64.whl (1.6 MB view hashes)

Uploaded CPython 3.11 Windows x86-64

PyGLM-2.7.1-cp311-cp311-win32.whl (1.3 MB view hashes)

Uploaded CPython 3.11 Windows x86

PyGLM-2.7.1-cp311-cp311-musllinux_1_1_x86_64.whl (12.1 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

PyGLM-2.7.1-cp311-cp311-musllinux_1_1_s390x.whl (12.0 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ s390x

PyGLM-2.7.1-cp311-cp311-musllinux_1_1_i686.whl (10.8 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ i686

PyGLM-2.7.1-cp311-cp311-musllinux_1_1_aarch64.whl (11.4 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ ARM64

PyGLM-2.7.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (10.5 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.27+ x86-64 manylinux: glibc 2.28+ x86-64

PyGLM-2.7.1-cp311-cp311-manylinux_2_27_s390x.manylinux_2_28_s390x.whl (10.6 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.27+ s390x manylinux: glibc 2.28+ s390x

PyGLM-2.7.1-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (10.0 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.27+ ARM64 manylinux: glibc 2.28+ ARM64

PyGLM-2.7.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.0 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

PyGLM-2.7.1-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl (11.0 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ s390x

PyGLM-2.7.1-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl (9.9 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ i686

PyGLM-2.7.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (10.4 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

PyGLM-2.7.1-cp311-cp311-macosx_11_0_arm64.whl (1.4 MB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

PyGLM-2.7.1-cp311-cp311-macosx_10_9_x86_64.whl (1.7 MB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

PyGLM-2.7.1-cp310-cp310-win_arm64.whl (1.2 MB view hashes)

Uploaded CPython 3.10 Windows ARM64

PyGLM-2.7.1-cp310-cp310-win_amd64.whl (1.6 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

PyGLM-2.7.1-cp310-cp310-win32.whl (1.3 MB view hashes)

Uploaded CPython 3.10 Windows x86

PyGLM-2.7.1-cp310-cp310-musllinux_1_1_x86_64.whl (11.7 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

PyGLM-2.7.1-cp310-cp310-musllinux_1_1_s390x.whl (11.7 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ s390x

PyGLM-2.7.1-cp310-cp310-musllinux_1_1_i686.whl (10.5 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

PyGLM-2.7.1-cp310-cp310-musllinux_1_1_aarch64.whl (11.1 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ ARM64

PyGLM-2.7.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (10.1 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.27+ x86-64 manylinux: glibc 2.28+ x86-64

PyGLM-2.7.1-cp310-cp310-manylinux_2_27_s390x.manylinux_2_28_s390x.whl (10.1 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.27+ s390x manylinux: glibc 2.28+ s390x

PyGLM-2.7.1-cp310-cp310-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (9.6 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.27+ ARM64 manylinux: glibc 2.28+ ARM64

PyGLM-2.7.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.5 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

PyGLM-2.7.1-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl (10.4 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ s390x

PyGLM-2.7.1-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl (9.4 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686

PyGLM-2.7.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (9.9 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

PyGLM-2.7.1-cp310-cp310-macosx_11_0_arm64.whl (1.4 MB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

PyGLM-2.7.1-cp310-cp310-macosx_10_9_x86_64.whl (1.7 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

PyGLM-2.7.1-cp39-cp39-win_arm64.whl (1.2 MB view hashes)

Uploaded CPython 3.9 Windows ARM64

PyGLM-2.7.1-cp39-cp39-win_amd64.whl (1.6 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

PyGLM-2.7.1-cp39-cp39-win32.whl (1.3 MB view hashes)

Uploaded CPython 3.9 Windows x86

PyGLM-2.7.1-cp39-cp39-musllinux_1_1_x86_64.whl (11.5 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

PyGLM-2.7.1-cp39-cp39-musllinux_1_1_s390x.whl (11.5 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ s390x

PyGLM-2.7.1-cp39-cp39-musllinux_1_1_i686.whl (10.3 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

PyGLM-2.7.1-cp39-cp39-musllinux_1_1_aarch64.whl (10.9 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ ARM64

PyGLM-2.7.1-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (10.0 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.27+ x86-64 manylinux: glibc 2.28+ x86-64

PyGLM-2.7.1-cp39-cp39-manylinux_2_27_s390x.manylinux_2_28_s390x.whl (10.0 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.27+ s390x manylinux: glibc 2.28+ s390x

PyGLM-2.7.1-cp39-cp39-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (9.5 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.27+ ARM64 manylinux: glibc 2.28+ ARM64

PyGLM-2.7.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.4 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

PyGLM-2.7.1-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl (10.3 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ s390x

PyGLM-2.7.1-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl (9.3 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686

PyGLM-2.7.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (9.8 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

PyGLM-2.7.1-cp39-cp39-macosx_11_0_arm64.whl (1.4 MB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

PyGLM-2.7.1-cp39-cp39-macosx_10_9_x86_64.whl (1.7 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

PyGLM-2.7.1-cp38-cp38-win_amd64.whl (1.6 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

PyGLM-2.7.1-cp38-cp38-win32.whl (1.3 MB view hashes)

Uploaded CPython 3.8 Windows x86

PyGLM-2.7.1-cp38-cp38-musllinux_1_1_x86_64.whl (11.3 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

PyGLM-2.7.1-cp38-cp38-musllinux_1_1_s390x.whl (11.3 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ s390x

PyGLM-2.7.1-cp38-cp38-musllinux_1_1_i686.whl (10.1 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ i686

PyGLM-2.7.1-cp38-cp38-musllinux_1_1_aarch64.whl (10.7 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ ARM64

PyGLM-2.7.1-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (9.7 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.27+ x86-64 manylinux: glibc 2.28+ x86-64

PyGLM-2.7.1-cp38-cp38-manylinux_2_27_s390x.manylinux_2_28_s390x.whl (9.7 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.27+ s390x manylinux: glibc 2.28+ s390x

PyGLM-2.7.1-cp38-cp38-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (9.2 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.27+ ARM64 manylinux: glibc 2.28+ ARM64

PyGLM-2.7.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.1 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

PyGLM-2.7.1-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl (10.0 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ s390x

PyGLM-2.7.1-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl (9.0 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686

PyGLM-2.7.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (9.4 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

PyGLM-2.7.1-cp38-cp38-macosx_11_0_arm64.whl (1.4 MB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

PyGLM-2.7.1-cp38-cp38-macosx_10_9_x86_64.whl (1.7 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page