382a513f40af2c5b9257db53a25bae4a20fa135a
[blender.git] / source / blender / python / intern / bpy_library.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  * Contributor(s): Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/python/intern/bpy_library.c
24  *  \ingroup pythonintern
25  */
26
27 /* nifty feature. swap out strings for RNA data */
28 #define USE_RNA_DATABLOCKS
29
30 #include <Python.h>
31 #include <stddef.h>
32
33 #include "BLO_readfile.h"
34
35 #include "BKE_global.h"
36 #include "BKE_main.h"
37 #include "BKE_library.h"
38 #include "BKE_idcode.h"
39 #include "BKE_report.h"
40 #include "BKE_context.h"
41
42 #include "BLI_utildefines.h"
43 #include "BLI_string.h"
44 #include "BLI_linklist.h"
45 #include "BLI_path_util.h"
46 #include "BLI_listbase.h"
47
48 #include "DNA_space_types.h" /* FILE_LINK, FILE_RELPATH */
49
50 #include "bpy_util.h"
51
52 #ifdef USE_RNA_DATABLOCKS
53 #  include "bpy_rna.h"
54 #  include "RNA_access.h"
55 #endif
56
57 typedef struct {
58         PyObject_HEAD /* required python macro   */
59         /* collection iterator specific parts */
60         char relpath[FILE_MAX];
61         char abspath[FILE_MAX]; /* absolute path */
62         BlendHandle *blo_handle;
63         int flag;
64         PyObject *dict;
65 } BPy_Library;
66
67 static PyObject *bpy_lib_load(PyObject *self, PyObject *args, PyObject *kwds);
68 static PyObject *bpy_lib_enter(BPy_Library *self, PyObject *args);
69 static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *args);
70 static PyObject *bpy_lib_dir(BPy_Library *self);
71
72 static PyMethodDef bpy_lib_methods[]= {
73         {"__enter__", (PyCFunction)bpy_lib_enter, METH_NOARGS},
74         {"__exit__", (PyCFunction)bpy_lib_exit, METH_VARARGS},
75         {"__dir__", (PyCFunction)bpy_lib_dir, METH_NOARGS},
76         {NULL}           /* sentinel */
77 };
78
79 static void bpy_lib_dealloc(BPy_Library *self)
80 {
81         Py_XDECREF(self->dict);
82         Py_TYPE(self)->tp_free(self);
83 }
84
85
86 static PyTypeObject bpy_lib_Type= {
87         PyVarObject_HEAD_INIT(NULL, 0)
88         "bpy_lib",              /* tp_name */
89         sizeof(BPy_Library),                    /* tp_basicsize */
90         0,                                                      /* tp_itemsize */
91         /* methods */
92         (destructor)bpy_lib_dealloc,/* tp_dealloc */
93         NULL,                       /* printfunc tp_print; */
94         NULL,                                           /* getattrfunc tp_getattr; */
95         NULL,                       /* setattrfunc tp_setattr; */
96         NULL,                                           /* 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         NULL /*PyObject_GenericGetAttr is assigned later */,    /* 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 ***/
131         NULL, /* subclassed */          /* 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
165 PyDoc_STRVAR(bpy_lib_load_doc,
166 ".. method:: load(filepath, link=False, relative=False)\n"
167 "\n"
168 "   Returns a context manager which exposes 2 library objects on entering.\n"
169 "   Each object has attributes matching bpy.data which are lists of strings to be linked.\n"
170 "\n"
171 "   :arg filepath: The path to a blend file.\n"
172 "   :type filepath: string\n"
173 "   :arg link: When False reference to the original file is lost.\n"
174 "   :type link: bool\n"
175 "   :arg relative: When True the path is stored relative to the open blend file.\n"
176 "   :type relative: bool\n"
177 );
178 static PyObject *bpy_lib_load(PyObject *UNUSED(self), PyObject *args, PyObject *kwds)
179 {
180         static const char *kwlist[]= {"filepath", "link", "relative", NULL};
181         BPy_Library *ret;
182         const char* filename= NULL;
183         int is_rel= 0, is_link= 0;
184
185         if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|ii:load", (char **)kwlist, &filename, &is_link, &is_rel))
186                 return NULL;
187
188         ret= PyObject_New(BPy_Library, &bpy_lib_Type);
189
190         BLI_strncpy(ret->relpath, filename, sizeof(ret->relpath));
191         BLI_strncpy(ret->abspath, filename, sizeof(ret->abspath));
192         BLI_path_abs(ret->abspath, G.main->name);
193
194         ret->blo_handle= NULL;
195         ret->flag=      (is_link ? FILE_LINK : 0) |
196                                 (is_rel ? FILE_RELPATH : 0);
197
198         ret->dict= PyDict_New();
199
200         return (PyObject *)ret;
201 }
202
203 static PyObject *_bpy_names(BPy_Library *self, int blocktype)
204 {
205         PyObject *list;
206         LinkNode *l, *names;
207         int totnames;
208
209         names= BLO_blendhandle_get_datablock_names(self->blo_handle, blocktype, &totnames);
210
211         if (names) {
212                 int counter= 0;
213                 list= PyList_New(totnames);
214                 for (l= names; l; l= l->next) {
215                         PyList_SET_ITEM(list, counter, PyUnicode_FromString((char *)l->link));
216                         counter++;
217                 }
218                 BLI_linklist_free(names, free); /* free linklist *and* each node's data */
219         }
220         else {
221                 list= PyList_New(0);
222         }
223
224         return list;
225 }
226
227 static PyObject *bpy_lib_enter(BPy_Library *self, PyObject *UNUSED(args))
228 {
229         PyObject *ret;
230         BPy_Library *self_from;
231         PyObject *from_dict= PyDict_New();
232         ReportList reports;
233
234         BKE_reports_init(&reports, RPT_STORE);
235
236         self->blo_handle= BLO_blendhandle_from_file(self->abspath, &reports);
237
238         if (self->blo_handle == NULL) {
239                 if (BPy_reports_to_error(&reports, PyExc_IOError, TRUE) != -1) {
240                         PyErr_Format(PyExc_IOError,
241                                      "load: %s failed to open blend file",
242                                      self->abspath);
243                 }
244                 return NULL;
245         }
246         else {
247                 int i= 0, code;
248                 while ((code= BKE_idcode_iter_step(&i))) {
249                         if (BKE_idcode_is_linkable(code)) {
250                                 const char *name_plural= BKE_idcode_to_name_plural(code);
251                                 PyObject *str= PyUnicode_FromString(name_plural);
252                                 PyDict_SetItem(self->dict, str, PyList_New(0));
253                                 PyDict_SetItem(from_dict, str, _bpy_names(self, code));
254                                 Py_DECREF(str);
255                         }
256                 }
257         }
258
259         /* create a dummy */
260         self_from= PyObject_New(BPy_Library, &bpy_lib_Type);
261         BLI_strncpy(self_from->relpath, self->relpath, sizeof(BPy_Library));
262         BLI_strncpy(self_from->abspath, self->abspath, sizeof(BPy_Library));
263
264         self_from->blo_handle= NULL;
265         self_from->flag= 0;
266         self_from->dict= from_dict; /* owns the dict */
267
268         /* return pair */
269         ret= PyTuple_New(2);
270
271         PyTuple_SET_ITEM(ret, 0, (PyObject *)self_from);
272
273         PyTuple_SET_ITEM(ret, 1, (PyObject *)self);
274         Py_INCREF(self);
275
276         BKE_reports_clear(&reports);
277
278         return ret;
279 }
280
281 static void bpy_lib_exit_warn_idname(BPy_Library *self, const char *name_plural, const char *idname)
282 {
283         PyObject *exc, *val, *tb;
284         PyErr_Fetch(&exc, &val, &tb);
285         if (PyErr_WarnFormat(PyExc_UserWarning, 1,
286                                                  "load: '%s' does not contain %s[\"%s\"]",
287                              self->abspath, name_plural, idname)) {
288                 /* Spurious errors can appear at shutdown */
289                 if (PyErr_ExceptionMatches(PyExc_Warning)) {
290                         PyErr_WriteUnraisable((PyObject *)self);
291                 }
292         }
293         PyErr_Restore(exc, val, tb);
294 }
295
296 static void bpy_lib_exit_warn_type(BPy_Library *self, PyObject *item)
297 {
298         PyObject *exc, *val, *tb;
299         PyErr_Fetch(&exc, &val, &tb);
300         if (PyErr_WarnFormat(PyExc_UserWarning, 1,
301                                                  "load: '%s' expected a string type, not a %.200s",
302                              self->abspath, Py_TYPE(item)->tp_name)) {
303                 /* Spurious errors can appear at shutdown */
304                 if (PyErr_ExceptionMatches(PyExc_Warning)) {
305                         PyErr_WriteUnraisable((PyObject *)self);
306                 }
307         }
308         PyErr_Restore(exc, val, tb);
309 }
310
311 static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *UNUSED(args))
312 {
313         Main *bmain= CTX_data_main(BPy_GetContext());
314         Main *mainl= NULL;
315         int err= 0;
316
317         flag_all_listbases_ids(LIB_PRE_EXISTING, 1);
318
319         /* here appending/linking starts */
320         mainl= BLO_library_append_begin(bmain, &(self->blo_handle), self->relpath);
321
322         {
323                 int i= 0, code;
324                 while ((code= BKE_idcode_iter_step(&i))) {
325                         if (BKE_idcode_is_linkable(code)) {
326                                 const char *name_plural= BKE_idcode_to_name_plural(code);
327                                 PyObject *ls= PyDict_GetItemString(self->dict, name_plural);
328                                 // printf("lib: %s\n", name_plural);
329                                 if (ls && PyList_Check(ls)) {
330                                         /* loop */
331                                         Py_ssize_t size= PyList_GET_SIZE(ls);
332                                         Py_ssize_t i;
333                                         PyObject *item;
334                                         const char *item_str;
335
336                                         for (i= 0; i < size; i++) {
337                                                 item= PyList_GET_ITEM(ls, i);
338                                                 item_str= _PyUnicode_AsString(item);
339
340                                                 // printf("  %s\n", item_str);
341
342                                                 if (item_str) {
343                                                         ID *id= BLO_library_append_named_part(mainl, &(self->blo_handle), item_str, code);
344                                                         if (id) {
345 #ifdef USE_RNA_DATABLOCKS
346                                                                 PointerRNA id_ptr;
347                                                                 RNA_id_pointer_create(id, &id_ptr);
348                                                                 Py_DECREF(item);
349                                                                 item= pyrna_struct_CreatePyObject(&id_ptr);
350 #endif
351                                                         }
352                                                         else {
353                                                                 bpy_lib_exit_warn_idname(self, name_plural, item_str);
354                                                                 /* just warn for now */
355                                                                 /* err = -1; */
356 #ifdef USE_RNA_DATABLOCKS
357                                                                 item= Py_None;
358                                                                 Py_INCREF(item);
359 #endif
360                                                         }
361
362                                                         /* ID or None */
363                                                 }
364                                                 else {
365                                                         /* XXX, could complain about this */
366                                                         bpy_lib_exit_warn_type(self, item);
367                                                         PyErr_Clear();
368
369 #ifdef USE_RNA_DATABLOCKS
370                                                         item= Py_None;
371                                                         Py_INCREF(item);
372 #endif
373                                                 }
374
375 #ifdef USE_RNA_DATABLOCKS
376                                                 PyList_SET_ITEM(ls, i, item);
377 #endif
378                                         }
379                                 }
380                         }
381                 }
382         }
383
384         if (err == -1) {
385                 /* exception raised above, XXX, this leaks some memory */
386                 BLO_blendhandle_close(self->blo_handle);
387                 self->blo_handle= NULL;
388                 flag_all_listbases_ids(LIB_PRE_EXISTING, 0);
389                 return NULL;
390         }
391         else {
392                 BLO_library_append_end(NULL, mainl, &(self->blo_handle), 0, self->flag);
393                 BLO_blendhandle_close(self->blo_handle);
394                 self->blo_handle= NULL;
395
396                 {       /* copied from wm_operator.c */
397                         /* mark all library linked objects to be updated */
398                         recalc_all_library_objects(G.main);
399
400                         /* append, rather than linking */
401                         if ((self->flag & FILE_LINK)==0) {
402                                 Library *lib= BLI_findstring(&G.main->library, self->abspath, offsetof(Library, name));
403                                 if (lib)  BKE_library_make_local(bmain, lib, 1);
404                                 else      BLI_assert(!"cant find name of just added library!");
405                         }
406                 }
407
408                 flag_all_listbases_ids(LIB_PRE_EXISTING, 0);
409
410                 Py_RETURN_NONE;
411         }
412 }
413
414 static PyObject *bpy_lib_dir(BPy_Library *self)
415 {
416         return PyDict_Keys(self->dict);
417 }
418
419
420 int bpy_lib_init(PyObject *mod_par)
421 {
422         static PyMethodDef load_meth= {"load", (PyCFunction)bpy_lib_load, METH_STATIC|METH_VARARGS|METH_KEYWORDS, bpy_lib_load_doc};
423         PyModule_AddObject(mod_par, "_library_load", PyCFunction_New(&load_meth, NULL));
424
425         /* some compilers dont like accessing this directly, delay assignment */
426         bpy_lib_Type.tp_getattro= PyObject_GenericGetAttr;
427
428         if (PyType_Ready(&bpy_lib_Type) < 0)
429                 return -1;
430
431         return 0;
432 }