Blender  V3.3
bpy_library_load.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_ghash.h"
18 #include "BLI_linklist.h"
19 #include "BLI_path_util.h"
20 #include "BLI_string.h"
21 #include "BLI_utildefines.h"
22 
24 #include "BKE_context.h"
25 #include "BKE_idtype.h"
26 #include "BKE_lib_id.h"
27 #include "BKE_main.h"
28 #include "BKE_report.h"
29 
30 #include "DNA_space_types.h" /* FILE_LINK, FILE_RELPATH */
31 
32 #include "BLO_readfile.h"
33 
34 #include "MEM_guardedalloc.h"
35 
36 #include "bpy_capi_utils.h"
37 #include "bpy_library.h"
38 
39 #include "../generic/py_capi_utils.h"
40 #include "../generic/python_utildefines.h"
41 
42 /* nifty feature. swap out strings for RNA data */
43 #define USE_RNA_DATABLOCKS
44 
45 #ifdef USE_RNA_DATABLOCKS
46 # include "RNA_access.h"
47 # include "bpy_rna.h"
48 #endif
49 
50 typedef struct {
51  PyObject_HEAD /* Required Python macro. */
52  /* Collection iterator specific parts. */
53  char relpath[FILE_MAX];
54  char abspath[FILE_MAX]; /* absolute path */
56  /* Referenced by `blo_handle`, so stored here to keep alive for long enough. */
59 
60  int flag;
61  PyObject *dict;
62  /* Borrowed reference to the `bmain`, taken from the RNA instance of #RNA_BlendDataLibraries.
63  * Defaults to #G.main, Otherwise use a temporary #Main when `bmain_is_temp` is true. */
66 } BPy_Library;
67 
68 static PyObject *bpy_lib_load(BPy_PropertyRNA *self, PyObject *args, PyObject *kwds);
69 static PyObject *bpy_lib_enter(BPy_Library *self);
70 static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *args);
71 static PyObject *bpy_lib_dir(BPy_Library *self);
72 
73 static PyMethodDef bpy_lib_methods[] = {
74  {"__enter__", (PyCFunction)bpy_lib_enter, METH_NOARGS},
75  {"__exit__", (PyCFunction)bpy_lib_exit, METH_VARARGS},
76  {"__dir__", (PyCFunction)bpy_lib_dir, METH_NOARGS},
77  {NULL} /* sentinel */
78 };
79 
80 static void bpy_lib_dealloc(BPy_Library *self)
81 {
82  Py_XDECREF(self->dict);
83  Py_TYPE(self)->tp_free(self);
84 }
85 
86 static PyTypeObject bpy_lib_Type = {
87  PyVarObject_HEAD_INIT(NULL, 0) "bpy_lib", /* tp_name */
88  sizeof(BPy_Library), /* tp_basicsize */
89  0, /* tp_itemsize */
90  /* methods */
91  (destructor)bpy_lib_dealloc, /* tp_dealloc */
92  0, /* tp_vectorcall_offset */
93  NULL, /* getattrfunc tp_getattr; */
94  NULL, /* setattrfunc tp_setattr; */
95  NULL,
96  /* tp_compare */ /* DEPRECATED in python 3.0! */
97  NULL, /* tp_repr */
98 
99  /* Method suites for standard classes */
100 
101  NULL, /* PyNumberMethods *tp_as_number; */
102  NULL, /* PySequenceMethods *tp_as_sequence; */
103  NULL, /* PyMappingMethods *tp_as_mapping; */
104 
105  /* More standard operations (here for binary compatibility) */
106 
107  NULL, /* hashfunc tp_hash; */
108  NULL, /* ternaryfunc tp_call; */
109  NULL, /* reprfunc tp_str; */
110 
111  /* will only use these if this is a subtype of a py class */
112  PyObject_GenericGetAttr, /* getattrofunc tp_getattro; */
113  NULL, /* setattrofunc tp_setattro; */
114 
115  /* Functions to access object as input/output buffer */
116  NULL, /* PyBufferProcs *tp_as_buffer; */
117 
118  /*** Flags to define presence of optional/expanded features ***/
119  Py_TPFLAGS_DEFAULT, /* long tp_flags; */
120 
121  NULL, /* char *tp_doc; Documentation string */
122  /*** Assigned meaning in release 2.0 ***/
123  /* call function for all accessible objects */
124  NULL, /* traverseproc tp_traverse; */
125 
126  /* delete references to contained objects */
127  NULL, /* inquiry tp_clear; */
128 
129  /*** Assigned meaning in release 2.1 ***/
130  /*** rich comparisons (subclassed) ***/
131  NULL, /* richcmpfunc tp_richcompare; */
132 
133  /*** weak reference enabler ***/
134  0,
135  /*** Added in release 2.2 ***/
136  /* Iterators */
137  NULL, /* getiterfunc tp_iter; */
138  NULL, /* iternextfunc tp_iternext; */
139 
140  /*** Attribute descriptor and subclassing stuff ***/
141  bpy_lib_methods, /* struct PyMethodDef *tp_methods; */
142  NULL, /* struct PyMemberDef *tp_members; */
143  NULL, /* struct PyGetSetDef *tp_getset; */
144  NULL, /* struct _typeobject *tp_base; */
145  NULL, /* PyObject *tp_dict; */
146  NULL, /* descrgetfunc tp_descr_get; */
147  NULL, /* descrsetfunc tp_descr_set; */
148  offsetof(BPy_Library, dict), /* long tp_dictoffset; */
149  NULL, /* initproc tp_init; */
150  NULL, /* allocfunc tp_alloc; */
151  NULL, /* newfunc tp_new; */
152  /* Low-level free-memory routine */
153  NULL, /* freefunc tp_free; */
154  /* For PyObject_IS_GC */
155  NULL, /* inquiry tp_is_gc; */
156  NULL, /* PyObject *tp_bases; */
157  /* method resolution order */
158  NULL, /* PyObject *tp_mro; */
159  NULL, /* PyObject *tp_cache; */
160  NULL, /* PyObject *tp_subclasses; */
161  NULL, /* PyObject *tp_weaklist; */
162  NULL,
163 };
164 
166  bpy_lib_load_doc,
167  ".. method:: load(filepath, link=False, relative=False, assets_only=False)\n"
168  "\n"
169  " Returns a context manager which exposes 2 library objects on entering.\n"
170  " Each object has attributes matching bpy.data which are lists of strings to be linked.\n"
171  "\n"
172  " :arg filepath: The path to a blend file.\n"
173  " :type filepath: string\n"
174  " :arg link: When False reference to the original file is lost.\n"
175  " :type link: bool\n"
176  " :arg relative: When True the path is stored relative to the open blend file.\n"
177  " :type relative: bool\n"
178  " :arg assets_only: If True, only list data-blocks marked as assets.\n"
179  " :type assets_only: bool\n");
180 static PyObject *bpy_lib_load(BPy_PropertyRNA *self, PyObject *args, PyObject *kw)
181 {
182  Main *bmain_base = CTX_data_main(BPY_context_get());
183  Main *bmain = self->ptr.data; /* Typically #G_MAIN */
184  BPy_Library *ret;
185  const char *filepath = NULL;
186  bool is_rel = false, is_link = false, use_assets_only = false;
187 
188  static const char *_keywords[] = {"filepath", "link", "relative", "assets_only", NULL};
189  static _PyArg_Parser _parser = {
190  "s" /* `filepath` */
191  /* Optional keyword only arguments. */
192  "|$"
193  "O&" /* `link` */
194  "O&" /* `relative` */
195  "O&" /* `assets_only` */
196  ":load",
197  _keywords,
198  0,
199  };
200  if (!_PyArg_ParseTupleAndKeywordsFast(args,
201  kw,
202  &_parser,
203  &filepath,
205  &is_link,
207  &is_rel,
209  &use_assets_only)) {
210  return NULL;
211  }
212 
213  ret = PyObject_New(BPy_Library, &bpy_lib_Type);
214 
215  BLI_strncpy(ret->relpath, filepath, sizeof(ret->relpath));
216  BLI_strncpy(ret->abspath, filepath, sizeof(ret->abspath));
217  BLI_path_abs(ret->abspath, BKE_main_blendfile_path(bmain));
218 
219  ret->bmain = bmain;
220  ret->bmain_is_temp = (bmain != bmain_base);
221 
222  ret->blo_handle = NULL;
223  ret->flag = ((is_link ? FILE_LINK : 0) | (is_rel ? FILE_RELPATH : 0) |
224  (use_assets_only ? FILE_ASSETS_ONLY : 0));
225 
226  ret->dict = _PyDict_NewPresized(INDEX_ID_MAX);
227 
228  return (PyObject *)ret;
229 }
230 
231 static PyObject *_bpy_names(BPy_Library *self, int blocktype)
232 {
233  PyObject *list;
234  LinkNode *l, *names;
235  int totnames;
236 
238  self->blo_handle, blocktype, (self->flag & FILE_ASSETS_ONLY) != 0, &totnames);
239  list = PyList_New(totnames);
240 
241  if (names) {
242  int counter = 0;
243  for (l = names; l; l = l->next) {
244  PyList_SET_ITEM(list, counter, PyUnicode_FromString((char *)l->link));
245  counter++;
246  }
247  BLI_linklist_freeN(names); /* free linklist *and* each node's data */
248  }
249 
250  return list;
251 }
252 
253 static PyObject *bpy_lib_enter(BPy_Library *self)
254 {
255  PyObject *ret;
256  BPy_Library *self_from;
257  PyObject *from_dict = _PyDict_NewPresized(INDEX_ID_MAX);
258  ReportList *reports = &self->reports;
259  BlendFileReadReport *bf_reports = &self->bf_reports;
260 
261  BKE_reports_init(reports, RPT_STORE);
262  memset(bf_reports, 0, sizeof(*bf_reports));
263  bf_reports->reports = reports;
264 
265  self->blo_handle = BLO_blendhandle_from_file(self->abspath, bf_reports);
266 
267  if (self->blo_handle == NULL) {
268  if (BPy_reports_to_error(reports, PyExc_IOError, true) != -1) {
269  PyErr_Format(PyExc_IOError, "load: %s failed to open blend file", self->abspath);
270  }
271  return NULL;
272  }
273 
274  int i = 0, code;
275  while ((code = BKE_idtype_idcode_iter_step(&i))) {
276  if (BKE_idtype_idcode_is_linkable(code)) {
277  const char *name_plural = BKE_idtype_idcode_to_name_plural(code);
278  PyObject *str = PyUnicode_FromString(name_plural);
279  PyObject *item;
280 
281  PyDict_SetItem(self->dict, str, item = PyList_New(0));
282  Py_DECREF(item);
283  PyDict_SetItem(from_dict, str, item = _bpy_names(self, code));
284  Py_DECREF(item);
285 
286  Py_DECREF(str);
287  }
288  }
289 
290  /* create a dummy */
291  self_from = PyObject_New(BPy_Library, &bpy_lib_Type);
292  BLI_strncpy(self_from->relpath, self->relpath, sizeof(self_from->relpath));
293  BLI_strncpy(self_from->abspath, self->abspath, sizeof(self_from->abspath));
294 
295  self_from->blo_handle = NULL;
296  self_from->flag = 0;
297  self_from->dict = from_dict; /* owns the dict */
298 
299  /* return pair */
300  ret = PyTuple_New(2);
301  PyTuple_SET_ITEMS(ret, (PyObject *)self_from, (PyObject *)self);
302  Py_INCREF(self);
303 
304  BKE_reports_clear(reports);
305 
306  return ret;
307 }
308 
310  const char *name_plural,
311  const char *idname)
312 {
313  PyObject *exc, *val, *tb;
314  PyErr_Fetch(&exc, &val, &tb);
315  if (PyErr_WarnFormat(PyExc_UserWarning,
316  1,
317  "load: '%s' does not contain %s[\"%s\"]",
318  self->abspath,
319  name_plural,
320  idname)) {
321  /* Spurious errors can appear at shutdown */
322  if (PyErr_ExceptionMatches(PyExc_Warning)) {
323  PyErr_WriteUnraisable((PyObject *)self);
324  }
325  }
326  PyErr_Restore(exc, val, tb);
327 }
328 
329 static void bpy_lib_exit_warn_type(BPy_Library *self, PyObject *item)
330 {
331  PyObject *exc, *val, *tb;
332  PyErr_Fetch(&exc, &val, &tb);
333  if (PyErr_WarnFormat(PyExc_UserWarning,
334  1,
335  "load: '%s' expected a string type, not a %.200s",
336  self->abspath,
337  Py_TYPE(item)->tp_name)) {
338  /* Spurious errors can appear at shutdown */
339  if (PyErr_ExceptionMatches(PyExc_Warning)) {
340  PyErr_WriteUnraisable((PyObject *)self);
341  }
342  }
343  PyErr_Restore(exc, val, tb);
344 }
345 
347  short idcode;
349  PyObject *py_list;
350  Py_ssize_t py_list_size;
351 };
352 
355  void *userdata)
356 {
357  struct LibExitLappContextItemsIterData *data = userdata;
358 
359  /* Since `bpy_lib_exit` loops over all ID types, all items in `lapp_context` end up being looped
360  * over for each ID type, so when it does not match the item can simply be skipped: it either has
361  * already been processed, or will be processed in a later loop. */
362  if (BKE_blendfile_link_append_context_item_idcode_get(lapp_context, item) != data->idcode) {
363  return true;
364  }
365 
366  const int py_list_index = POINTER_AS_INT(
368  ID *new_id = BKE_blendfile_link_append_context_item_newid_get(lapp_context, item);
369 
370  BLI_assert(py_list_index < data->py_list_size);
371 
372  /* Fully invalid items (which got set to `Py_None` already in first loop of `bpy_lib_exit`)
373  * should never be accessed here, since their index should never be set to any item in
374  * `lapp_context`. */
375  PyObject *item_src = PyList_GET_ITEM(data->py_list, py_list_index);
376  BLI_assert(item_src != Py_None);
377 
378  PyObject *py_item;
379  if (new_id != NULL) {
380  PointerRNA newid_ptr;
381  RNA_id_pointer_create(new_id, &newid_ptr);
382  py_item = pyrna_struct_CreatePyObject(&newid_ptr);
383  }
384  else {
385  const char *item_idname = PyUnicode_AsUTF8(item_src);
386  const char *idcode_name_plural = BKE_idtype_idcode_to_name_plural(data->idcode);
387 
388  bpy_lib_exit_warn_idname(data->py_library, idcode_name_plural, item_idname);
389 
390  py_item = Py_INCREF_RET(Py_None);
391  }
392 
393  PyList_SET_ITEM(data->py_list, py_list_index, py_item);
394 
395  Py_DECREF(item_src);
396 
397  return true;
398 }
399 
400 static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *UNUSED(args))
401 {
402  Main *bmain = self->bmain;
403  const bool do_append = ((self->flag & FILE_LINK) == 0);
404 
406 
407  /* here appending/linking starts */
408  const int id_tag_extra = self->bmain_is_temp ? LIB_TAG_TEMP_MAIN : 0;
409  struct LibraryLink_Params liblink_params;
410  BLO_library_link_params_init(&liblink_params, bmain, self->flag, id_tag_extra);
411 
413  &liblink_params);
414  BKE_blendfile_link_append_context_library_add(lapp_context, self->abspath, self->blo_handle);
415 
416  int idcode_step = 0;
417  short idcode;
418  while ((idcode = BKE_idtype_idcode_iter_step(&idcode_step))) {
419  if (!BKE_idtype_idcode_is_linkable(idcode) || (idcode == ID_WS && !do_append)) {
420  continue;
421  }
422 
423  const char *name_plural = BKE_idtype_idcode_to_name_plural(idcode);
424  PyObject *ls = PyDict_GetItemString(self->dict, name_plural);
425  // printf("lib: %s\n", name_plural);
426  if (ls == NULL || !PyList_Check(ls)) {
427  continue;
428  }
429 
430  const Py_ssize_t size = PyList_GET_SIZE(ls);
431  if (size == 0) {
432  continue;
433  }
434 
435  /* loop */
436  for (Py_ssize_t i = 0; i < size; i++) {
437  PyObject *item_src = PyList_GET_ITEM(ls, i);
438  const char *item_idname = PyUnicode_AsUTF8(item_src);
439 
440  // printf(" %s\n", item_idname);
441 
442  /* NOTE: index of item in py list is stored in userdata pointer, so that it can be found
443  * later on to replace the ID name by the actual ID pointer. */
444  if (item_idname != NULL) {
446  lapp_context, item_idname, idcode, POINTER_FROM_INT(i));
448  }
449  else {
450  /* XXX, could complain about this */
451  bpy_lib_exit_warn_type(self, item_src);
452  PyErr_Clear();
453 
454 #ifdef USE_RNA_DATABLOCKS
455  /* We can replace the item immediately with `None`. */
456  PyObject *py_item = Py_INCREF_RET(Py_None);
457  PyList_SET_ITEM(ls, i, py_item);
458  Py_DECREF(item_src);
459 #endif
460  }
461  }
462  }
463 
464  BKE_blendfile_link(lapp_context, NULL);
465  if (do_append) {
466  BKE_blendfile_append(lapp_context, NULL);
467  }
468 
469  /* If enabled, replace named items in given lists by the final matching new ID pointer. */
470 #ifdef USE_RNA_DATABLOCKS
471  idcode_step = 0;
472  while ((idcode = BKE_idtype_idcode_iter_step(&idcode_step))) {
473  if (!BKE_idtype_idcode_is_linkable(idcode) || (idcode == ID_WS && !do_append)) {
474  continue;
475  }
476  const char *name_plural = BKE_idtype_idcode_to_name_plural(idcode);
477  PyObject *ls = PyDict_GetItemString(self->dict, name_plural);
478  // printf("lib: %s\n", name_plural);
479  if (ls == NULL || !PyList_Check(ls)) {
480  continue;
481  }
482 
483  const Py_ssize_t size = PyList_GET_SIZE(ls);
484  if (size == 0) {
485  continue;
486  }
487 
488  /* Loop over linked items in `lapp_context` to find matching python one in the list, and
489  * replace them with proper ID pointer. */
490  struct LibExitLappContextItemsIterData iter_data = {
491  .idcode = idcode, .py_library = self, .py_list = ls, .py_list_size = size};
493  lapp_context,
496  &iter_data);
497  }
498 #endif // USE_RNA_DATABLOCKS
499 
500  BLO_blendhandle_close(self->blo_handle);
501  self->blo_handle = NULL;
502 
505 
506  Py_RETURN_NONE;
507 }
508 
509 static PyObject *bpy_lib_dir(BPy_Library *self)
510 {
511  return PyDict_Keys(self->dict);
512 }
513 
515  "load",
516  (PyCFunction)bpy_lib_load,
517  METH_VARARGS | METH_KEYWORDS,
518  bpy_lib_load_doc,
519 };
520 
522 {
523  if (PyType_Ready(&bpy_lib_Type) < 0) {
524  return -1;
525  }
526 
527  return 0;
528 }
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1074
const char * BKE_idtype_idcode_to_name_plural(short idcode)
Definition: idtype.c:149
short BKE_idtype_idcode_iter_step(int *index)
Definition: idtype.c:442
bool BKE_idtype_idcode_is_linkable(short idcode)
Definition: idtype.c:175
void BKE_main_id_tag_all(struct Main *mainvar, int tag, bool value)
Definition: lib_id.c:930
const char * BKE_main_blendfile_path(const struct Main *bmain) ATTR_NONNULL()
void BKE_reports_clear(ReportList *reports)
Definition: report.c:63
void BKE_reports_init(ReportList *reports, int flag)
Definition: report.c:50
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define FILE_MAX
bool BLI_path_abs(char *path, const char *basepath) ATTR_NONNULL()
Definition: path_util.c:897
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
#define POINTER_FROM_INT(i)
#define UNUSED(x)
#define POINTER_AS_INT(i)
external readfile function prototypes.
BlendHandle * BLO_blendhandle_from_file(const char *filepath, struct BlendFileReadReport *reports)
Definition: readblenentry.c:48
struct LinkNode * BLO_blendhandle_get_datablock_names(BlendHandle *bh, int ofblocktype, bool use_assets_only, int *r_tot_names)
struct BlendHandle BlendHandle
Definition: BLO_readfile.h:35
void BLO_library_link_params_init(struct LibraryLink_Params *params, struct Main *bmain, int flag, int id_tag_extra)
Definition: readfile.c:4622
void BLO_blendhandle_close(BlendHandle *bh)
@ INDEX_ID_MAX
Definition: DNA_ID.h:1058
@ LIB_TAG_TEMP_MAIN
Definition: DNA_ID.h:757
@ LIB_TAG_PRE_EXISTING
Definition: DNA_ID.h:709
@ ID_WS
Definition: DNA_ID_enums.h:79
@ FILE_RELPATH
@ FILE_LINK
@ FILE_ASSETS_ONLY
Read Guarded memory(de)allocation.
ATTR_WARN_UNUSED_RESULT const BMLoop * l
short BPy_reports_to_error(ReportList *reports, PyObject *exception, const bool clear)
struct bContext * BPY_context_get(void)
PyObject * self
Definition: bpy_driver.c:165
PyMethodDef BPY_library_load_method_def
static void bpy_lib_dealloc(BPy_Library *self)
static PyMethodDef bpy_lib_methods[]
PyDoc_STRVAR(bpy_lib_load_doc, ".. method:: load(filepath, link=False, relative=False, assets_only=False)\n" "\n" " Returns a context manager which exposes 2 library objects on entering.\n" " Each object has attributes matching bpy.data which are lists of strings to be linked.\n" "\n" " :arg filepath: The path to a blend file.\n" " :type filepath: string\n" " :arg link: When False reference to the original file is lost.\n" " :type link: bool\n" " :arg relative: When True the path is stored relative to the open blend file.\n" " :type relative: bool\n" " :arg assets_only: If True, only list data-blocks marked as assets.\n" " :type assets_only: bool\n")
static PyObject * bpy_lib_enter(BPy_Library *self)
static PyObject * bpy_lib_exit(BPy_Library *self, PyObject *args)
static PyObject * bpy_lib_dir(BPy_Library *self)
int BPY_library_load_type_ready(void)
static PyObject * _bpy_names(BPy_Library *self, int blocktype)
static void bpy_lib_exit_warn_type(BPy_Library *self, PyObject *item)
static PyTypeObject bpy_lib_Type
static bool bpy_lib_exit_lapp_context_items_cb(BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item, void *userdata)
static PyObject * bpy_lib_load(BPy_PropertyRNA *self, PyObject *args, PyObject *kwds)
static void bpy_lib_exit_warn_idname(BPy_Library *self, const char *name_plural, const char *idname)
PyObject * pyrna_struct_CreatePyObject(PointerRNA *ptr)
Definition: bpy_rna.c:7505
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
#define str(s)
ccl_gpu_kernel_postfix ccl_global int * counter
static char ** names
Definition: makesdna.c:65
int PyC_ParseBool(PyObject *o, void *p)
#define PyTuple_SET_ITEMS(op_arg,...)
return ret
void RNA_id_pointer_create(ID *id, PointerRNA *r_ptr)
Definition: rna_access.c:112
struct BMLoop * next
Definition: bmesh_class.h:233
BlendHandle * blo_handle
char abspath[FILE_MAX]
BlendFileReadReport bf_reports
PyObject * dict
ReportList reports
PyObject_HEAD char relpath[FILE_MAX]
struct ReportList * reports
Definition: BLO_readfile.h:80
Definition: DNA_ID.h:368
Definition: BKE_main.h:121