Blender  V3.3
bpy_rna_data.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
14 #include <Python.h>
15 #include <stddef.h>
16 
17 #include "BLI_string.h"
18 #include "BLI_utildefines.h"
19 
20 #include "BKE_global.h"
21 #include "BKE_main.h"
22 
23 #include "RNA_access.h"
24 #include "RNA_prototypes.h"
25 
26 #include "bpy_rna.h"
27 #include "bpy_rna_data.h"
28 
29 typedef struct {
30  PyObject_HEAD /* Required Python macro. */
32  char filepath[1024];
34 
35 static PyObject *bpy_rna_data_temp_data(PyObject *self, PyObject *args, PyObject *kwds);
36 static PyObject *bpy_rna_data_context_enter(BPy_DataContext *self);
37 static PyObject *bpy_rna_data_context_exit(BPy_DataContext *self, PyObject *args);
38 
39 static PyMethodDef bpy_rna_data_context_methods[] = {
40  {"__enter__", (PyCFunction)bpy_rna_data_context_enter, METH_NOARGS},
41  {"__exit__", (PyCFunction)bpy_rna_data_context_exit, METH_VARARGS},
42  {NULL} /* sentinel */
43 };
44 
45 static int bpy_rna_data_context_traverse(BPy_DataContext *self, visitproc visit, void *arg)
46 {
47  Py_VISIT(self->data_rna);
48  return 0;
49 }
50 
52 {
53  Py_CLEAR(self->data_rna);
54  return 0;
55 }
56 
58 {
59  PyObject_GC_UnTrack(self);
60  Py_CLEAR(self->data_rna);
61  PyObject_GC_Del(self);
62 }
63 
64 static PyTypeObject bpy_rna_data_context_Type = {
65  PyVarObject_HEAD_INIT(NULL, 0) "bpy_rna_data_context", /* tp_name */
66  sizeof(BPy_DataContext), /* tp_basicsize */
67  0, /* tp_itemsize */
68  /* methods */
69  (destructor)bpy_rna_data_context_dealloc, /* tp_dealloc */
70  0, /* tp_vectorcall_offset */
71  NULL, /* getattrfunc tp_getattr; */
72  NULL, /* setattrfunc tp_setattr; */
73  NULL,
74  /* tp_compare */ /* DEPRECATED in python 3.0! */
75  NULL, /* tp_repr */
76 
77  /* Method suites for standard classes */
78 
79  NULL, /* PyNumberMethods *tp_as_number; */
80  NULL, /* PySequenceMethods *tp_as_sequence; */
81  NULL, /* PyMappingMethods *tp_as_mapping; */
82 
83  /* More standard operations (here for binary compatibility) */
84 
85  NULL, /* hashfunc tp_hash; */
86  NULL, /* ternaryfunc tp_call; */
87  NULL, /* reprfunc tp_str; */
88 
89  /* will only use these if this is a subtype of a py class */
90  NULL, /* getattrofunc tp_getattro; */
91  NULL, /* setattrofunc tp_setattro; */
92 
93  /* Functions to access object as input/output buffer */
94  NULL, /* PyBufferProcs *tp_as_buffer; */
95 
96  /*** Flags to define presence of optional/expanded features ***/
97  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* long tp_flags; */
98 
99  NULL, /* char *tp_doc; Documentation string */
100  /*** Assigned meaning in release 2.0 ***/
101  /* call function for all accessible objects */
102  (traverseproc)bpy_rna_data_context_traverse, /* traverseproc tp_traverse; */
103 
104  /* delete references to contained objects */
105  (inquiry)bpy_rna_data_context_clear, /* inquiry tp_clear; */
106 
107  /*** Assigned meaning in release 2.1 ***/
108  /*** rich comparisons (subclassed) ***/
109  NULL, /* richcmpfunc tp_richcompare; */
110 
111  /*** weak reference enabler ***/
112  0,
113  /*** Added in release 2.2 ***/
114  /* Iterators */
115  NULL, /* getiterfunc tp_iter; */
116  NULL, /* iternextfunc tp_iternext; */
117 
118  /*** Attribute descriptor and subclassing stuff ***/
119  bpy_rna_data_context_methods, /* struct PyMethodDef *tp_methods; */
120  NULL, /* struct PyMemberDef *tp_members; */
121  NULL, /* struct PyGetSetDef *tp_getset; */
122  NULL, /* struct _typeobject *tp_base; */
123  NULL, /* PyObject *tp_dict; */
124  NULL, /* descrgetfunc tp_descr_get; */
125  NULL, /* descrsetfunc tp_descr_set; */
126  0, /* long tp_dictoffset; */
127  NULL, /* initproc tp_init; */
128  NULL, /* allocfunc tp_alloc; */
129  NULL, /* newfunc tp_new; */
130  /* Low-level free-memory routine */
131  NULL, /* freefunc tp_free; */
132  /* For PyObject_IS_GC */
133  NULL, /* inquiry tp_is_gc; */
134  NULL, /* PyObject *tp_bases; */
135  /* method resolution order */
136  NULL, /* PyObject *tp_mro; */
137  NULL, /* PyObject *tp_cache; */
138  NULL, /* PyObject *tp_subclasses; */
139  NULL, /* PyObject *tp_weaklist; */
140  NULL,
141 };
142 
143 PyDoc_STRVAR(bpy_rna_data_context_load_doc,
144  ".. method:: temp_data(filepath=None)\n"
145  "\n"
146  " A context manager that temporarily creates blender file data.\n"
147  "\n"
148  " :arg filepath: The file path for the newly temporary data. "
149  "When None, the path of the currently open file is used.\n"
150  " :type filepath: str or NoneType\n"
151  "\n"
152  " :return: Blend file data which is freed once the context exists.\n"
153  " :rtype: :class:`bpy.types.BlendData`\n");
154 
155 static PyObject *bpy_rna_data_temp_data(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
156 {
158  const char *filepath = NULL;
159  static const char *_keywords[] = {"filepath", NULL};
160  static _PyArg_Parser _parser = {
161  "|$" /* Optional keyword only arguments. */
162  "z" /* `filepath` */
163  ":temp_data",
164  _keywords,
165  0,
166  };
167  if (!_PyArg_ParseTupleAndKeywordsFast(args, kw, &_parser, &filepath)) {
168  return NULL;
169  }
170 
171  ret = PyObject_GC_New(BPy_DataContext, &bpy_rna_data_context_Type);
172 
173  STRNCPY(ret->filepath, filepath ? filepath : G_MAIN->filepath);
174 
175  return (PyObject *)ret;
176 }
177 
179 {
180  Main *bmain_temp = BKE_main_new();
181  PointerRNA ptr;
182  RNA_pointer_create(NULL, &RNA_BlendData, bmain_temp, &ptr);
183 
184  self->data_rna = (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ptr);
185 
186  PyObject_GC_Track(self);
187 
188  return (PyObject *)self->data_rna;
189 }
190 
191 static PyObject *bpy_rna_data_context_exit(BPy_DataContext *self, PyObject *UNUSED(args))
192 {
193  BKE_main_free(self->data_rna->ptr.data);
194  RNA_POINTER_INVALIDATE(&self->data_rna->ptr);
195  Py_RETURN_NONE;
196 }
197 
199  "temp_data",
200  (PyCFunction)bpy_rna_data_temp_data,
201  METH_STATIC | METH_VARARGS | METH_KEYWORDS,
202  bpy_rna_data_context_load_doc,
203 };
204 
206 {
207  if (PyType_Ready(&bpy_rna_data_context_Type) < 0) {
208  return -1;
209  }
210 
211  return 0;
212 }
#define G_MAIN
Definition: BKE_global.h:267
struct Main * BKE_main_new(void)
Definition: main.c:32
void BKE_main_free(struct Main *mainvar)
Definition: main.c:40
#define STRNCPY(dst, src)
Definition: BLI_string.h:483
#define UNUSED(x)
#define RNA_POINTER_INVALIDATE(ptr)
Definition: RNA_access.h:744
PyObject * self
Definition: bpy_driver.c:165
PyObject * pyrna_struct_CreatePyObject(PointerRNA *ptr)
Definition: bpy_rna.c:7505
static PyObject * bpy_rna_data_context_exit(BPy_DataContext *self, PyObject *args)
static PyObject * bpy_rna_data_context_enter(BPy_DataContext *self)
Definition: bpy_rna_data.c:178
static int bpy_rna_data_context_clear(BPy_DataContext *self)
Definition: bpy_rna_data.c:51
static PyTypeObject bpy_rna_data_context_Type
Definition: bpy_rna_data.c:64
static int bpy_rna_data_context_traverse(BPy_DataContext *self, visitproc visit, void *arg)
Definition: bpy_rna_data.c:45
PyMethodDef BPY_rna_data_context_method_def
Definition: bpy_rna_data.c:198
static void bpy_rna_data_context_dealloc(BPy_DataContext *self)
Definition: bpy_rna_data.c:57
static PyMethodDef bpy_rna_data_context_methods[]
Definition: bpy_rna_data.c:39
PyDoc_STRVAR(bpy_rna_data_context_load_doc, ".. method:: temp_data(filepath=None)\n" "\n" " A context manager that temporarily creates blender file data.\n" "\n" " :arg filepath: The file path for the newly temporary data. " "When None, the path of the currently open file is used.\n" " :type filepath: str or NoneType\n" "\n" " :return: Blend file data which is freed once the context exists.\n" " :rtype: :class:`bpy.types.BlendData`\n")
static PyObject * bpy_rna_data_temp_data(PyObject *self, PyObject *args, PyObject *kwds)
int BPY_rna_data_context_type_ready(void)
Definition: bpy_rna_data.c:205
return ret
void RNA_pointer_create(ID *id, StructRNA *type, void *data, PointerRNA *r_ptr)
Definition: rna_access.c:136
PyObject_HEAD BPy_StructRNA * data_rna
Definition: bpy_rna_data.c:31
Definition: BKE_main.h:121
PointerRNA * ptr
Definition: wm_files.c:3480