f9cd3338cded51295795cabe7ab16713a6edd971
[blender.git] / source / blender / python / intern / bpy_library_load.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file blender/python/intern/bpy_library_load.c
22  *  \ingroup pythonintern
23  *
24  * This file exposed blend file library appending/linking to python, typically
25  * this would be done via RNA api but in this case a hand written python api
26  * allows us to use pythons context manager (__enter__ and __exit__).
27  *
28  * Everything here is exposed via bpy.data.libraries.load(...) which returns
29  * a context manager.
30  */
31
32 #include <Python.h>
33 #include <stddef.h>
34
35 #include "BLI_utildefines.h"
36 #include "BLI_ghash.h"
37 #include "BLI_string.h"
38 #include "BLI_linklist.h"
39 #include "BLI_path_util.h"
40
41 #include "BKE_context.h"
42 #include "BKE_idcode.h"
43 #include "BKE_library.h"
44 #include "BKE_main.h"
45 #include "BKE_report.h"
46
47 #include "DNA_space_types.h" /* FILE_LINK, FILE_RELPATH */
48
49 #include "BLO_readfile.h"
50
51 #include "bpy_capi_utils.h"
52 #include "bpy_library.h"
53
54 #include "../generic/py_capi_utils.h"
55 #include "../generic/python_utildefines.h"
56
57 /* nifty feature. swap out strings for RNA data */
58 #define USE_RNA_DATABLOCKS
59
60 #ifdef USE_RNA_DATABLOCKS
61 #  include "bpy_rna.h"
62 #  include "RNA_access.h"
63 #endif
64
65 typedef struct {
66         PyObject_HEAD /* required python macro */
67         /* collection iterator specific parts */
68         char relpath[FILE_MAX];
69         char abspath[FILE_MAX]; /* absolute path */
70         BlendHandle *blo_handle;
71         int flag;
72         PyObject *dict;
73 } BPy_Library;
74
75 static PyObject *bpy_lib_load(PyObject *self, PyObject *args, PyObject *kwds);
76 static PyObject *bpy_lib_enter(BPy_Library *self);
77 static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *args);
78 static PyObject *bpy_lib_dir(BPy_Library *self);
79
80 static PyMethodDef bpy_lib_methods[] = {
81         {"__enter__", (PyCFunction)bpy_lib_enter, METH_NOARGS},
82         {"__exit__",  (PyCFunction)bpy_lib_exit,  METH_VARARGS},
83         {"__dir__",   (PyCFunction)bpy_lib_dir,   METH_NOARGS},
84         {NULL}           /* sentinel */
85 };
86
87 static void bpy_lib_dealloc(BPy_Library *self)
88 {
89         Py_XDECREF(self->dict);
90         Py_TYPE(self)->tp_free(self);
91 }
92
93
94 static PyTypeObject bpy_lib_Type = {
95         PyVarObject_HEAD_INIT(NULL, 0)
96         "bpy_lib",      /* tp_name */
97         sizeof(BPy_Library),            /* tp_basicsize */
98         0,                          /* tp_itemsize */
99         /* methods */
100         (destructor)bpy_lib_dealloc, /* tp_dealloc */
101         NULL,                       /* printfunc tp_print; */
102         NULL,                       /* getattrfunc tp_getattr; */
103         NULL,                       /* setattrfunc tp_setattr; */
104         NULL,                       /* tp_compare */ /* DEPRECATED in python 3.0! */
105         NULL,                       /* tp_repr */
106
107         /* Method suites for standard classes */
108
109         NULL,                       /* PyNumberMethods *tp_as_number; */
110         NULL,                       /* PySequenceMethods *tp_as_sequence; */
111         NULL,                       /* PyMappingMethods *tp_as_mapping; */
112
113         /* More standard operations (here for binary compatibility) */
114
115         NULL,                       /* hashfunc tp_hash; */
116         NULL,                       /* ternaryfunc tp_call; */
117         NULL,                       /* reprfunc tp_str; */
118
119         /* will only use these if this is a subtype of a py class */
120         NULL /*PyObject_GenericGetAttr is assigned later */,    /* getattrofunc tp_getattro; */
121         NULL,                       /* setattrofunc tp_setattro; */
122
123         /* Functions to access object as input/output buffer */
124         NULL,                       /* PyBufferProcs *tp_as_buffer; */
125
126         /*** Flags to define presence of optional/expanded features ***/
127         Py_TPFLAGS_DEFAULT,         /* long tp_flags; */
128
129         NULL,                       /*  char *tp_doc;  Documentation string */
130         /*** Assigned meaning in release 2.0 ***/
131         /* call function for all accessible objects */
132         NULL,                       /* traverseproc tp_traverse; */
133
134         /* delete references to contained objects */
135         NULL,                       /* inquiry tp_clear; */
136
137         /***  Assigned meaning in release 2.1 ***/
138         /*** rich comparisons ***/
139         NULL, /* subclassed */          /* richcmpfunc tp_richcompare; */
140
141         /***  weak reference enabler ***/
142         0,
143         /*** Added in release 2.2 ***/
144         /*   Iterators */
145         NULL,                       /* getiterfunc tp_iter; */
146         NULL,                       /* iternextfunc tp_iternext; */
147
148         /*** Attribute descriptor and subclassing stuff ***/
149         bpy_lib_methods,            /* struct PyMethodDef *tp_methods; */
150         NULL,                       /* struct PyMemberDef *tp_members; */
151         NULL,                       /* struct PyGetSetDef *tp_getset; */
152         NULL,                       /* struct _typeobject *tp_base; */
153         NULL,                       /* PyObject *tp_dict; */
154         NULL,                       /* descrgetfunc tp_descr_get; */
155         NULL,                       /* descrsetfunc tp_descr_set; */
156         offsetof(BPy_Library, dict), /* long tp_dictoffset; */
157         NULL,                       /* initproc tp_init; */
158         NULL,                       /* allocfunc tp_alloc; */
159         NULL,                       /* newfunc tp_new; */
160         /*  Low-level free-memory routine */
161         NULL,                       /* freefunc tp_free;  */
162         /* For PyObject_IS_GC */
163         NULL,                       /* inquiry tp_is_gc;  */
164         NULL,                       /* PyObject *tp_bases; */
165         /* method resolution order */
166         NULL,                       /* PyObject *tp_mro;  */
167         NULL,                       /* PyObject *tp_cache; */
168         NULL,                       /* PyObject *tp_subclasses; */
169         NULL,                       /* PyObject *tp_weaklist; */
170         NULL
171 };
172
173 PyDoc_STRVAR(bpy_lib_load_doc,
174 ".. method:: load(filepath, link=False, relative=False)\n"
175 "\n"
176 "   Returns a context manager which exposes 2 library objects on entering.\n"
177 "   Each object has attributes matching bpy.data which are lists of strings to be linked.\n"
178 "\n"
179 "   :arg filepath: The path to a blend file.\n"
180 "   :type filepath: string\n"
181 "   :arg link: When False reference to the original file is lost.\n"
182 "   :type link: bool\n"
183 "   :arg relative: When True the path is stored relative to the open blend file.\n"
184 "   :type relative: bool\n"
185 );
186 static PyObject *bpy_lib_load(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
187 {
188         Main *bmain = CTX_data_main(BPy_GetContext());
189         BPy_Library *ret;
190         const char *filename = NULL;
191         bool is_rel = false, is_link = false;
192
193         static const char *_keywords[] = {"filepath", "link", "relative", NULL};
194         static _PyArg_Parser _parser = {"s|O&O&:load", _keywords, 0};
195         if (!_PyArg_ParseTupleAndKeywordsFast(
196                 args, kw, &_parser,
197                 &filename,
198                 PyC_ParseBool, &is_link,
199                 PyC_ParseBool, &is_rel))
200         {
201                 return NULL;
202         }
203
204         ret = PyObject_New(BPy_Library, &bpy_lib_Type);
205
206         BLI_strncpy(ret->relpath, filename, sizeof(ret->relpath));
207         BLI_strncpy(ret->abspath, filename, sizeof(ret->abspath));
208         BLI_path_abs(ret->abspath, BKE_main_blendfile_path(bmain));
209
210         ret->blo_handle = NULL;
211         ret->flag = ((is_link ? FILE_LINK : 0) |
212                      (is_rel ? FILE_RELPATH : 0));
213
214         ret->dict = _PyDict_NewPresized(MAX_LIBARRAY);
215
216         return (PyObject *)ret;
217 }
218
219 static PyObject *_bpy_names(BPy_Library *self, int blocktype)
220 {
221         PyObject *list;
222         LinkNode *l, *names;
223         int totnames;
224
225         names = BLO_blendhandle_get_datablock_names(self->blo_handle, blocktype, &totnames);
226         list = PyList_New(totnames);
227
228         if (names) {
229                 int counter = 0;
230                 for (l = names; l; l = l->next) {
231                         PyList_SET_ITEM(list, counter, PyUnicode_FromString((char *)l->link));
232                         counter++;
233                 }
234                 BLI_linklist_free(names, free); /* free linklist *and* each node's data */
235         }
236
237         return list;
238 }
239
240 static PyObject *bpy_lib_enter(BPy_Library *self)
241 {
242         PyObject *ret;
243         BPy_Library *self_from;
244         PyObject *from_dict = _PyDict_NewPresized(MAX_LIBARRAY);
245         ReportList reports;
246
247         BKE_reports_init(&reports, RPT_STORE);
248
249         self->blo_handle = BLO_blendhandle_from_file(self->abspath, &reports);
250
251         if (self->blo_handle == NULL) {
252                 if (BPy_reports_to_error(&reports, PyExc_IOError, true) != -1) {
253                         PyErr_Format(PyExc_IOError,
254                                      "load: %s failed to open blend file",
255                                      self->abspath);
256                 }
257                 return NULL;
258         }
259         else {
260                 int i = 0, code;
261                 while ((code = BKE_idcode_iter_step(&i))) {
262                         if (BKE_idcode_is_linkable(code)) {
263                                 const char *name_plural = BKE_idcode_to_name_plural(code);
264                                 PyObject *str = PyUnicode_FromString(name_plural);
265                                 PyObject *item;
266
267                                 PyDict_SetItem(self->dict, str, item = PyList_New(0));
268                                 Py_DECREF(item);
269                                 PyDict_SetItem(from_dict, str, item = _bpy_names(self, code));
270                                 Py_DECREF(item);
271
272                                 Py_DECREF(str);
273                         }
274                 }
275         }
276
277         /* create a dummy */
278         self_from = PyObject_New(BPy_Library, &bpy_lib_Type);
279         BLI_strncpy(self_from->relpath, self->relpath, sizeof(self_from->relpath));
280         BLI_strncpy(self_from->abspath, self->abspath, sizeof(self_from->abspath));
281
282         self_from->blo_handle = NULL;
283         self_from->flag = 0;
284         self_from->dict = from_dict; /* owns the dict */
285
286         /* return pair */
287         ret = PyTuple_New(2);
288         PyTuple_SET_ITEMS(ret,
289                 (PyObject *)self_from,
290                 (PyObject *)self);
291         Py_INCREF(self);
292
293         BKE_reports_clear(&reports);
294
295         return ret;
296 }
297
298 static void bpy_lib_exit_warn_idname(BPy_Library *self, const char *name_plural, const char *idname)
299 {
300         PyObject *exc, *val, *tb;
301         PyErr_Fetch(&exc, &val, &tb);
302         if (PyErr_WarnFormat(PyExc_UserWarning, 1,
303                              "load: '%s' does not contain %s[\"%s\"]",
304                              self->abspath, name_plural, idname))
305         {
306                 /* Spurious errors can appear at shutdown */
307                 if (PyErr_ExceptionMatches(PyExc_Warning)) {
308                         PyErr_WriteUnraisable((PyObject *)self);
309                 }
310         }
311         PyErr_Restore(exc, val, tb);
312 }
313
314 static void bpy_lib_exit_warn_type(BPy_Library *self, PyObject *item)
315 {
316         PyObject *exc, *val, *tb;
317         PyErr_Fetch(&exc, &val, &tb);
318         if (PyErr_WarnFormat(PyExc_UserWarning, 1,
319                              "load: '%s' expected a string type, not a %.200s",
320                              self->abspath, Py_TYPE(item)->tp_name))
321         {
322                 /* Spurious errors can appear at shutdown */
323                 if (PyErr_ExceptionMatches(PyExc_Warning)) {
324                         PyErr_WriteUnraisable((PyObject *)self);
325                 }
326         }
327         PyErr_Restore(exc, val, tb);
328 }
329
330 static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *UNUSED(args))
331 {
332         Main *bmain = CTX_data_main(BPy_GetContext());
333         Main *mainl = NULL;
334         int err = 0;
335         const bool do_append = ((self->flag & FILE_LINK) == 0);
336
337         BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, true);
338
339         /* here appending/linking starts */
340         mainl = BLO_library_link_begin(bmain, &(self->blo_handle), self->relpath);
341
342         {
343                 int idcode_step = 0, idcode;
344                 while ((idcode = BKE_idcode_iter_step(&idcode_step))) {
345                         if (BKE_idcode_is_linkable(idcode) && (idcode != ID_WS || do_append)) {
346                                 const char *name_plural = BKE_idcode_to_name_plural(idcode);
347                                 PyObject *ls = PyDict_GetItemString(self->dict, name_plural);
348                                 // printf("lib: %s\n", name_plural);
349                                 if (ls && PyList_Check(ls)) {
350                                         /* loop */
351                                         Py_ssize_t size = PyList_GET_SIZE(ls);
352                                         Py_ssize_t i;
353
354                                         for (i = 0; i < size; i++) {
355                                                 PyObject *item_src = PyList_GET_ITEM(ls, i);
356                                                 PyObject *item_dst;  /* must be set below */
357                                                 const char *item_idname = _PyUnicode_AsString(item_src);
358
359                                                 // printf("  %s\n", item_idname);
360
361                                                 if (item_idname) {
362                                                         ID *id = BLO_library_link_named_part(mainl, &(self->blo_handle), idcode, item_idname);
363                                                         if (id) {
364 #ifdef USE_RNA_DATABLOCKS
365                                                                 /* swap name for pointer to the id */
366                                                                 item_dst = PyCapsule_New((void *)id, NULL, NULL);
367 #else
368                                                                 /* leave as is */
369                                                                 continue;
370 #endif
371                                                         }
372                                                         else {
373                                                                 bpy_lib_exit_warn_idname(self, name_plural, item_idname);
374                                                                 /* just warn for now */
375                                                                 /* err = -1; */
376                                                                 item_dst = Py_INCREF_RET(Py_None);
377                                                         }
378
379                                                         /* ID or None */
380                                                 }
381                                                 else {
382                                                         /* XXX, could complain about this */
383                                                         bpy_lib_exit_warn_type(self, item_src);
384                                                         PyErr_Clear();
385                                                         item_dst = Py_INCREF_RET(Py_None);
386                                                 }
387
388                                                 /* item_dst must be new or already incref'd */
389                                                 Py_DECREF(item_src);
390                                                 PyList_SET_ITEM(ls, i, item_dst);
391                                         }
392                                 }
393                         }
394                 }
395         }
396
397         if (err == -1) {
398                 /* exception raised above, XXX, this leaks some memory */
399                 BLO_blendhandle_close(self->blo_handle);
400                 self->blo_handle = NULL;
401                 BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, false);
402                 return NULL;
403         }
404         else {
405                 Library *lib = mainl->curlib; /* newly added lib, assign before append end */
406                 BLO_library_link_end(mainl, &(self->blo_handle), self->flag, NULL, NULL, NULL);
407                 BLO_blendhandle_close(self->blo_handle);
408                 self->blo_handle = NULL;
409
410                 GHash *old_to_new_ids = BLI_ghash_ptr_new(__func__);
411
412                 /* copied from wm_operator.c */
413                 {
414                         /* mark all library linked objects to be updated */
415                         BKE_main_lib_objects_recalc_all(bmain);
416
417                         /* append, rather than linking */
418                         if (do_append) {
419                                 BKE_library_make_local(bmain, lib, old_to_new_ids, true, false);
420                         }
421                 }
422
423                 BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, false);
424
425                 /* finally swap the capsules for real bpy objects
426                  * important since BLO_library_append_end initializes NodeTree types used by srna->refine */
427 #ifdef USE_RNA_DATABLOCKS
428                 {
429                         int idcode_step = 0, idcode;
430                         while ((idcode = BKE_idcode_iter_step(&idcode_step))) {
431                                 if (BKE_idcode_is_linkable(idcode) && (idcode != ID_WS || do_append)) {
432                                         const char *name_plural = BKE_idcode_to_name_plural(idcode);
433                                         PyObject *ls = PyDict_GetItemString(self->dict, name_plural);
434                                         if (ls && PyList_Check(ls)) {
435                                                 Py_ssize_t size = PyList_GET_SIZE(ls);
436                                                 Py_ssize_t i;
437                                                 PyObject *item;
438
439                                                 for (i = 0; i < size; i++) {
440                                                         item = PyList_GET_ITEM(ls, i);
441                                                         if (PyCapsule_CheckExact(item)) {
442                                                                 PointerRNA id_ptr;
443                                                                 ID *id;
444
445                                                                 id = PyCapsule_GetPointer(item, NULL);
446                                                                 id = BLI_ghash_lookup_default(old_to_new_ids, id, id);
447                                                                 Py_DECREF(item);
448
449                                                                 RNA_id_pointer_create(id, &id_ptr);
450                                                                 item = pyrna_struct_CreatePyObject(&id_ptr);
451                                                                 PyList_SET_ITEM(ls, i, item);
452                                                         }
453                                                 }
454                                         }
455                                 }
456                         }
457                 }
458 #endif  /* USE_RNA_DATABLOCKS */
459
460                 BLI_ghash_free(old_to_new_ids, NULL, NULL);
461                 Py_RETURN_NONE;
462         }
463 }
464
465 static PyObject *bpy_lib_dir(BPy_Library *self)
466 {
467         return PyDict_Keys(self->dict);
468 }
469
470
471 int BPY_library_load_module(PyObject *mod_par)
472 {
473         static PyMethodDef load_meth = {
474                 "load", (PyCFunction)bpy_lib_load,
475                 METH_STATIC | METH_VARARGS | METH_KEYWORDS,
476                 bpy_lib_load_doc,
477         };
478         PyModule_AddObject(mod_par, "_library_load", PyCFunction_New(&load_meth, NULL));
479
480         /* some compilers don't like accessing this directly, delay assignment */
481         bpy_lib_Type.tp_getattro = PyObject_GenericGetAttr;
482
483         if (PyType_Ready(&bpy_lib_Type) < 0)
484                 return -1;
485
486         return 0;
487 }