Blender V5.0
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.h"
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
22static PyTypeObject BlenderAppUSDType;
23
24static PyStructSequence_Field app_usd_info_fields[] = {
25 {"supported", "Boolean, True when Blender is built with USD support"},
26 {"version", "The USD version as a tuple of 3 numbers"},
27 {"version_string", "The USD version formatted as a string"},
28 {nullptr},
29};
30
31static PyStructSequence_Desc app_usd_info_desc = {
32 /*name*/ "bpy.app.usd",
33 /*doc*/
34 "This module contains information about the Universal Scene Description library Bender is "
35 "linked against",
36 /*fields*/ app_usd_info_fields,
37 /*n_in_sequence*/ ARRAY_SIZE(app_usd_info_fields) - 1,
38};
39
40static PyObject *make_usd_info()
41{
42 PyObject *usd_info = PyStructSequence_New(&BlenderAppUSDType);
43
44 if (usd_info == nullptr) {
45 return nullptr;
46 }
47
48 int pos = 0;
49
50#ifndef WITH_USD
51# define SetStrItem(str) PyStructSequence_SET_ITEM(usd_info, pos++, PyUnicode_FromString(str))
52#endif
53
54#define SetObjItem(obj) PyStructSequence_SET_ITEM(usd_info, pos++, obj)
55
56#ifdef WITH_USD
57 const int curversion = blender::io::usd::USD_get_version();
58 const int major = curversion / 10000;
59 const int minor = (curversion / 100) % 100;
60 const int patch = curversion % 100;
61
62 SetObjItem(PyBool_FromLong(1));
63 SetObjItem(PyC_Tuple_Pack_I32({major, minor, patch}));
64 SetObjItem(PyUnicode_FromFormat("%2d, %2d, %2d", major, minor, patch));
65#else
66 SetObjItem(PyBool_FromLong(0));
68 SetStrItem("Unknown");
69#endif
70
71 if (UNLIKELY(PyErr_Occurred())) {
72 Py_DECREF(usd_info);
73 return nullptr;
74 }
75
76#undef SetStrItem
77#undef SetObjItem
78
79 return usd_info;
80}
81
83{
84 PyStructSequence_InitType(&BlenderAppUSDType, &app_usd_info_desc);
85
86 PyObject *ret = make_usd_info();
87
88 /* prevent user from creating new instances */
89 BlenderAppUSDType.tp_init = nullptr;
90 BlenderAppUSDType.tp_new = nullptr;
91 /* Without this we can't do `set(sys.modules)` #29635. */
92 BlenderAppUSDType.tp_hash = (hashfunc)Py_HashPointer;
93
94 return ret;
95}
#define ARRAY_SIZE(arr)
#define UNLIKELY(x)
static PyStructSequence_Desc app_usd_info_desc
static PyTypeObject BlenderAppUSDType
static PyObject * make_usd_info()
PyObject * BPY_app_usd_struct()
static PyStructSequence_Field app_usd_info_fields[]
#define SetStrItem(str)
#define SetObjItem(obj)
uint pos
PyObject * PyC_Tuple_Pack_I32(const blender::Span< int > values)
header-only compatibility defines.
#define Py_HashPointer
Py_DECREF(oname)
return ret