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