Blender V5.3
bpy_rna_data.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
15
16#include <Python.h>
17#include <cstddef>
18
20#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
21
22#include "BLI_path_utils.hh"
23#include "BLI_string.hh"
24
25#include "BKE_global.hh"
26#include "BKE_main.hh"
27
28#include "RNA_access.hh"
29#include "RNA_prototypes.hh"
30
31#include "bpy_rna.hh"
32#include "bpy_rna_data.hh"
33
34namespace blender {
35
37 PyObject_HEAD /* Required Python macro. */
40};
41
42static PyObject *bpy_rna_data_temp_data(PyObject *self, PyObject *args, PyObject *kw);
43static PyObject *bpy_rna_data_context_enter(BPy_DataContext *self);
44static PyObject *bpy_rna_data_context_exit(BPy_DataContext *self, PyObject *args);
45
46#ifdef __GNUC__
47# ifdef __clang__
48# pragma clang diagnostic push
49# pragma clang diagnostic ignored "-Wcast-function-type"
50# else
51# pragma GCC diagnostic push
52# pragma GCC diagnostic ignored "-Wcast-function-type"
53# endif
54#endif
55
56static PyMethodDef bpy_rna_data_context_methods[] = {
57 {"__enter__", reinterpret_cast<PyCFunction>(bpy_rna_data_context_enter), METH_NOARGS},
58 {"__exit__", reinterpret_cast<PyCFunction>(bpy_rna_data_context_exit), METH_VARARGS},
59 {nullptr} /* sentinel */
60};
61
62#ifdef __GNUC__
63# ifdef __clang__
64# pragma clang diagnostic pop
65# else
66# pragma GCC diagnostic pop
67# endif
68#endif
69
70static int bpy_rna_data_context_traverse(BPy_DataContext *self, visitproc visit, void *arg)
71{
72 Py_VISIT(self->data_rna);
73 return 0;
74}
75
77{
78 Py_CLEAR(self->data_rna);
79 return 0;
80}
81
83{
84 PyObject_GC_UnTrack(self);
85 Py_CLEAR(self->data_rna);
86 PyObject_GC_Del(self);
87}
88static PyTypeObject bpy_rna_data_context_Type = {
89 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
90 /*tp_name*/ "bpy_rna_data_context",
91 /*tp_basicsize*/ sizeof(BPy_DataContext),
92 /*tp_itemsize*/ 0,
93 /*tp_dealloc*/ reinterpret_cast<destructor>(bpy_rna_data_context_dealloc),
94 /*tp_vectorcall_offset*/ 0,
95 /*tp_getattr*/ nullptr,
96 /*tp_setattr*/ nullptr,
97 /*tp_as_async*/ nullptr,
98 /*tp_repr*/ nullptr,
99 /*tp_as_number*/ nullptr,
100 /*tp_as_sequence*/ nullptr,
101 /*tp_as_mapping*/ nullptr,
102 /*tp_hash*/ nullptr,
103 /*tp_call*/ nullptr,
104 /*tp_str*/ nullptr,
105 /*tp_getattro*/ nullptr,
106 /*tp_setattro*/ nullptr,
107 /*tp_as_buffer*/ nullptr,
108 /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
109 /*tp_doc*/ nullptr,
110 /*tp_traverse*/ reinterpret_cast<traverseproc>(bpy_rna_data_context_traverse),
111 /*tp_clear*/ reinterpret_cast<inquiry>(bpy_rna_data_context_clear),
112 /*tp_richcompare*/ nullptr,
113 /*tp_weaklistoffset*/ 0,
114 /*tp_iter*/ nullptr,
115 /*tp_iternext*/ nullptr,
116 /*tp_methods*/ bpy_rna_data_context_methods,
117 /*tp_members*/ nullptr,
118 /*tp_getset*/ nullptr,
119 /*tp_base*/ nullptr,
120 /*tp_dict*/ nullptr,
121 /*tp_descr_get*/ nullptr,
122 /*tp_descr_set*/ nullptr,
123 /*tp_dictoffset*/ 0,
124 /*tp_init*/ nullptr,
125 /*tp_alloc*/ nullptr,
126 /*tp_new*/ nullptr,
127 /*tp_free*/ nullptr,
128 /*tp_is_gc*/ nullptr,
129 /*tp_bases*/ nullptr,
130 /*tp_mro*/ nullptr,
131 /*tp_cache*/ nullptr,
132 /*tp_subclasses*/ nullptr,
133 /*tp_weaklist*/ nullptr,
134 /*tp_del*/ nullptr,
135 /*tp_version_tag*/ 0,
136 /*tp_finalize*/ nullptr,
137 /*tp_vectorcall*/ nullptr,
138};
139
141 /* Wrap. */
142 bpy_rna_data_context_load_doc,
143 ".. staticmethod:: temp_data(*, filepath=None)\n"
144 "\n"
145 " A context manager that temporarily creates blender file data.\n"
146 "\n"
147 " :param filepath: The file path for the newly temporary data. "
148 "When None, the path of the currently open file is used.\n"
149 " :type filepath: str | bytes | None\n"
150 "\n"
151 " :return: Blend file data which is freed once the context exits.\n"
152 " :rtype: :class:`bpy.types.BlendData`\n");
153static PyObject *bpy_rna_data_temp_data(PyObject * /*self*/, PyObject *args, PyObject *kw)
154{
155 PyC_UnicodeAsBytesAndSize_Data filepath_data = {nullptr};
157 static const char *_keywords[] = {"filepath", nullptr};
158 static _PyArg_Parser _parser = {
159 "|$" /* Optional, keyword only arguments. */
160 "O&" /* `filepath` */
161 ":temp_data",
162 _keywords,
163 nullptr,
164 };
165 if (!_PyArg_ParseTupleAndKeywordsFast(
166 args, kw, &_parser, PyC_ParseUnicodeAsBytesAndSize_OrNone, &filepath_data))
167 {
168 return nullptr;
169 }
170
171 ret = PyObject_GC_New(BPy_DataContext, &bpy_rna_data_context_Type);
172 ret->data_rna = nullptr;
173
174 STRNCPY(ret->filepath, filepath_data.value ? filepath_data.value : G_MAIN->filepath);
175 Py_XDECREF(filepath_data.value_coerce);
176
177 return reinterpret_cast<PyObject *>(ret);
178}
179
181{
182 if (self->data_rna != nullptr) {
183 PyErr_SetString(PyExc_RuntimeError, "Context manager already entered");
184 return nullptr;
185 }
186
187 Main *bmain_temp = BKE_main_new();
188 STRNCPY(bmain_temp->filepath, self->filepath);
189
190 PointerRNA ptr = RNA_pointer_create_discrete(nullptr, RNA_BlendData, bmain_temp);
191
192 self->data_rna = reinterpret_cast<BPy_StructRNA *>(pyrna_struct_CreatePyObject(&ptr));
193
194 BLI_assert(!PyObject_GC_IsTracked((PyObject *)self));
195 PyObject_GC_Track(self);
196
197 return Py_NewRef(reinterpret_cast<PyObject *>(self->data_rna));
198}
199
200static PyObject *bpy_rna_data_context_exit(BPy_DataContext *self, PyObject * /*args*/)
201{
202 /* Only possible when `__exit__` is called directly instead of via a `with` statement. */
203 if (self->data_rna == nullptr) {
204 PyErr_SetString(PyExc_RuntimeError, "Context manager not entered");
205 return nullptr;
206 }
207 if (self->data_rna->ptr->data == nullptr) {
208 PyErr_SetString(PyExc_RuntimeError, "Context manager already exited");
209 return nullptr;
210 }
211
212 BKE_main_free(static_cast<Main *>(self->data_rna->ptr->data));
213 self->data_rna->ptr->invalidate();
214 Py_RETURN_NONE;
215}
216
217#ifdef __GNUC__
218# ifdef __clang__
219# pragma clang diagnostic push
220# pragma clang diagnostic ignored "-Wcast-function-type"
221# else
222# pragma GCC diagnostic push
223# pragma GCC diagnostic ignored "-Wcast-function-type"
224# endif
225#endif
226
228 "temp_data",
229 reinterpret_cast<PyCFunction>(bpy_rna_data_temp_data),
230 METH_STATIC | METH_VARARGS | METH_KEYWORDS,
231 bpy_rna_data_context_load_doc,
232};
233
234#ifdef __GNUC__
235# ifdef __clang__
236# pragma clang diagnostic pop
237# else
238# pragma GCC diagnostic pop
239# endif
240#endif
241
243{
244 if (PyType_Ready(&bpy_rna_data_context_Type) < 0) {
245 return -1;
246 }
247
248 return 0;
249}
250
251} // namespace blender
#define G_MAIN
#define FILE_MAX
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static PyObject * bpy_rna_data_context_exit(BPy_DataContext *self, PyObject *args)
char * STRNCPY(char(&dst)[N], const char *src)
static int bpy_rna_data_context_clear(BPy_DataContext *self)
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
Main * BKE_main_new()
Definition main.cc:93
static PyTypeObject bpy_rna_data_context_Type
PyObject * pyrna_struct_CreatePyObject(PointerRNA *ptr)
Definition bpy_rna.cc:8874
static int bpy_rna_data_context_traverse(BPy_DataContext *self, visitproc visit, void *arg)
PyMethodDef BPY_rna_data_context_method_def
static PyObject * bpy_rna_data_temp_data(PyObject *self, PyObject *args, PyObject *kw)
static PyMethodDef bpy_rna_data_context_methods[]
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
static PyObject * bpy_rna_data_context_enter(BPy_DataContext *self)
void BKE_main_free(Main *bmain)
Definition main.cc:202
static void bpy_rna_data_context_dealloc(BPy_DataContext *self)
int PyC_ParseUnicodeAsBytesAndSize_OrNone(PyObject *o, void *p)
int BPY_rna_data_context_type_ready()
header-only compatibility defines.
PyObject_HEAD BPy_StructRNA * data_rna
PyObject_HEAD std::optional< PointerRNA > ptr
Definition bpy_rna.hh:133
char filepath[1024]
Definition BKE_main.hh:267