Merge branch 'blender-v4.4-release'
[blender.git] / source / blender / python / intern / bpy_app_usd.cc
blob3cb9fc7e696a14b27cac1dd40405b3eacbcc0327
1 /* SPDX-FileCopyrightText: 2020 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_usd.hh"
14 #include "../generic/py_capi_utils.hh"
16 #ifdef WITH_USD
17 # include "usd.hh"
18 #endif
20 static PyTypeObject BlenderAppUSDType;
22 static PyStructSequence_Field app_usd_info_fields[] = {
23 {"supported", "Boolean, True when Blender is built with USD support"},
24 {"version", "The USD version as a tuple of 3 numbers"},
25 {"version_string", "The USD version formatted as a string"},
26 {nullptr},
29 static PyStructSequence_Desc app_usd_info_desc = {
30 /*name*/ "bpy.app.usd",
31 /*doc*/
32 "This module contains information about the Universal Scene Description library Bender is "
33 "linked against",
34 /*fields*/ app_usd_info_fields,
35 /*n_in_sequence*/ ARRAY_SIZE(app_usd_info_fields) - 1,
38 static PyObject *make_usd_info()
40 PyObject *usd_info = PyStructSequence_New(&BlenderAppUSDType);
42 if (usd_info == nullptr) {
43 return nullptr;
46 int pos = 0;
48 #ifndef WITH_USD
49 # define SetStrItem(str) PyStructSequence_SET_ITEM(usd_info, pos++, PyUnicode_FromString(str))
50 #endif
52 #define SetObjItem(obj) PyStructSequence_SET_ITEM(usd_info, pos++, obj)
54 #ifdef WITH_USD
55 const int curversion = blender::io::usd::USD_get_version();
56 const int major = curversion / 10000;
57 const int minor = (curversion / 100) % 100;
58 const int patch = curversion % 100;
60 SetObjItem(PyBool_FromLong(1));
61 SetObjItem(PyC_Tuple_Pack_I32({major, minor, patch}));
62 SetObjItem(PyUnicode_FromFormat("%2d, %2d, %2d", major, minor, patch));
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(usd_info);
71 return nullptr;
74 #undef SetStrItem
75 #undef SetObjItem
77 return usd_info;
80 PyObject *BPY_app_usd_struct()
82 PyStructSequence_InitType(&BlenderAppUSDType, &app_usd_info_desc);
84 PyObject *ret = make_usd_info();
86 /* prevent user from creating new instances */
87 BlenderAppUSDType.tp_init = nullptr;
88 BlenderAppUSDType.tp_new = nullptr;
89 /* Without this we can't do `set(sys.modules)` #29635. */
90 BlenderAppUSDType.tp_hash = (hashfunc)_Py_HashPointer;
92 return ret;