Blender V5.3
abc_export_capi.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "ABC_alembic.h"
10#include "IO_subdiv_disabler.hh"
11#include "abc_archive.h"
13
14#include "MEM_guardedalloc.h"
15
16#include "DEG_depsgraph.hh"
19
20#include "DNA_scene_types.h"
21
22#include "BKE_context.hh"
23#include "BKE_global.hh"
24#include "BKE_lib_id.hh"
25#include "BKE_main.hh"
26#include "BKE_scene.hh"
27
28#include "BLI_fileops.hh"
29#include "BLI_path_utils.hh"
30#include "BLI_string.hh"
31#include "BLI_timeit.hh"
32
33#include "ED_util.hh"
34
35#include "WM_api.hh"
36#include "WM_types.hh"
37
38#include "CLG_log.h"
39
40namespace blender {
41
42static CLG_LogRef LOG = {"io.alembic"};
43
44#include <memory>
45
47 Main *bmain = nullptr;
48 Depsgraph *depsgraph = nullptr;
49 wmWindowManager *wm = nullptr;
50
51 char filepath[FILE_MAX] = {};
53
54 bool was_canceled = false;
55 bool export_ok = false;
57};
58
59namespace io::alembic {
60
61/* Construct the depsgraph for exporting. */
63{
64 if (job->params.collection[0]) {
65 Collection *collection = reinterpret_cast<Collection *>(
67 if (!collection) {
69 RPT_ERROR, "Alembic Export: Unable to find collection '%s'", job->params.collection);
70 return false;
71 }
72
74 }
75 else {
77 }
78
79 return true;
80}
81
83{
84 timeit::Nanoseconds duration = timeit::Clock::now() - data->start_time;
85 std::cout << "Alembic export of '" << data->filepath << "' took ";
86 timeit::print_duration(duration);
87 std::cout << '\n';
88}
89
90static void export_startjob(void *customdata, wmJobWorkerStatus *worker_status)
91{
92 ExportJobData *data = static_cast<ExportJobData *>(customdata);
93 data->was_canceled = false;
94 data->start_time = timeit::Clock::now();
95
96 G.is_rendering = true;
98 G.is_break = false;
99
100 worker_status->progress = 0.0f;
101 worker_status->do_update = true;
102
103 BKE_scene_graph_update_tagged(data->depsgraph, data->bmain);
104
105 SubdivModifierDisabler subdiv_disabler(data->depsgraph);
106 if (!data->params.apply_subdiv) {
107 subdiv_disabler.disable_modifiers();
108 BKE_scene_graph_update_tagged(data->depsgraph, data->bmain);
109 }
110
111 /* For restoring the current frame after exporting animation is done. */
112 Scene *scene = DEG_get_input_scene(data->depsgraph);
113 const int orig_frame = scene->r.cfra;
114 const bool export_animation = (data->params.frame_start != data->params.frame_end);
115
116 /* Create the Alembic archive. */
117 std::unique_ptr<ABCArchive> abc_archive;
118 try {
119 abc_archive = std::make_unique<ABCArchive>(
120 data->bmain, scene, data->params, std::string(data->filepath));
121 }
122 catch (const std::exception &ex) {
123 std::stringstream error_message_stream;
124 error_message_stream << "Error writing to " << data->filepath;
125 const std::string &error_message = error_message_stream.str();
126
127 /* The exception message can be very cryptic (just "iostream error" on Linux, for example),
128 * so better not to include it in the report. */
129 CLOG_ERROR(&LOG, "%s: %s", error_message.c_str(), ex.what());
130 WM_global_report(RPT_ERROR, error_message.c_str());
131 data->export_ok = false;
132 return;
133 }
134 catch (...) {
135 /* Unknown exception class, so we cannot include its message. */
136 std::stringstream error_message_stream;
137 error_message_stream << "Unknown error writing to " << data->filepath;
138 WM_global_report(RPT_ERROR, error_message_stream.str().c_str());
139 data->export_ok = false;
140 return;
141 }
142
143 ABCHierarchyIterator iter(data->bmain, data->depsgraph, abc_archive.get(), data->params);
144
145 if (export_animation) {
146 CLOG_STR_DEBUG(&LOG, "Exporting animation");
147
148 /* Writing the animated frames is not 100% of the work, but it's our best guess. */
149 const float progress_per_frame = 1.0f / std::max(size_t(1), abc_archive->total_frame_count());
150 ABCArchive::Frames::const_iterator frame_it = abc_archive->frames_begin();
151 const ABCArchive::Frames::const_iterator frames_end = abc_archive->frames_end();
152
153 for (; frame_it != frames_end; frame_it++) {
154 double frame = *frame_it;
155
156 if (G.is_break || worker_status->stop) {
157 break;
158 }
159
160 /* Update the scene for the next frame to render. */
161 scene->r.cfra = int(frame);
162 scene->r.subframe = float(frame - scene->r.cfra);
164
165 CLOG_DEBUG(&LOG, "Exporting frame %.2f", frame);
166 ExportSubset export_subset = abc_archive->export_subset_for_frame(frame);
167 iter.set_export_subset(export_subset);
168 iter.iterate_and_write();
169
170 worker_status->progress += progress_per_frame;
171 worker_status->do_update = true;
172 }
173 }
174 else {
175 /* If we're not animating, a single iteration over all objects is enough. */
176 iter.iterate_and_write();
177 }
178
179 iter.release_writers();
180
181 /* Finish up by going back to the keyframe that was current before we started. */
182 if (scene->r.cfra != orig_frame) {
183 scene->r.cfra = orig_frame;
185 }
186
187 data->export_ok = !data->was_canceled;
188
189 worker_status->progress = 1.0f;
190 worker_status->do_update = true;
191}
192
193static void export_endjob(void *customdata)
194{
195 ExportJobData *data = static_cast<ExportJobData *>(customdata);
196
197 DEG_graph_free(data->depsgraph);
198
199 if (data->was_canceled && BLI_exists(data->filepath)) {
200 BLI_delete(data->filepath, false, false);
201 }
202
203 G.is_rendering = false;
204 WM_locked_interface_set(data->wm, false);
206}
207
208} // namespace io::alembic
209
210bool ABC_export(Scene *scene,
211 bContext *C,
212 const char *filepath,
213 const AlembicExportParams *params,
214 bool as_background_job)
215{
216 ViewLayer *view_layer = CTX_data_view_layer(C);
217
218 ExportJobData *job = MEM_new<ExportJobData>("ExportJobData");
219
220 job->bmain = CTX_data_main(C);
221 job->wm = CTX_wm_manager(C);
222 job->export_ok = false;
223 STRNCPY(job->filepath, filepath);
224
226
227 job->depsgraph = DEG_graph_new(job->bmain, scene, view_layer, params->evaluation_mode);
228 job->params = *params;
229
230 /* Construct the depsgraph for exporting.
231 *
232 * Has to be done from main thread currently, as it may affect Main original data (e.g. when
233 * doing deferred update of the view-layers, see #112534 for details). */
235 return false;
236 }
237
238 bool export_ok = false;
239 if (as_background_job) {
240 wmJob *wm_job = WM_jobs_get(job->wm,
242 scene,
243 "Exporting Alembic...",
246
247 /* setup job */
249 wm_job, job, [](void *j) { MEM_delete(static_cast<ExportJobData *>(j)); });
253
255 }
256 else {
257 wmJobWorkerStatus worker_status = {};
258 io::alembic::export_startjob(job, &worker_status);
260 export_ok = job->export_ok;
261
262 MEM_delete(job);
263 }
264
265 return export_ok;
266}
267
268} // namespace blender
File and directory operations.
#define FILE_MAX
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define CLOG_STR_DEBUG(clg_ref, str)
Definition CLG_log.h:199
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NC_SCENE
Definition WM_types.hh:391
#define ND_FRAME
Definition WM_types.hh:453
void set_export_subset(ExportSubset export_subset)
static void export_endjob(void *customdata)
static void export_startjob(void *customdata, wmJobWorkerStatus *worker_status)
static bool build_depsgraph(ExportJobData *job)
static void report_job_duration(const ExportJobData *data)
std::chrono::nanoseconds Nanoseconds
Definition BLI_timeit.hh:21
Clock::time_point TimePoint
Definition BLI_timeit.hh:20
void print_duration(Nanoseconds duration)
Definition timeit.cc:45
BMesh Iterators.
void WM_global_reportf(eReportType type, const char *format,...)
void BKE_scene_graph_update_for_newframe(Depsgraph *depsgraph)
Definition scene.cc:2953
char * STRNCPY(char(&dst)[N], const char *src)
ID * BKE_libblock_find_name(Main *bmain, short type, const char *name, const std::optional< Library * > lib=std::nullopt) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition lib_id.cc:1808
bool ED_editors_flush_edits(Main *bmain)
Definition ed_util.cc:335
void DEG_graph_build_from_view_layer(Depsgraph *graph)
void DEG_graph_build_from_collection(Depsgraph *graph, Collection *collection)
void WM_jobs_callbacks(wmJob *wm_job, wm_jobs_start_callback startjob, void(*initjob)(void *), void(*update)(void *), void(*endjob)(void *))
Definition wm_jobs.cc:407
void DEG_graph_free(Depsgraph *graph)
Definition depsgraph.cc:307
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
@ WM_JOB_TYPE_ALEMBIC_EXPORT
Definition WM_api.hh:2037
Depsgraph * DEG_graph_new(Main *bmain, Scene *scene, ViewLayer *view_layer, eEvaluationMode mode)
Definition depsgraph.cc:279
wmWindow * CTX_wm_window(const bContext *C)
void WM_jobs_customdata_set(wmJob *wm_job, void *customdata, void(*free)(void *customdata))
Definition wm_jobs.cc:377
@ WM_JOB_PROGRESS
Definition WM_api.hh:1997
ViewLayer * CTX_data_view_layer(const bContext *C)
void WM_jobs_start(wmWindowManager *wm, wmJob *wm_job)
Definition wm_jobs.cc:498
Global G
Definition blender.cc:57
void WM_locked_interface_set(wmWindowManager *wm, bool lock)
BMesh const char void * data
wmWindowManager * CTX_wm_manager(const bContext *C)
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
void BKE_scene_graph_update_tagged(Depsgraph *depsgraph, Main *bmain)
Definition scene.cc:2867
void WM_global_report(eReportType type, const char *message)
Main * CTX_data_main(const bContext *C)
wmJob * WM_jobs_get(wmWindowManager *wm, wmWindow *win, const void *owner, const char *name, const eWM_JobFlag flag, const eWM_JobType job_type)
Definition wm_jobs.cc:228
Scene * DEG_get_input_scene(const Depsgraph *graph)
bool ABC_export(struct Scene *scene, struct bContext *C, const char *filepath, const struct AlembicExportParams *params, bool as_background_job)
static CLG_LogRef LOG
Definition logging.cpp:12
void WM_jobs_timer(wmJob *wm_job, double time_step, uint note, uint endnote, void(*timer_step)(void *))
Definition wm_jobs.cc:393
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
char collection[MAX_ID_NAME - 2]
Definition ABC_alembic.h:64
enum eEvaluationMode evaluation_mode
Definition ABC_alembic.h:55
AlembicExportParams params
timeit::TimePoint start_time
struct RenderData r