Merge branch 'blender-v4.4-release'
[blender.git] / source / blender / python / intern / bpy_app_ocio.cc
blob39cc0f2c49249b2e3b9dd9bf5e6e57312c2f56d4
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_ocio.hh"
14 #include "../generic/py_capi_utils.hh"
16 #ifdef WITH_OCIO
17 # include "ocio_capi.h"
18 #endif
20 static PyTypeObject BlenderAppOCIOType;
22 static PyStructSequence_Field app_ocio_info_fields[] = {
23 {"supported", "Boolean, True when Blender is built with OpenColorIO support"},
24 {"version", "The OpenColorIO version as a tuple of 3 numbers"},
25 {"version_string", "The OpenColorIO version formatted as a string"},
26 {nullptr},
29 static PyStructSequence_Desc app_ocio_info_desc = {
30 /*name*/ "bpy.app.ocio",
31 /*doc*/ "This module contains information about OpenColorIO blender is linked against",
32 /*fields*/ app_ocio_info_fields,
33 /*n_in_sequence*/ ARRAY_SIZE(app_ocio_info_fields) - 1,
36 static PyObject *make_ocio_info()
38 PyObject *ocio_info;
39 int pos = 0;
41 #ifdef WITH_OCIO
42 int curversion;
43 #endif
45 ocio_info = PyStructSequence_New(&BlenderAppOCIOType);
46 if (ocio_info == nullptr) {
47 return nullptr;
50 #ifndef WITH_OCIO
51 # define SetStrItem(str) PyStructSequence_SET_ITEM(ocio_info, pos++, PyUnicode_FromString(str))
52 #endif
54 #define SetObjItem(obj) PyStructSequence_SET_ITEM(ocio_info, pos++, obj)
56 #ifdef WITH_OCIO
57 curversion = OCIO_getVersionHex();
58 SetObjItem(PyBool_FromLong(1));
59 SetObjItem(
60 PyC_Tuple_Pack_I32({curversion >> 24, (curversion >> 16) % 256, (curversion >> 8) % 256}));
61 SetObjItem(PyUnicode_FromFormat(
62 "%2d, %2d, %2d", curversion >> 24, (curversion >> 16) % 256, (curversion >> 8) % 256));
63 #else
64 SetObjItem(PyBool_FromLong(0));
65 SetObjItem(PyC_Tuple_Pack_I32({0, 0, 0}));
66 SetStrItem("Unknown");
67 #endif
69 if (UNLIKELY(PyErr_Occurred())) {
70 Py_DECREF(ocio_info);
71 return nullptr;
74 #undef SetStrItem
75 #undef SetObjItem
77 return ocio_info;
80 PyObject *BPY_app_ocio_struct()
82 PyObject *ret;
84 PyStructSequence_InitType(&BlenderAppOCIOType, &app_ocio_info_desc);
86 ret = make_ocio_info();
88 /* prevent user from creating new instances */
89 BlenderAppOCIOType.tp_init = nullptr;
90 BlenderAppOCIOType.tp_new = nullptr;
91 /* Without this we can't do `set(sys.modules)` #29635. */
92 BlenderAppOCIOType.tp_hash = (hashfunc)_Py_HashPointer;
94 return ret;