PyAPI: API for selectively writing data-blocks
[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_blender.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 "   :arg filepath: The path to write the blend-file.\n"
58 "   :type filepath: string\n"
59 "   :arg datablocks: data-blocks (:class:`bpy.types.ID` instances).\n"
60 "   :type datablocks: sequence\n"
61 "   :arg relative_remap: When Truem, remap the paths relative to the current blend-file.\n"
62 "   :type relative_remap: bool\n"
63 "   :arg fake_user: When True, data-blocks will be written with fake-user flag enabled.\n"
64 "   :type fake_user: bool\n"
65 );
66 static PyObject *bpy_lib_write(PyObject *UNUSED(self), PyObject *args, PyObject *kwds)
67 {
68         static const char *kwlist[] = {
69                 "filepath", "datablocks",
70                 /* optional */
71                 "relative_remap", "fake_user",
72                 NULL,
73         };
74
75         /* args */
76         const char *filepath;
77         char filepath_abs[FILE_MAX];
78         PyObject *datablocks = NULL;
79         bool use_relative_remap = false, use_fake_user = false;
80
81         if (!PyArg_ParseTupleAndKeywords(
82                 args, kwds,
83                 "sO!|$O&O&:write", (char **)kwlist,
84                 &filepath,
85                 &PySet_Type, &datablocks,
86                 PyC_ParseBool, &use_relative_remap,
87                 PyC_ParseBool, &use_fake_user))
88         {
89                 return NULL;
90         }
91
92         Main *bmain_src = G.main;
93         int write_flags = 0;
94
95         if (use_relative_remap) {
96                 write_flags |= G_FILE_RELATIVE_REMAP;
97         }
98
99         BLI_strncpy(filepath_abs, filepath, FILE_MAX);
100         BLI_path_abs(filepath_abs, G.main->name);
101
102         BKE_blendfile_write_partial_begin(bmain_src);
103
104         /* array of ID's and backup any data we modify */
105         struct {
106                 ID *id;
107                 /* original values */
108                 short id_flag;
109                 short id_us;
110         } *id_store_array, *id_store;
111         int id_store_len = 0;
112
113         PyObject *ret;
114
115         /* collect all id data from the set and store in 'id_store_array' */
116         {
117                 Py_ssize_t pos, hash;
118                 PyObject *key;
119
120                 id_store_array = MEM_mallocN(sizeof(*id_store_array) * PySet_Size(datablocks), __func__);
121                 id_store = id_store_array;
122
123                 pos = hash = 0;
124                 while (_PySet_NextEntry(datablocks, &pos, &key, &hash)) {
125
126                         if (!pyrna_id_FromPyObject(key, &id_store->id)) {
127                                 PyErr_Format(PyExc_TypeError,
128                                              "Expected and ID type, not %.200s",
129                                              Py_TYPE(key)->tp_name);
130                                 ret = NULL;
131                                 goto finally;
132                         }
133                         else {
134                                 id_store->id_flag = id_store->id->flag;
135                                 id_store->id_us = id_store->id->us;
136
137                                 if (use_fake_user) {
138                                         id_store->id->flag |= LIB_FAKEUSER;
139                                 }
140                                 id_store->id->us = 1;
141
142                                 BKE_blendfile_write_partial_tag_ID(id_store->id, true);
143
144                                 id_store_len += 1;
145                                 id_store++;
146                         }
147                 }
148         }
149
150         /* write blend */
151         int retval = 0;
152         ReportList reports;
153
154         BKE_reports_init(&reports, RPT_STORE);
155
156         retval = BKE_blendfile_write_partial(bmain_src, filepath_abs, write_flags, &reports);
157
158         /* cleanup state */
159         BKE_blendfile_write_partial_end(bmain_src);
160
161         if (retval) {
162                 BKE_reports_print(&reports, RPT_ERROR_ALL);
163                 BKE_reports_clear(&reports);
164                 ret = Py_None;
165                 Py_INCREF(ret);
166         }
167         else {
168                 if (BPy_reports_to_error(&reports, PyExc_RuntimeError, true) == 0) {
169                         PyErr_SetString(PyExc_RuntimeError, "Unknown error writing library data");
170                 }
171                 ret = NULL;
172         }
173
174
175 finally:
176
177         /* clear all flags for ID's added to the store (may run on error too) */
178         id_store = id_store_array;
179
180         for (int i = 0; i < id_store_len; id_store++, i++) {
181
182
183                 if (use_fake_user) {
184                         if ((id_store->id_flag & LIB_FAKEUSER) == 0) {
185                                 id_store->id->flag &= ~LIB_FAKEUSER;
186                         }
187                 }
188
189                 id_store->id->us = id_store->id_us;
190
191                 BKE_blendfile_write_partial_tag_ID(id_store->id, false);
192         }
193
194         MEM_freeN(id_store_array);
195
196         return ret;
197 }
198
199
200 int BPY_library_write_module(PyObject *mod_par)
201 {
202         static PyMethodDef write_meth = {
203                 "write", (PyCFunction)bpy_lib_write,
204                 METH_STATIC | METH_VARARGS | METH_KEYWORDS,
205                 bpy_lib_write_doc,
206         };
207
208         PyModule_AddObject(mod_par, "_library_write", PyCFunction_New(&write_meth, NULL));
209
210         return 0;
211 }