Blender V4.5
bpy_library_write.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
12#include <Python.h>
13#include <cstddef>
14
15#include "BLI_path_utils.hh"
16#include "BLI_string.h"
17
18#include "BKE_blendfile.hh"
19#include "BKE_global.hh"
20#include "BKE_layer.hh"
21#include "BKE_lib_id.hh"
22#include "BKE_main.hh"
24#include "BKE_report.hh"
25
26#include "BLO_writefile.hh"
27
28#include "RNA_types.hh"
29
30#include "bpy_capi_utils.hh"
31#include "bpy_library.hh" /* Declaration for #BPY_library_load_method_def */
32#include "bpy_rna.hh"
33
35#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
36
37using namespace blender::bke::blendfile;
38
40 /* Wrap. */
41 bpy_lib_write_doc,
42 ".. method:: write(filepath, datablocks, path_remap=False, fake_user=False, compress=False)\n"
43 "\n"
44 " Write data-blocks into a blend file.\n"
45 "\n"
46 " .. note::\n"
47 "\n"
48 " Indirectly referenced data-blocks will be expanded and written too.\n"
49 "\n"
50 " :arg filepath: The path to write the blend-file.\n"
51 " :type filepath: str | bytes\n"
52 " :arg datablocks: set of data-blocks.\n"
53 " :type datablocks: set[:class:`bpy.types.ID`]\n"
54 " :arg path_remap: Optionally remap paths when writing the file:\n"
55 "\n"
56 " - ``NONE`` No path manipulation (default).\n"
57 " - ``RELATIVE`` Remap paths that are already relative to the new location.\n"
58 " - ``RELATIVE_ALL`` Remap all paths to be relative to the new location.\n"
59 " - ``ABSOLUTE`` Make all paths absolute on writing.\n"
60 "\n"
61 " :type path_remap: str\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");
66static PyObject *bpy_lib_write(BPy_PropertyRNA *self, PyObject *args, PyObject *kw)
67{
68 /* args */
69 PyC_UnicodeAsBytesAndSize_Data filepath_data = {nullptr};
70 char filepath_abs[FILE_MAX];
71 PyObject *datablocks = nullptr;
72
73 const PyC_StringEnumItems path_remap_items[] = {
76 {BLO_WRITE_PATH_REMAP_RELATIVE_ALL, "RELATIVE_ALL"},
78 {0, nullptr},
79 };
80 PyC_StringEnum path_remap = {path_remap_items, BLO_WRITE_PATH_REMAP_NONE};
81
82 bool use_fake_user = false, use_compress = false;
83
84 static const char *_keywords[] = {
85 "filepath",
86 "datablocks",
87 "path_remap",
88 "fake_user",
89 "compress",
90 nullptr,
91 };
92 static _PyArg_Parser _parser = {
94 "O&" /* `filepath` */
95 "O!" /* `datablocks` */
96 "|$" /* Optional keyword only arguments. */
97 "O&" /* `path_remap` */
98 "O&" /* `fake_user` */
99 "O&" /* `compress` */
100 ":write",
101 _keywords,
102 nullptr,
103 };
104 if (!_PyArg_ParseTupleAndKeywordsFast(args,
105 kw,
106 &_parser,
108 &filepath_data,
109 &PySet_Type,
110 &datablocks,
112 &path_remap,
114 &use_fake_user,
116 &use_compress))
117 {
118 return nullptr;
119 }
120
121 Main *bmain_src = static_cast<Main *>(self->ptr->data); /* Typically #G_MAIN */
122
123 /* Ensure invariants before writing, or writing process may fail. See e.g. #165588. */
125 BKE_main_ensure_invariants(*bmain_src);
126
127 int write_flags = 0;
128
129 if (use_compress) {
130 write_flags |= G_FILE_COMPRESS;
131 }
132
133 STRNCPY(filepath_abs, filepath_data.value);
134 Py_XDECREF(filepath_data.value_coerce);
135
137
138 PartialWriteContext partial_write_ctx{bmain_src->filepath};
139 const PartialWriteContext::IDAddOptions add_options{
140 (PartialWriteContext::IDAddOperations::ADD_DEPENDENCIES |
142 use_fake_user ? PartialWriteContext::IDAddOperations::SET_FAKE_USER : 0))};
143
144 if (PySet_GET_SIZE(datablocks) > 0) {
145 PyObject *it = PyObject_GetIter(datablocks);
146 PyObject *key;
147 while ((key = PyIter_Next(it))) {
148 /* Borrow from the set. */
149 Py_DECREF(key);
150 ID *id;
151 if (!pyrna_id_FromPyObject(key, &id)) {
152 PyErr_Format(PyExc_TypeError, "Expected an ID type, not %.200s", Py_TYPE(key)->tp_name);
153 break;
154 }
155 partial_write_ctx.id_add(id, add_options, nullptr);
156 }
157 Py_DECREF(it);
158 if (key) {
159 return nullptr;
160 }
161 }
162
163 BLI_assert(partial_write_ctx.is_valid());
164
165 /* write blend */
166 ReportList reports;
167
168 BKE_reports_init(&reports, RPT_STORE);
169 bool success = partial_write_ctx.write(
170 filepath_abs, write_flags, path_remap.value_found, reports);
171
172 PyObject *py_return_value;
173 if (success) {
175 py_return_value = Py_None;
176 Py_INCREF(py_return_value);
177 }
178 else {
179 if (BPy_reports_to_error(&reports, PyExc_IOError, false) == 0) {
180 PyErr_SetString(PyExc_IOError, "Unknown error writing library data");
181 }
182 py_return_value = nullptr;
183 }
184
185 BKE_reports_free(&reports);
186
187 return py_return_value;
188}
189
190#ifdef __GNUC__
191# ifdef __clang__
192# pragma clang diagnostic push
193# pragma clang diagnostic ignored "-Wcast-function-type"
194# else
195# pragma GCC diagnostic push
196# pragma GCC diagnostic ignored "-Wcast-function-type"
197# endif
198#endif
199
201 "write",
202 (PyCFunction)bpy_lib_write,
203 METH_VARARGS | METH_KEYWORDS,
204 bpy_lib_write_doc,
205};
206
207#ifdef __GNUC__
208# ifdef __clang__
209# pragma clang diagnostic pop
210# else
211# pragma GCC diagnostic pop
212# endif
213#endif
@ G_FILE_COMPRESS
void BKE_main_view_layers_synced_ensure(const Main *bmain)
const char * BKE_main_blendfile_path_from_global()
Definition main.cc:882
void BKE_main_ensure_invariants(Main &bmain, std::optional< blender::Span< ID * > > modified_ids=std::nullopt)
void BKE_reports_free(ReportList *reports)
Definition report.cc:70
void BKE_reports_init(ReportList *reports, int flag)
Definition report.cc:55
void BKE_reports_print(ReportList *reports, eReportType level)
Definition report.cc:316
#define BLI_assert(a)
Definition BLI_assert.h:46
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
#define FILE_MAX
int key
char * STRNCPY(char(&dst)[N], const char *src)
Definition BLI_string.h:688
external writefile.cc function prototypes.
@ BLO_WRITE_PATH_REMAP_NONE
@ BLO_WRITE_PATH_REMAP_RELATIVE_ALL
@ BLO_WRITE_PATH_REMAP_ABSOLUTE
@ BLO_WRITE_PATH_REMAP_RELATIVE
#define RPT_ERROR_ALL
short BPy_reports_to_error(ReportList *reports, PyObject *exception, const bool clear)
PyObject * self
PyDoc_STRVAR(bpy_lib_write_doc, ".. method:: write(filepath, datablocks, path_remap=False, fake_user=False, compress=False)\n" "\n" " Write data-blocks into a blend file.\n" "\n" " .. note::\n" "\n" " Indirectly referenced data-blocks will be expanded and written too.\n" "\n" " :arg filepath: The path to write the blend-file.\n" " :type filepath: str | bytes\n" " :arg datablocks: set of data-blocks.\n" " :type datablocks: set[:class:`bpy.types.ID`]\n" " :arg path_remap: Optionally remap paths when writing the file:\n" "\n" " - ``NONE`` No path manipulation (default).\n" " - ``RELATIVE`` Remap paths that are already relative to the new location.\n" " - ``RELATIVE_ALL`` Remap all paths to be relative to the new location.\n" " - ``ABSOLUTE`` Make all paths absolute on writing.\n" "\n" " :type path_remap: str\n" " :arg fake_user: When True, data-blocks will be written with fake-user flag enabled.\n" " :type fake_user: bool\n" " :arg compress: When True, write a compressed blend file.\n" " :type compress: bool\n")
PyMethodDef BPY_library_write_method_def
static PyObject * bpy_lib_write(BPy_PropertyRNA *self, PyObject *args, PyObject *kw)
bool pyrna_id_FromPyObject(PyObject *obj, ID **id)
Definition bpy_rna.cc:8501
int PyC_ParseUnicodeAsBytesAndSize(PyObject *o, void *p)
int PyC_ParseStringEnum(PyObject *o, void *p)
int PyC_ParseBool(PyObject *o, void *p)
header-only compatibility defines.
#define PY_ARG_PARSER_HEAD_COMPAT()
Definition DNA_ID.h:404
char filepath[1024]
Definition BKE_main.hh:155