ead10efb212b75db1b1b4ab4e82e7f67b4463595
[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_capi_utils.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, compress=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 *kw)
73 {
74         /* args */
75         const char *filepath;
76         char filepath_abs[FILE_MAX];
77         PyObject *datablocks = NULL;
78         bool use_relative_remap = false, use_fake_user = false, use_compress = false;
79
80         static const char *_keywords[] = {
81                 "filepath", "datablocks",
82                 /* optional */
83                 "relative_remap", "fake_user", "compress",
84                 NULL,
85         };
86         static _PyArg_Parser _parser = {"sO!|$O&O&O&:write", _keywords, 0};
87         if (!_PyArg_ParseTupleAndKeywordsFast(
88                 args, kw, &_parser,
89                 &filepath,
90                 &PySet_Type, &datablocks,
91                 PyC_ParseBool, &use_relative_remap,
92                 PyC_ParseBool, &use_fake_user,
93                 PyC_ParseBool, &use_compress))
94         {
95                 return NULL;
96         }
97
98         Main *bmain_src = G.main;
99         int write_flags = 0;
100
101         if (use_relative_remap) {
102                 write_flags |= G_FILE_RELATIVE_REMAP;
103         }
104
105         if (use_compress) {
106                 write_flags |= G_FILE_COMPRESS;
107         }
108
109         BLI_strncpy(filepath_abs, filepath, FILE_MAX);
110         BLI_path_abs(filepath_abs, BKE_main_blendfile_path_from_global());
111
112         BKE_blendfile_write_partial_begin(bmain_src);
113
114         /* array of ID's and backup any data we modify */
115         struct {
116                 ID *id;
117                 /* original values */
118                 short id_flag;
119                 short id_us;
120         } *id_store_array, *id_store;
121         int id_store_len = 0;
122
123         PyObject *ret;
124
125         /* collect all id data from the set and store in 'id_store_array' */
126         {
127                 Py_ssize_t pos, hash;
128                 PyObject *key;
129
130                 id_store_array = MEM_mallocN(sizeof(*id_store_array) * PySet_Size(datablocks), __func__);
131                 id_store = id_store_array;
132
133                 pos = hash = 0;
134                 while (_PySet_NextEntry(datablocks, &pos, &key, &hash)) {
135
136                         if (!pyrna_id_FromPyObject(key, &id_store->id)) {
137                                 PyErr_Format(PyExc_TypeError,
138                                              "Expected and ID type, not %.200s",
139                                              Py_TYPE(key)->tp_name);
140                                 ret = NULL;
141                                 goto finally;
142                         }
143                         else {
144                                 id_store->id_flag = id_store->id->flag;
145                                 id_store->id_us = id_store->id->us;
146
147                                 if (use_fake_user) {
148                                         id_store->id->flag |= LIB_FAKEUSER;
149                                 }
150                                 id_store->id->us = 1;
151
152                                 BKE_blendfile_write_partial_tag_ID(id_store->id, true);
153
154                                 id_store_len += 1;
155                                 id_store++;
156                         }
157                 }
158         }
159
160         /* write blend */
161         int retval = 0;
162         ReportList reports;
163
164         BKE_reports_init(&reports, RPT_STORE);
165
166         retval = BKE_blendfile_write_partial(bmain_src, filepath_abs, write_flags, &reports);
167
168         /* cleanup state */
169         BKE_blendfile_write_partial_end(bmain_src);
170
171         if (retval) {
172                 BKE_reports_print(&reports, RPT_ERROR_ALL);
173                 BKE_reports_clear(&reports);
174                 ret = Py_None;
175                 Py_INCREF(ret);
176         }
177         else {
178                 if (BPy_reports_to_error(&reports, PyExc_IOError, true) == 0) {
179                         PyErr_SetString(PyExc_IOError, "Unknown error writing library data");
180                 }
181                 ret = NULL;
182         }
183
184
185 finally:
186
187         /* clear all flags for ID's added to the store (may run on error too) */
188         id_store = id_store_array;
189
190         for (int i = 0; i < id_store_len; id_store++, i++) {
191
192
193                 if (use_fake_user) {
194                         if ((id_store->id_flag & LIB_FAKEUSER) == 0) {
195                                 id_store->id->flag &= ~LIB_FAKEUSER;
196                         }
197                 }
198
199                 id_store->id->us = id_store->id_us;
200
201                 BKE_blendfile_write_partial_tag_ID(id_store->id, false);
202         }
203
204         MEM_freeN(id_store_array);
205
206         return ret;
207 }
208
209
210 int BPY_library_write_module(PyObject *mod_par)
211 {
212         static PyMethodDef write_meth = {
213                 "write", (PyCFunction)bpy_lib_write,
214                 METH_STATIC | METH_VARARGS | METH_KEYWORDS,
215                 bpy_lib_write_doc,
216         };
217
218         PyModule_AddObject(mod_par, "_library_write", PyCFunction_New(&write_meth, NULL));
219
220         return 0;
221 }