[Bf-blender-cvs] [0a8d21e0c96] master: PyAPI: re-enable the "bgl" module for headless builds

Campbell Barton noreply at git.blender.org
Sat Jul 16 09:33:05 CEST 2022


Commit: 0a8d21e0c96e2f9c7f6f990d756f5abe011ed805
Author: Campbell Barton
Date:   Sat Jul 16 17:28:09 2022 +1000
Branches: master
https://developer.blender.org/rB0a8d21e0c96e2f9c7f6f990d756f5abe011ed805

PyAPI: re-enable the "bgl" module for headless builds

Instead of removing the `bgl` module, set all it's functions to stubs
so importing `bgl` or any of it's members doesn't raise an error.

This avoids problems for scripts that import bgl but don't call it's
functions when running in background mode.

===================================================================

M	source/blender/python/generic/CMakeLists.txt
M	source/blender/python/generic/bgl.c
M	source/blender/python/intern/bpy_interface.c

===================================================================

diff --git a/source/blender/python/generic/CMakeLists.txt b/source/blender/python/generic/CMakeLists.txt
index dfca528e758..69bcfdfae4e 100644
--- a/source/blender/python/generic/CMakeLists.txt
+++ b/source/blender/python/generic/CMakeLists.txt
@@ -17,6 +17,7 @@ set(INC_SYS
 )
 
 set(SRC
+  bgl.c
   bl_math_py_api.c
   blf_py_api.c
   bpy_threads.c
@@ -26,6 +27,7 @@ set(SRC
   py_capi_rna.c
   py_capi_utils.c
 
+  bgl.h
   bl_math_py_api.h
   blf_py_api.h
   idprop_py_api.h
@@ -38,14 +40,6 @@ set(SRC
   python_utildefines.h
 )
 
-if(WITH_OPENGL)
-  list(APPEND SRC
-    bgl.c
-
-    bgl.h
-  )
-endif()
-
 set(LIB
   ${GLEW_LIBRARY}
   ${PYTHON_LINKFLAGS}
diff --git a/source/blender/python/generic/bgl.c b/source/blender/python/generic/bgl.c
index 92ed0c51b1f..f5c1f060e80 100644
--- a/source/blender/python/generic/bgl.c
+++ b/source/blender/python/generic/bgl.c
@@ -1082,18 +1082,34 @@ static PyObject *Buffer_repr(Buffer *self)
 /** \name OpenGL API Wrapping
  * \{ */
 
-#define BGL_Wrap(funcname, ret, arg_list) \
-  static PyObject *Method_##funcname(PyObject *UNUSED(self), PyObject *args) \
-  { \
-    arg_def arg_list; \
-    ret_def_##ret; \
-    if (!PyArg_ParseTuple(args, arg_str arg_list, arg_ref arg_list)) { \
+#ifdef WITH_OPENGL
+#  define BGL_Wrap(funcname, ret, arg_list) \
+    static PyObject *Method_##funcname(PyObject *UNUSED(self), PyObject *args) \
+    { \
+      arg_def arg_list; \
+      ret_def_##ret; \
+      if (!PyArg_ParseTuple(args, arg_str arg_list, arg_ref arg_list)) { \
+        return NULL; \
+      } \
+      GPU_bgl_start(); \
+      ret_set_##ret gl##funcname(arg_var arg_list); \
+      ret_ret_##ret; \
+    }
+#else
+
+static void bgl_no_opengl_error(void)
+{
+  PyErr_SetString(PyExc_RuntimeError, "Built without OpenGL support");
+}
+
+#  define BGL_Wrap(funcname, ret, arg_list) \
+    static PyObject *Method_##funcname(PyObject *UNUSED(self), PyObject *args) \
+    { \
+      (void)args; \
+      bgl_no_opengl_error(); \
       return NULL; \
-    } \
-    GPU_bgl_start(); \
-    ret_set_##ret gl##funcname(arg_var arg_list); \
-    ret_ret_##ret; \
-  }
+    }
+#endif
 
 /* GL_VERSION_1_0 */
 BGL_Wrap(BlendFunc, void, (GLenum, GLenum));
@@ -1421,12 +1437,22 @@ static void py_module_dict_add_method(PyObject *submodule,
 #ifdef __GNUC__
 #  pragma GCC diagnostic ignored "-Waddress"
 #endif
-#define PY_MOD_ADD_METHOD(func) \
-  { \
-    static PyMethodDef method_def = {"gl" #func, Method_##func, METH_VARARGS}; \
-    py_module_dict_add_method(submodule, dict, &method_def, (gl##func != NULL)); \
-  } \
-  ((void)0)
+
+#ifdef WITH_OPENGL
+#  define PY_MOD_ADD_METHOD(func) \
+    { \
+      static PyMethodDef method_def = {"gl" #func, Method_##func, METH_VARARGS}; \
+      py_module_dict_add_method(submodule, dict, &method_def, (gl##func != NULL)); \
+    } \
+    ((void)0)
+#else
+#  define PY_MOD_ADD_METHOD(func) \
+    { \
+      static PyMethodDef method_def = {"gl" #func, Method_##func, METH_VARARGS}; \
+      py_module_dict_add_method(submodule, dict, &method_def, false); \
+    } \
+    ((void)0)
+#endif
 
 static void init_bgl_version_1_0_methods(PyObject *submodule, PyObject *dict)
 {
@@ -2620,9 +2646,13 @@ static PyObject *Method_ShaderSource(PyObject *UNUSED(self), PyObject *args)
     return NULL;
   }
 
+#ifdef WITH_OPENGL
   glShaderSource(shader, 1, (const char **)&source, NULL);
-
   Py_RETURN_NONE;
+#else
+  bgl_no_opengl_error();
+  return NULL;
+#endif
 }
 
 /** \} */
diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c
index 08dd5fe9cfc..939fa475344 100644
--- a/source/blender/python/intern/bpy_interface.c
+++ b/source/blender/python/intern/bpy_interface.c
@@ -259,9 +259,7 @@ static struct _inittab bpy_internal_modules[] = {
     {"mathutils.kdtree", PyInit_mathutils_kdtree},
 #endif
     {"_bpy_path", BPyInit__bpy_path},
-#ifdef WITH_OPENGL
     {"bgl", BPyInit_bgl},
-#endif
     {"blf", BPyInit_blf},
     {"bl_math", BPyInit_bl_math},
     {"imbuf", BPyInit_imbuf},



More information about the Bf-blender-cvs mailing list