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