Blender V5.3
bpy_app_usd.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_utildefines.hh"
10#include <Python.h>
11
12#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
13
14#include "bpy_app_usd.hh"
15
17
18#ifdef WITH_USD
19# include "usd.hh"
20#endif
21
22namespace blender {
23
24static PyTypeObject BlenderAppUSDType;
25
26static PyStructSequence_Field app_usd_info_fields[] = {
27 {"supported", "Boolean, True when Blender is built with USD support"},
28 {"version", "The USD version as a tuple of 3 numbers"},
29 {"version_string", "The USD version formatted as a string"},
30 {nullptr},
31};
32
33static PyStructSequence_Desc app_usd_info_desc = {
34 /*name*/ "bpy.app.usd",
35 /*doc*/
36 "This module contains information about the Universal Scene Description library Bender is "
37 "linked against",
38 /*fields*/ app_usd_info_fields,
39 /*n_in_sequence*/ ARRAY_SIZE(app_usd_info_fields) - 1,
40};
41
42static PyObject *make_usd_info()
43{
44 PyObject *usd_info = PyStructSequence_New(&BlenderAppUSDType);
45
46 if (usd_info == nullptr) {
47 return nullptr;
48 }
49
50 int pos = 0;
51
52#ifndef WITH_USD
53# define SetStrItem(str) PyStructSequence_SET_ITEM(usd_info, pos++, PyUnicode_FromString(str))
54#endif
55
56#define SetObjItem(obj) PyStructSequence_SET_ITEM(usd_info, pos++, obj)
57
58#ifdef WITH_USD
59 const int curversion = io::usd::USD_get_version();
60 const int major = curversion / 10000;
61 const int minor = (curversion / 100) % 100;
62 const int patch = curversion % 100;
63
64 SetObjItem(PyBool_FromLong(1));
65 SetObjItem(PyC_Tuple_Pack_I32({major, minor, patch}));
66 SetObjItem(PyUnicode_FromFormat("%2d, %2d, %2d", major, minor, patch));
67#else
68 SetObjItem(PyBool_FromLong(0));
70 SetStrItem("Unknown");
71#endif
72
73 if (PyErr_Occurred()) [[unlikely]] {
74 Py_DECREF(usd_info);
75 return nullptr;
76 }
77
78#undef SetStrItem
79#undef SetObjItem
80
81 return usd_info;
82}
83
85{
86 PyStructSequence_InitType(&BlenderAppUSDType, &app_usd_info_desc);
87
88 PyObject *ret = make_usd_info();
89
90 /* prevent user from creating new instances */
91 BlenderAppUSDType.tp_init = nullptr;
92 BlenderAppUSDType.tp_new = nullptr;
93 /* Without this we can't do `set(sys.modules)` #29635. */
94 BlenderAppUSDType.tp_hash = reinterpret_cast<hashfunc>(Py_HashPointer);
95
96 return ret;
97}
98
99} // namespace blender
#define SetStrItem(str)
#define SetObjItem(obj)
BMesh Iterators.
static PyStructSequence_Desc app_usd_info_desc
PyObject * PyC_Tuple_Pack_I32(const Span< int > values)
PyObject * BPY_app_usd_struct()
static PyObject * make_usd_info()
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
static PyTypeObject BlenderAppUSDType
static PyStructSequence_Field app_usd_info_fields[]
header-only compatibility defines.
#define Py_HashPointer