Merge branch 'blender-v4.4-release'
[blender.git] / source / blender / python / intern / bpy_app_opensubdiv.cc
blob9f82b4870c98150975cb0b4db81e0070f5490097
1 /* SPDX-FileCopyrightText: 2023 Blender Authors
3 * SPDX-License-Identifier: GPL-2.0-or-later */
5 /** \file
6 * \ingroup pythonintern
7 */
9 #include "BLI_utildefines.h"
10 #include <Python.h>
12 #include "bpy_app_opensubdiv.hh"
14 #include "../generic/py_capi_utils.hh"
16 #ifdef WITH_OPENSUBDIV
17 # include "opensubdiv_capi.hh"
18 #endif
20 static PyTypeObject BlenderAppOpenSubdivType;
22 static PyStructSequence_Field app_opensubdiv_info_fields[] = {
23 {"supported", "Boolean, True when Blender is built with OpenSubdiv support"},
24 {"version", "The OpenSubdiv version as a tuple of 3 numbers"},
25 {"version_string", "The OpenSubdiv version formatted as a string"},
26 {nullptr},
29 static PyStructSequence_Desc app_opensubdiv_info_desc = {
30 /*name*/ "bpy.app.opensubdiv",
31 /*doc*/ "This module contains information about OpenSubdiv blender is linked against",
32 /*fields*/ app_opensubdiv_info_fields,
33 /*n_in_sequence*/ ARRAY_SIZE(app_opensubdiv_info_fields) - 1,
36 static PyObject *make_opensubdiv_info()
38 PyObject *opensubdiv_info;
39 int pos = 0;
41 opensubdiv_info = PyStructSequence_New(&BlenderAppOpenSubdivType);
42 if (opensubdiv_info == nullptr) {
43 return nullptr;
46 #ifndef WITH_OPENSUBDIV
47 # define SetStrItem(str) \
48 PyStructSequence_SET_ITEM(opensubdiv_info, pos++, PyUnicode_FromString(str))
49 #endif
51 #define SetObjItem(obj) PyStructSequence_SET_ITEM(opensubdiv_info, pos++, obj)
53 #ifdef WITH_OPENSUBDIV
54 const int curversion = openSubdiv_getVersionHex();
55 SetObjItem(PyBool_FromLong(1));
56 SetObjItem(PyC_Tuple_Pack_I32({curversion / 10000, (curversion / 100) % 100, curversion % 100}));
57 SetObjItem(PyUnicode_FromFormat(
58 "%2d, %2d, %2d", curversion / 10000, (curversion / 100) % 100, curversion % 100));
59 #else
60 SetObjItem(PyBool_FromLong(0));
61 SetObjItem(PyC_Tuple_Pack_I32({0, 0, 0}));
62 SetStrItem("Unknown");
63 #endif
65 if (UNLIKELY(PyErr_Occurred())) {
66 Py_DECREF(opensubdiv_info);
67 return nullptr;
70 #undef SetStrItem
71 #undef SetObjItem
73 return opensubdiv_info;
76 PyObject *BPY_app_opensubdiv_struct()
78 PyObject *ret;
80 PyStructSequence_InitType(&BlenderAppOpenSubdivType, &app_opensubdiv_info_desc);
82 ret = make_opensubdiv_info();
84 /* prevent user from creating new instances */
85 BlenderAppOpenSubdivType.tp_init = nullptr;
86 BlenderAppOpenSubdivType.tp_new = nullptr;
87 /* Without this we can't do `set(sys.modules)` #29635. */
88 BlenderAppOpenSubdivType.tp_hash = (hashfunc)_Py_HashPointer;
90 return ret;