syphon.utils.opengl

 1from importlib import import_module
 2from typing import Any
 3
 4import AppKit
 5
 6from syphon.utils.exceptions import CGLContextNotFoundException, NSOpenGLContextNotFoundException
 7
 8# Keep the public texture-target default available without importing PyOpenGL.
 9GL_TEXTURE_2D = 0x0DE1
10
11
12def _require_pyopengl():
13    try:
14        return import_module("OpenGL.GL")
15    except ModuleNotFoundError as exc:
16        if exc.name not in {"OpenGL", "OpenGL.GL"}:
17            raise
18        raise ImportError(
19            "OpenGL support requires PyOpenGL. Install it with: pip install 'syphon-python[opengl]'"
20        ) from exc
21
22
23def get_current_cgl_context_obj() -> Any:
24    """
25    Get the CGL context object for the current NSOpenGLContext.
26
27    Returns:
28    - Any: The CGL context object.
29
30    Raises:
31    - NSOpenGLContextNotFoundException: If the current NSOpenGLContext cannot be retrieved.
32    - CGLContextNotFoundException: If the CGLContextObj cannot be retrieved from NSOpenGLContext.
33    """
34    ns_ctx = AppKit.NSOpenGLContext.currentContext()
35
36    if ns_ctx is None:
37        raise NSOpenGLContextNotFoundException()
38
39    cgl_context = ns_ctx.CGLContextObj()
40
41    if cgl_context is None:
42        raise CGLContextNotFoundException()
43
44    return cgl_context
GL_TEXTURE_2D = 3553
def get_current_cgl_context_obj() -> Any:
24def get_current_cgl_context_obj() -> Any:
25    """
26    Get the CGL context object for the current NSOpenGLContext.
27
28    Returns:
29    - Any: The CGL context object.
30
31    Raises:
32    - NSOpenGLContextNotFoundException: If the current NSOpenGLContext cannot be retrieved.
33    - CGLContextNotFoundException: If the CGLContextObj cannot be retrieved from NSOpenGLContext.
34    """
35    ns_ctx = AppKit.NSOpenGLContext.currentContext()
36
37    if ns_ctx is None:
38        raise NSOpenGLContextNotFoundException()
39
40    cgl_context = ns_ctx.CGLContextObj()
41
42    if cgl_context is None:
43        raise CGLContextNotFoundException()
44
45    return cgl_context

Get the CGL context object for the current NSOpenGLContext.

Returns:

  • Any: The CGL context object.

Raises:

  • NSOpenGLContextNotFoundException: If the current NSOpenGLContext cannot be retrieved.
  • CGLContextNotFoundException: If the CGLContextObj cannot be retrieved from NSOpenGLContext.