4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * Contributor(s): Campbell Barton
22 * ***** END GPL LICENSE BLOCK *****
25 /** \file blender/python/intern/bpy_library.c
26 * \ingroup pythonintern
29 /* nifty feature. swap out strings for RNA data */
30 #define USE_RNA_DATABLOCKS
35 #include "BLO_readfile.h"
37 #include "BKE_global.h"
39 #include "BKE_library.h"
40 #include "BKE_idcode.h"
41 #include "BKE_report.h"
43 #include "BLI_utildefines.h"
44 #include "BLI_string.h"
45 #include "BLI_linklist.h"
46 #include "BLI_path_util.h"
47 #include "BLI_listbase.h"
49 #include "DNA_space_types.h" /* FILE_LINK, FILE_RELPATH */
53 #ifdef USE_RNA_DATABLOCKS
55 # include "RNA_access.h"
59 PyObject_HEAD /* required python macro */
60 /* collection iterator spesific parts */
61 char relpath[FILE_MAX];
62 char abspath[FILE_MAX]; /* absolute path */
63 BlendHandle *blo_handle;
68 static PyObject *bpy_lib_load(PyObject *self, PyObject *args, PyObject *kwds);
69 static PyObject *bpy_lib_enter(BPy_Library *self, PyObject *args);
70 static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *args);
71 static PyObject *bpy_lib_dir(BPy_Library *self);
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},
80 static void bpy_lib_dealloc(BPy_Library *self)
82 Py_XDECREF(self->dict);
83 Py_TYPE(self)->tp_free(self);
87 static PyTypeObject bpy_lib_Type= {
88 PyVarObject_HEAD_INIT(NULL, 0)
89 "bpy_lib", /* tp_name */
90 sizeof(BPy_Library), /* tp_basicsize */
93 (destructor)bpy_lib_dealloc,/* tp_dealloc */
94 NULL, /* printfunc tp_print; */
95 NULL, /* getattrfunc tp_getattr; */
96 NULL, /* setattrfunc tp_setattr; */
97 NULL, /* tp_compare */ /* DEPRECATED in python 3.0! */
100 /* Method suites for standard classes */
102 NULL, /* PyNumberMethods *tp_as_number; */
103 NULL, /* PySequenceMethods *tp_as_sequence; */
104 NULL, /* PyMappingMethods *tp_as_mapping; */
106 /* More standard operations (here for binary compatibility) */
108 NULL, /* hashfunc tp_hash; */
109 NULL, /* ternaryfunc tp_call; */
110 NULL, /* reprfunc tp_str; */
112 /* will only use these if this is a subtype of a py class */
113 NULL /*PyObject_GenericGetAttr is assigned later */, /* getattrofunc tp_getattro; */
114 NULL, /* setattrofunc tp_setattro; */
116 /* Functions to access object as input/output buffer */
117 NULL, /* PyBufferProcs *tp_as_buffer; */
119 /*** Flags to define presence of optional/expanded features ***/
120 Py_TPFLAGS_DEFAULT, /* long tp_flags; */
122 NULL, /* char *tp_doc; Documentation string */
123 /*** Assigned meaning in release 2.0 ***/
124 /* call function for all accessible objects */
125 NULL, /* traverseproc tp_traverse; */
127 /* delete references to contained objects */
128 NULL, /* inquiry tp_clear; */
130 /*** Assigned meaning in release 2.1 ***/
131 /*** rich comparisons ***/
132 NULL, /* subclassed */ /* richcmpfunc tp_richcompare; */
134 /*** weak reference enabler ***/
136 /*** Added in release 2.2 ***/
138 NULL, /* getiterfunc tp_iter; */
139 NULL, /* iternextfunc tp_iternext; */
141 /*** Attribute descriptor and subclassing stuff ***/
142 bpy_lib_methods, /* struct PyMethodDef *tp_methods; */
143 NULL, /* struct PyMemberDef *tp_members; */
144 NULL, /* struct PyGetSetDef *tp_getset; */
145 NULL, /* struct _typeobject *tp_base; */
146 NULL, /* PyObject *tp_dict; */
147 NULL, /* descrgetfunc tp_descr_get; */
148 NULL, /* descrsetfunc tp_descr_set; */
149 offsetof(BPy_Library, dict),/* long tp_dictoffset; */
150 NULL, /* initproc tp_init; */
151 NULL, /* allocfunc tp_alloc; */
152 NULL, /* newfunc tp_new; */
153 /* Low-level free-memory routine */
154 NULL, /* freefunc tp_free; */
155 /* For PyObject_IS_GC */
156 NULL, /* inquiry tp_is_gc; */
157 NULL, /* PyObject *tp_bases; */
158 /* method resolution order */
159 NULL, /* PyObject *tp_mro; */
160 NULL, /* PyObject *tp_cache; */
161 NULL, /* PyObject *tp_subclasses; */
162 NULL, /* PyObject *tp_weaklist; */
166 PyDoc_STRVAR(bpy_lib_load_doc,
167 ".. method:: load(filepath, link=False, relative=False)\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"
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"
179 static PyObject *bpy_lib_load(PyObject *UNUSED(self), PyObject *args, PyObject *kwds)
181 static const char *kwlist[]= {"filepath", "link", "relative", NULL};
183 const char* filename= NULL;
184 int is_rel= 0, is_link= 0;
186 if(!PyArg_ParseTupleAndKeywords(args, kwds, "s|ii:load", (char **)kwlist, &filename, &is_link, &is_rel))
189 ret= PyObject_New(BPy_Library, &bpy_lib_Type);
191 BLI_strncpy(ret->relpath, filename, sizeof(ret->relpath));
192 BLI_strncpy(ret->abspath, filename, sizeof(ret->abspath));
193 BLI_path_abs(ret->abspath, G.main->name);
195 ret->blo_handle= NULL;
196 ret->flag= (is_link ? FILE_LINK : 0) |
197 (is_rel ? FILE_RELPATH : 0);
199 ret->dict= PyDict_New();
201 return (PyObject *)ret;
204 static PyObject *_bpy_names(BPy_Library *self, int blocktype)
210 names= BLO_blendhandle_get_datablock_names(self->blo_handle, blocktype, &totnames);
214 list= PyList_New(totnames);
215 for(l= names; l; l= l->next) {
216 PyList_SET_ITEM(list, counter, PyUnicode_FromString((char * )l->link));
219 BLI_linklist_free(names, free); /* free linklist *and* each node's data */
228 static PyObject *bpy_lib_enter(BPy_Library *self, PyObject *UNUSED(args))
231 BPy_Library *self_from;
232 PyObject *from_dict= PyDict_New();
235 BKE_reports_init(&reports, RPT_STORE);
237 self->blo_handle= BLO_blendhandle_from_file(self->abspath, &reports);
239 if(self->blo_handle == NULL) {
240 if(BPy_reports_to_error(&reports, PyExc_IOError, TRUE) != -1) {
241 PyErr_Format(PyExc_IOError,
242 "load: %s failed to open blend file",
249 while((code= BKE_idcode_iter_step(&i))) {
250 if(BKE_idcode_is_linkable(code)) {
251 const char *name_plural= BKE_idcode_to_name_plural(code);
252 PyObject *str= PyUnicode_FromString(name_plural);
253 PyDict_SetItem(self->dict, str, PyList_New(0));
254 PyDict_SetItem(from_dict, str, _bpy_names(self, code));
261 self_from= PyObject_New(BPy_Library, &bpy_lib_Type);
262 BLI_strncpy(self_from->relpath, self->relpath, sizeof(BPy_Library));
263 BLI_strncpy(self_from->abspath, self->abspath, sizeof(BPy_Library));
265 self_from->blo_handle= NULL;
267 self_from->dict= from_dict; /* owns the dict */
272 PyTuple_SET_ITEM(ret, 0, (PyObject *)self_from);
274 PyTuple_SET_ITEM(ret, 1, (PyObject *)self);
277 BKE_reports_clear(&reports);
282 static void bpy_lib_exit_warn_idname(BPy_Library *self, const char *name_plural, const char *idname)
284 PyObject *exc, *val, *tb;
285 PyErr_Fetch(&exc, &val, &tb);
286 if (PyErr_WarnFormat(PyExc_UserWarning, 1,
287 "load: '%s' does not contain %s[\"%s\"]",
288 self->abspath, name_plural, idname)) {
289 /* Spurious errors can appear at shutdown */
290 if (PyErr_ExceptionMatches(PyExc_Warning)) {
291 PyErr_WriteUnraisable((PyObject *)self);
294 PyErr_Restore(exc, val, tb);
297 static void bpy_lib_exit_warn_type(BPy_Library *self, PyObject *item)
299 PyObject *exc, *val, *tb;
300 PyErr_Fetch(&exc, &val, &tb);
301 if (PyErr_WarnFormat(PyExc_UserWarning, 1,
302 "load: '%s' expected a string type, not a %.200s",
303 self->abspath, Py_TYPE(item)->tp_name)) {
304 /* Spurious errors can appear at shutdown */
305 if (PyErr_ExceptionMatches(PyExc_Warning)) {
306 PyErr_WriteUnraisable((PyObject *)self);
309 PyErr_Restore(exc, val, tb);
312 static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *UNUSED(args))
317 flag_all_listbases_ids(LIB_PRE_EXISTING, 1);
319 /* here appending/linking starts */
320 mainl= BLO_library_append_begin(BPy_GetContext(), &(self->blo_handle), self->relpath);
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)) {
331 Py_ssize_t size= PyList_GET_SIZE(ls);
334 const char *item_str;
336 for(i= 0; i < size; i++) {
337 item= PyList_GET_ITEM(ls, i);
338 item_str= _PyUnicode_AsString(item);
340 // printf(" %s\n", item_str);
343 ID *id= BLO_library_append_named_part(mainl, &(self->blo_handle), item_str, code);
345 #ifdef USE_RNA_DATABLOCKS
347 RNA_id_pointer_create(id, &id_ptr);
349 item= pyrna_struct_CreatePyObject(&id_ptr);
353 bpy_lib_exit_warn_idname(self, name_plural, item_str);
354 /* just warn for now */
356 #ifdef USE_RNA_DATABLOCKS
365 /* XXX, could complain about this */
366 bpy_lib_exit_warn_type(self, item);
369 #ifdef USE_RNA_DATABLOCKS
375 #ifdef USE_RNA_DATABLOCKS
376 PyList_SET_ITEM(ls, i, item);
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);
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;
396 { /* copied from wm_operator.c */
397 /* mark all library linked objects to be updated */
398 recalc_all_library_objects(G.main);
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) all_local(lib, 1);
404 else BLI_assert(!"cant find name of just added library!");
408 flag_all_listbases_ids(LIB_PRE_EXISTING, 0);
414 static PyObject *bpy_lib_dir(BPy_Library *self)
416 return PyDict_Keys(self->dict);
420 int bpy_lib_init(PyObject *mod_par)
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));
425 /* some compilers dont like accessing this directly, delay assignment */
426 bpy_lib_Type.tp_getattro= PyObject_GenericGetAttr;
428 if(PyType_Ready(&bpy_lib_Type) < 0)