Py-Driver: add 'self' option
[blender.git] / source / blender / python / intern / bpy_library_write.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_write.c
22  *  \ingroup pythonintern
23  *
24  * Python API for writing a set of data-blocks into a file.
25  * Useful for writing out asset-libraries, defines: `bpy.data.libraries.write(...)`.
26  */
27
28 #include <Python.h>
29 #include <stddef.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "BLI_utildefines.h"
34 #include "BLI_string.h"
35 #include "BLI_path_util.h"
36
37 #include "BKE_library.h"
38 #include "BKE_blendfile.h"
39 #include "BKE_global.h"
40 #include "BKE_main.h"
41 #include "BKE_report.h"
42
43 #include "RNA_types.h"
44
45 #include "bpy_rna.h"
46 #include "bpy_util.h"
47 #include "bpy_library.h"
48
49 #include "../generic/py_capi_utils.h"
50
51
52 PyDoc_STRVAR(bpy_lib_write_doc,
53 ".. method:: write(filepath, datablocks, relative_remap=False, fake_user=False)\n"
54 "\n"
55 "   Write data-blocks into a blend file.\n"
56 "\n"
57 "   .. note::\n"
58 "\n"
59 "      Indirectly referenced data-blocks will be expanded and written too.\n"
60 "\n"
61 "   :arg filepath: The path to write the blend-file.\n"
62 "   :type filepath: string\n"
63 "   :arg datablocks: set of data-blocks (:class:`bpy.types.ID` instances).\n"
64 "   :type datablocks: set\n"
65 "   :arg relative_remap: When True, remap the paths relative to the current blend-file.\n"
66 "   :type relative_remap: bool\n"
67 "   :arg fake_user: When True, data-blocks will be written with fake-user flag enabled.\n"
68 "   :type fake_user: bool\n"
69 "   :arg compress: When True, write a compressed blend file.\n"
70 "   :type compress: bool\n"
71 );
72 static PyObject *bpy_lib_write(PyObject *UNUSED(self), PyObject *args, PyObject *kwds)
73 {
74         static const char *kwlist[] = {
75                 "filepath", "datablocks",
76                 /* optional */
77                 "relative_remap", "fake_user", "compress",
78                 NULL,
79         };
80
81         /* args */
82         const char *filepath;
83         char filepath_abs[FILE_MAX];
84         PyObject *datablocks = NULL;
85         bool use_relative_remap = false, use_fake_user = false, use_compress = false;
86
87         if (!PyArg_ParseTupleAndKeywords(
88                 args, kwds,
89                 "sO!|$O&O&O&:write", (char **)kwlist,
90                 &filepath,
91                 &PySet_Type, &datablocks,
92                 PyC_ParseBool, &use_relative_remap,
93                 PyC_ParseBool, &use_fake_user,
94                 PyC_ParseBool, &use_compress))
95         {
96                 return NULL;
97         }
98
99         Main *bmain_src = G.main;
100         int write_flags = 0;
101
102         if (use_relative_remap) {
103                 write_flags |= G_FILE_RELATIVE_REMAP;
104         }
105
106         if (use_compress) {
107                 write_flags |= G_FILE_COMPRESS;
108         }
109
110         BLI_strncpy(filepath_abs, filepath, FILE_MAX);
111         BLI_path_abs(filepath_abs, G.main->name);
112
113         BKE_blendfile_write_partial_begin(bmain_src);
114
115         /* array of ID's and backup any data we modify */
116         struct {
117                 ID *id;
118                 /* original values */
119                 short id_flag;
120                 short id_us;
121         } *id_store_array, *id_store;
122         int id_store_len = 0;
123
124         PyObject *ret;
125
126         /* collect all id data from the set and store in 'id_store_array' */
127         {
128                 Py_ssize_t pos, hash;
129                 PyObject *key;
130
131                 id_store_array = MEM_mallocN(sizeof(*id_store_array) * PySet_Size(datablocks), __func__);
132                 id_store = id_store_array;
133
134                 pos = hash = 0;
135                 while (_PySet_NextEntry(datablocks, &pos, &key, &hash)) {
136
137                         if (!pyrna_id_FromPyObject(key, &id_store->id)) {
138                                 PyErr_Format(PyExc_TypeError,
139                                              "Expected and ID type, not %.200s",
140                                              Py_TYPE(key)->tp_name);
141                                 ret = NULL;
142                                 goto finally;
143                         }
144                         else {
145                                 id_store->id_flag = id_store->id->flag;
146                                 id_store->id_us = id_store->id->us;
147
148                                 if (use_fake_user) {
149                                         id_store->id->flag |= LIB_FAKEUSER;
150                                 }
151                                 id_store->id->us = 1;
152
153                                 BKE_blendfile_write_partial_tag_ID(id_store->id, true);
154
155                                 id_store_len += 1;
156                                 id_store++;
157                         }
158                 }
159         }
160
161         /* write blend */
162         int retval = 0;
163         ReportList reports;
164
165         BKE_reports_init(&reports, RPT_STORE);
166
167         retval = BKE_blendfile_write_partial(bmain_src, filepath_abs, write_flags, &reports);
168
169         /* cleanup state */
170         BKE_blendfile_write_partial_end(bmain_src);
171
172         if (retval) {
173                 BKE_reports_print(&reports, RPT_ERROR_ALL);
174                 BKE_reports_clear(&reports);
175                 ret = Py_None;
176                 Py_INCREF(ret);
177         }
178         else {
179                 if (BPy_reports_to_error(&reports, PyExc_IOError, true) == 0) {
180                         PyErr_SetString(PyExc_IOError, "Unknown error writing library data");
181                 }
182                 ret = NULL;
183         }
184
185
186 finally:
187
188         /* clear all flags for ID's added to the store (may run on error too) */
189         id_store = id_store_array;
190
191         for (int i = 0; i < id_store_len; id_store++, i++) {
192
193
194                 if (use_fake_user) {
195                         if ((id_store->id_flag & LIB_FAKEUSER) == 0) {
196                                 id_store->id->flag &= ~LIB_FAKEUSER;
197                         }
198                 }
199
200                 id_store->id->us = id_store->id_us;
201
202                 BKE_blendfile_write_partial_tag_ID(id_store->id, false);
203         }
204
205         MEM_freeN(id_store_array);
206
207         return ret;
208 }
209
210
211 int BPY_library_write_module(PyObject *mod_par)
212 {
213         static PyMethodDef write_meth = {
214                 "write", (PyCFunction)bpy_lib_write,
215                 METH_STATIC | METH_VARARGS | METH_KEYWORDS,
216                 bpy_lib_write_doc,
217         };
218
219         PyModule_AddObject(mod_par, "_library_write", PyCFunction_New(&write_meth, NULL));
220
221         return 0;
222 }