Bevel mod: cleanup flags and extra data.
[blender.git] / source / blender / python / intern / bpy_library_write.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 /** \file
18  * \ingroup pythonintern
19  *
20  * Python API for writing a set of data-blocks into a file.
21  * Useful for writing out asset-libraries, defines: `bpy.data.libraries.write(...)`.
22  */
23
24 #include <Python.h>
25 #include <stddef.h>
26
27 #include "MEM_guardedalloc.h"
28
29 #include "BLI_utildefines.h"
30 #include "BLI_string.h"
31 #include "BLI_path_util.h"
32
33 #include "BKE_blendfile.h"
34 #include "BKE_global.h"
35 #include "BKE_main.h"
36 #include "BKE_report.h"
37
38 #include "RNA_types.h"
39
40 #include "bpy_rna.h"
41 #include "bpy_capi_utils.h"
42 #include "bpy_library.h"
43
44 #include "../generic/py_capi_utils.h"
45
46
47 PyDoc_STRVAR(bpy_lib_write_doc,
48 ".. method:: write(filepath, datablocks, relative_remap=False, fake_user=False, compress=False)\n"
49 "\n"
50 "   Write data-blocks into a blend file.\n"
51 "\n"
52 "   .. note::\n"
53 "\n"
54 "      Indirectly referenced data-blocks will be expanded and written too.\n"
55 "\n"
56 "   :arg filepath: The path to write the blend-file.\n"
57 "   :type filepath: string\n"
58 "   :arg datablocks: set of data-blocks (:class:`bpy.types.ID` instances).\n"
59 "   :type datablocks: set\n"
60 "   :arg relative_remap: When True, remap the paths relative to the current blend-file.\n"
61 "   :type relative_remap: bool\n"
62 "   :arg fake_user: When True, data-blocks will be written with fake-user flag enabled.\n"
63 "   :type fake_user: bool\n"
64 "   :arg compress: When True, write a compressed blend file.\n"
65 "   :type compress: bool\n"
66 );
67 static PyObject *bpy_lib_write(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
68 {
69         /* args */
70         const char *filepath;
71         char filepath_abs[FILE_MAX];
72         PyObject *datablocks = NULL;
73         bool use_relative_remap = false, use_fake_user = false, use_compress = false;
74
75         static const char *_keywords[] = {
76                 "filepath", "datablocks",
77                 /* optional */
78                 "relative_remap", "fake_user", "compress",
79                 NULL,
80         };
81         static _PyArg_Parser _parser = {"sO!|$O&O&O&:write", _keywords, 0};
82         if (!_PyArg_ParseTupleAndKeywordsFast(
83                 args, kw, &_parser,
84                 &filepath,
85                 &PySet_Type, &datablocks,
86                 PyC_ParseBool, &use_relative_remap,
87                 PyC_ParseBool, &use_fake_user,
88                 PyC_ParseBool, &use_compress))
89         {
90                 return NULL;
91         }
92
93         Main *bmain_src = G_MAIN;
94         int write_flags = 0;
95
96         if (use_relative_remap) {
97                 write_flags |= G_FILE_RELATIVE_REMAP;
98         }
99
100         if (use_compress) {
101                 write_flags |= G_FILE_COMPRESS;
102         }
103
104         BLI_strncpy(filepath_abs, filepath, FILE_MAX);
105         BLI_path_abs(filepath_abs, BKE_main_blendfile_path_from_global());
106
107         BKE_blendfile_write_partial_begin(bmain_src);
108
109         /* array of ID's and backup any data we modify */
110         struct {
111                 ID *id;
112                 /* original values */
113                 short id_flag;
114                 short id_us;
115         } *id_store_array, *id_store;
116         int id_store_len = 0;
117
118         PyObject *ret;
119
120         /* collect all id data from the set and store in 'id_store_array' */
121         {
122                 Py_ssize_t pos, hash;
123                 PyObject *key;
124
125                 id_store_array = MEM_mallocN(sizeof(*id_store_array) * PySet_Size(datablocks), __func__);
126                 id_store = id_store_array;
127
128                 pos = hash = 0;
129                 while (_PySet_NextEntry(datablocks, &pos, &key, &hash)) {
130
131                         if (!pyrna_id_FromPyObject(key, &id_store->id)) {
132                                 PyErr_Format(PyExc_TypeError,
133                                              "Expected an ID type, not %.200s",
134                                              Py_TYPE(key)->tp_name);
135                                 ret = NULL;
136                                 goto finally;
137                         }
138                         else {
139                                 id_store->id_flag = id_store->id->flag;
140                                 id_store->id_us = id_store->id->us;
141
142                                 if (use_fake_user) {
143                                         id_store->id->flag |= LIB_FAKEUSER;
144                                 }
145                                 id_store->id->us = 1;
146
147                                 BKE_blendfile_write_partial_tag_ID(id_store->id, true);
148
149                                 id_store_len += 1;
150                                 id_store++;
151                         }
152                 }
153         }
154
155         /* write blend */
156         int retval = 0;
157         ReportList reports;
158
159         BKE_reports_init(&reports, RPT_STORE);
160
161         retval = BKE_blendfile_write_partial(bmain_src, filepath_abs, write_flags, &reports);
162
163         /* cleanup state */
164         BKE_blendfile_write_partial_end(bmain_src);
165
166         if (retval) {
167                 BKE_reports_print(&reports, RPT_ERROR_ALL);
168                 BKE_reports_clear(&reports);
169                 ret = Py_None;
170                 Py_INCREF(ret);
171         }
172         else {
173                 if (BPy_reports_to_error(&reports, PyExc_IOError, true) == 0) {
174                         PyErr_SetString(PyExc_IOError, "Unknown error writing library data");
175                 }
176                 ret = NULL;
177         }
178
179
180 finally:
181
182         /* clear all flags for ID's added to the store (may run on error too) */
183         id_store = id_store_array;
184
185         for (int i = 0; i < id_store_len; id_store++, i++) {
186
187
188                 if (use_fake_user) {
189                         if ((id_store->id_flag & LIB_FAKEUSER) == 0) {
190                                 id_store->id->flag &= ~LIB_FAKEUSER;
191                         }
192                 }
193
194                 id_store->id->us = id_store->id_us;
195
196                 BKE_blendfile_write_partial_tag_ID(id_store->id, false);
197         }
198
199         MEM_freeN(id_store_array);
200
201         return ret;
202 }
203
204
205 int BPY_library_write_module(PyObject *mod_par)
206 {
207         static PyMethodDef write_meth = {
208                 "write", (PyCFunction)bpy_lib_write,
209                 METH_STATIC | METH_VARARGS | METH_KEYWORDS,
210                 bpy_lib_write_doc,
211         };
212
213         PyModule_AddObject(mod_par, "_library_write", PyCFunction_New(&write_meth, NULL));
214
215         return 0;
216 }