PyGLM: OpenGL Mathematics library for Python
PyGLM is an OpenGL Mathematics library for Python, providing vector and matrix types, and mathematical functions for graphics programming. It is a Python extension written in C++ that wraps the popular GLM (OpenGL Mathematics) library. The current version is 2.8.3. It maintains an infrequent but active release cadence, focusing on stability and compatibility with its underlying C++ counterpart.
Common errors
-
AttributeError: module 'glm' has no attribute 'vec2'
cause This error typically occurs when the wrong `glm` package has been installed. There is another package named `glm` on PyPI which is a JSON parser, not the OpenGL Mathematics library.fixEnsure you installed `pyglm` and not `glm`. Uninstall `glm` (`pip uninstall glm`) and then install the correct package (`pip install pyglm`). -
ERROR: Failed building wheel for pyglm
cause PyGLM is a C++ extension and requires C++ build tools to be present on your system if a pre-compiled wheel is not available for your specific Python version and operating system. On Windows, this often means Visual C++ Build Tools.fixOn Windows, install the 'Desktop development with C++' workload from the Visual Studio Installer. On Linux, ensure `build-essential` (Debian/Ubuntu) or `Development Tools` (Fedora/RHEL) are installed.
Warnings
- breaking The direct import `import glm` will be deprecated in PyGLM 3.0. Users should transition to `from pyglm import glm` for future compatibility.
- gotcha PyGLM's syntax differs from the original C++ GLM library in several ways, including using `.` instead of `::` for namespaces (e.g., `glm.vec2`), requiring `_LH` suffix for left-handed coordinate system functions (e.g., `glm.perspectiveLH`), and using default `packed_highp` precision.
- gotcha Certain functions, like `glm.frexp`, may issue a `UserWarning`. These can be silenced if desired.
- gotcha The GLM C++ library, which PyGLM wraps, is licensed under the MIT License. Users are required to include the GLM license (`COPYING` or `GLM_LICENSE` file) in their projects, especially for binary distributions.
Install
-
pip install pyglm
Imports
- glm
from pyglm import glm (before v2.8), import pyglm
import glm
Quickstart
import glm
# Vector operations
v = glm.vec3()
v.x = 7
print(f"v.xxy: {v.xxy}")
# Matrix initialization
m = glm.mat4()
print(f"m:\n{m}")
# Vector arithmetic with iterables
v_sum = glm.vec4(1, 2, 3, 4) + (8, 7, 6, 5)
print(f"v_sum: {v_sum}")