Blender  V2.93
bpy_app_usd.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2020 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include "BLI_utildefines.h"
25 #include <Python.h>
26 
27 #include "bpy_app_usd.h"
28 
29 #include "../generic/py_capi_utils.h"
30 
31 #ifdef WITH_USD
32 # include "usd.h"
33 #endif
34 
35 static PyTypeObject BlenderAppUSDType;
36 
37 static PyStructSequence_Field app_usd_info_fields[] = {
38  {"supported", "Boolean, True when Blender is built with USD support"},
39  {"version", "The USD version as a tuple of 3 numbers"},
40  {"version_string", "The USD version formatted as a string"},
41  {NULL},
42 };
43 
44 static PyStructSequence_Desc app_usd_info_desc = {
45  "bpy.app.usd", /* name */
46  "This module contains information about the Universal Scene Description library Bender is "
47  "linked against", /* doc */
48  app_usd_info_fields, /* fields */
50 };
51 
52 static PyObject *make_usd_info(void)
53 {
54  PyObject *usd_info = PyStructSequence_New(&BlenderAppUSDType);
55 
56  if (usd_info == NULL) {
57  return NULL;
58  }
59 
60  int pos = 0;
61 
62 #ifndef WITH_USD
63 # define SetStrItem(str) PyStructSequence_SET_ITEM(usd_info, pos++, PyUnicode_FromString(str))
64 #endif
65 
66 #define SetObjItem(obj) PyStructSequence_SET_ITEM(usd_info, pos++, obj)
67 
68 #ifdef WITH_USD
69  const int curversion = USD_get_version();
70  const int major = curversion / 10000;
71  const int minor = (curversion / 100) % 100;
72  const int patch = curversion % 100;
73 
74  SetObjItem(PyBool_FromLong(1));
75  SetObjItem(PyC_Tuple_Pack_I32(major, minor, patch));
76  SetObjItem(PyUnicode_FromFormat("%2d, %2d, %2d", major, minor, patch));
77 #else
78  SetObjItem(PyBool_FromLong(0));
80  SetStrItem("Unknown");
81 #endif
82 
83  if (UNLIKELY(PyErr_Occurred())) {
84  Py_DECREF(usd_info);
85  return NULL;
86  }
87 
88 #undef SetStrItem
89 #undef SetObjItem
90 
91  return usd_info;
92 }
93 
94 PyObject *BPY_app_usd_struct(void)
95 {
96  PyStructSequence_InitType(&BlenderAppUSDType, &app_usd_info_desc);
97 
98  PyObject *ret = make_usd_info();
99 
100  /* prevent user from creating new instances */
101  BlenderAppUSDType.tp_init = NULL;
102  BlenderAppUSDType.tp_new = NULL;
103  BlenderAppUSDType.tp_hash = (hashfunc)
104  _Py_HashPointer; /* without this we can't do set(sys.modules) T29635. */
105 
106  return ret;
107 }
#define ARRAY_SIZE(arr)
#define UNLIKELY(x)
static PyStructSequence_Desc app_usd_info_desc
Definition: bpy_app_usd.c:44
static PyTypeObject BlenderAppUSDType
Definition: bpy_app_usd.c:35
PyObject * BPY_app_usd_struct(void)
Definition: bpy_app_usd.c:94
static PyObject * make_usd_info(void)
Definition: bpy_app_usd.c:52
static PyStructSequence_Field app_usd_info_fields[]
Definition: bpy_app_usd.c:37
#define SetStrItem(str)
#define SetObjItem(obj)
uint pos
#define PyC_Tuple_Pack_I32(...)
Definition: py_capi_utils.h:67
return ret
int USD_get_version(void)
Definition: usd_capi.cc:240