Blender  V3.3
usd_capi_export.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2019 Blender Foundation. All rights reserved. */
3 
4 #include "usd.h"
5 #include "usd_common.h"
7 
8 #include <pxr/base/plug/registry.h>
9 #include <pxr/pxr.h>
10 #include <pxr/usd/usd/stage.h>
11 #include <pxr/usd/usdGeom/tokens.h>
12 
13 #include "MEM_guardedalloc.h"
14 
15 #include "DEG_depsgraph.h"
16 #include "DEG_depsgraph_build.h"
17 #include "DEG_depsgraph_query.h"
18 
19 #include "DNA_scene_types.h"
20 
21 #include "BKE_appdir.h"
22 #include "BKE_blender_version.h"
23 #include "BKE_context.h"
24 #include "BKE_global.h"
25 #include "BKE_scene.h"
26 
27 #include "BLI_fileops.h"
28 #include "BLI_path_util.h"
29 #include "BLI_string.h"
30 #include "BLI_timeit.hh"
31 
32 #include "WM_api.h"
33 #include "WM_types.h"
34 
35 namespace blender::io::usd {
36 
37 struct ExportJobData {
41 
44 
45  bool export_ok;
47 };
48 
50 {
51  timeit::Nanoseconds duration = timeit::Clock::now() - data->start_time;
52  std::cout << "USD export of '" << data->filepath << "' took ";
53  timeit::print_duration(duration);
54  std::cout << '\n';
55 }
56 
57 static void export_startjob(void *customdata,
58  /* Cannot be const, this function implements wm_jobs_start_callback.
59  * NOLINTNEXTLINE: readability-non-const-parameter. */
60  short *stop,
61  short *do_update,
62  float *progress)
63 {
64  ExportJobData *data = static_cast<ExportJobData *>(customdata);
65  data->export_ok = false;
66  data->start_time = timeit::Clock::now();
67 
68  G.is_rendering = true;
69  WM_set_locked_interface(data->wm, true);
70  G.is_break = false;
71 
72  Scene *scene = DEG_get_input_scene(data->depsgraph);
73 
74  /* Evaluate the despgraph for exporting.
75  *
76  * Note that, unlike with its building, this is expected to be safe to perform from worker
77  * thread, since UI is locked during export, so there should not be any more changes in the Main
78  * original data concurrently done from the main thread at this point. All necessary (deferred)
79  * changes are expected to have been triggered and processed during depsgraph building in
80  * #USD_export. */
81  BKE_scene_graph_update_tagged(data->depsgraph, data->bmain);
82 
83  *progress = 0.0f;
84  *do_update = true;
85 
86  /* For restoring the current frame after exporting animation is done. */
87  const int orig_frame = scene->r.cfra;
88 
89  pxr::UsdStageRefPtr usd_stage = pxr::UsdStage::CreateNew(data->filepath);
90  if (!usd_stage) {
91  /* This happens when the USD JSON files cannot be found. When that happens,
92  * the USD library doesn't know it has the functionality to write USDA and
93  * USDC files, and creating a new UsdStage fails. */
94  WM_reportf(
95  RPT_ERROR, "USD Export: unable to find suitable USD plugin to write %s", data->filepath);
96  return;
97  }
98 
99  usd_stage->SetMetadata(pxr::UsdGeomTokens->upAxis, pxr::VtValue(pxr::UsdGeomTokens->z));
100  usd_stage->SetMetadata(pxr::UsdGeomTokens->metersPerUnit,
101  static_cast<double>(scene->unit.scale_length));
102  usd_stage->GetRootLayer()->SetDocumentation(std::string("Blender v") +
104 
105  /* Set up the stage for animated data. */
106  if (data->params.export_animation) {
107  usd_stage->SetTimeCodesPerSecond(FPS);
108  usd_stage->SetStartTimeCode(scene->r.sfra);
109  usd_stage->SetEndTimeCode(scene->r.efra);
110  }
111 
112  USDHierarchyIterator iter(data->bmain, data->depsgraph, usd_stage, data->params);
113 
114  if (data->params.export_animation) {
115  /* Writing the animated frames is not 100% of the work, but it's our best guess. */
116  float progress_per_frame = 1.0f / std::max(1, (scene->r.efra - scene->r.sfra + 1));
117 
118  for (float frame = scene->r.sfra; frame <= scene->r.efra; frame++) {
119  if (G.is_break || (stop != nullptr && *stop)) {
120  break;
121  }
122 
123  /* Update the scene for the next frame to render. */
124  scene->r.cfra = static_cast<int>(frame);
125  scene->r.subframe = frame - scene->r.cfra;
127 
128  iter.set_export_frame(frame);
129  iter.iterate_and_write();
130 
131  *progress += progress_per_frame;
132  *do_update = true;
133  }
134  }
135  else {
136  /* If we're not animating, a single iteration over all objects is enough. */
137  iter.iterate_and_write();
138  }
139 
140  iter.release_writers();
141  usd_stage->GetRootLayer()->Save();
142 
143  /* Finish up by going back to the keyframe that was current before we started. */
144  if (scene->r.cfra != orig_frame) {
145  scene->r.cfra = orig_frame;
147  }
148 
149  data->export_ok = true;
150  *progress = 1.0f;
151  *do_update = true;
152 }
153 
154 static void export_endjob(void *customdata)
155 {
156  ExportJobData *data = static_cast<ExportJobData *>(customdata);
157 
158  DEG_graph_free(data->depsgraph);
159 
160  if (!data->export_ok && BLI_exists(data->filepath)) {
161  BLI_delete(data->filepath, false, false);
162  }
163 
164  G.is_rendering = false;
165  WM_set_locked_interface(data->wm, false);
167 }
168 
169 } // namespace blender::io::usd
170 
172  const char *filepath,
173  const USDExportParams *params,
174  bool as_background_job)
175 {
176  ViewLayer *view_layer = CTX_data_view_layer(C);
178 
180 
182  MEM_mallocN(sizeof(blender::io::usd::ExportJobData), "ExportJobData"));
183 
184  job->bmain = CTX_data_main(C);
185  job->wm = CTX_wm_manager(C);
186  job->export_ok = false;
187  BLI_strncpy(job->filepath, filepath, sizeof(job->filepath));
188 
189  job->depsgraph = DEG_graph_new(job->bmain, scene, view_layer, params->evaluation_mode);
190  job->params = *params;
191 
192  /* Construct the depsgraph for exporting.
193  *
194  * Has to be done from main thread currently, as it may affect Main original data (e.g. when
195  * doing deferred update of the viewlayers, see #112534 for details). */
196  if (job->params.visible_objects_only) {
198  }
199  else {
201  }
202 
203  bool export_ok = false;
204  if (as_background_job) {
205  wmJob *wm_job = WM_jobs_get(
206  job->wm, CTX_wm_window(C), scene, "USD Export", WM_JOB_PROGRESS, WM_JOB_TYPE_ALEMBIC);
207 
208  /* setup job */
209  WM_jobs_customdata_set(wm_job, job, MEM_freeN);
210  WM_jobs_timer(wm_job, 0.1, NC_SCENE | ND_FRAME, NC_SCENE | ND_FRAME);
211  WM_jobs_callbacks(wm_job,
213  nullptr,
214  nullptr,
216 
217  WM_jobs_start(CTX_wm_manager(C), wm_job);
218  }
219  else {
220  /* Fake a job context, so that we don't need NULL pointer checks while exporting. */
221  short stop = 0, do_update = 0;
222  float progress = 0.0f;
223 
224  blender::io::usd::export_startjob(job, &stop, &do_update, &progress);
226  export_ok = job->export_ok;
227 
228  MEM_freeN(job);
229  }
230 
231  return export_ok;
232 }
233 
235 {
236  /* USD 19.11 defines:
237  *
238  * #define PXR_MAJOR_VERSION 0
239  * #define PXR_MINOR_VERSION 19
240  * #define PXR_PATCH_VERSION 11
241  * #define PXR_VERSION 1911
242  *
243  * So the major version is implicit/invisible in the public version number.
244  */
245  return PXR_VERSION;
246 }
const char * BKE_blender_version_string(void)
Definition: blender.c:124
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1090
struct wmWindowManager * CTX_wm_manager(const bContext *C)
Definition: context.c:713
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1100
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1074
struct wmWindow * CTX_wm_window(const bContext *C)
Definition: context.c:723
void BKE_scene_graph_update_tagged(struct Depsgraph *depsgraph, struct Main *bmain)
Definition: scene.cc:2648
void BKE_scene_graph_update_for_newframe(struct Depsgraph *depsgraph)
Definition: scene.cc:2728
File and directory operations.
int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: storage.c:314
int BLI_delete(const char *file, bool dir, bool recursive) ATTR_NONNULL()
Definition: fileops.c:934
#define FILE_MAX
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
Depsgraph * DEG_graph_new(struct Main *bmain, struct Scene *scene, struct ViewLayer *view_layer, eEvaluationMode mode)
Definition: depsgraph.cc:267
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:35
void DEG_graph_free(Depsgraph *graph)
Definition: depsgraph.cc:295
void DEG_graph_build_from_view_layer(struct Depsgraph *graph)
void DEG_graph_build_for_all_objects(struct Depsgraph *graph)
struct Scene * DEG_get_input_scene(const Depsgraph *graph)
#define FPS
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble z
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble GLdouble r _GL_VOID_RET _GL_VOID GLfloat GLfloat r _GL_VOID_RET _GL_VOID GLint GLint r _GL_VOID_RET _GL_VOID GLshort GLshort r _GL_VOID_RET _GL_VOID GLdouble GLdouble r
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:25
@ WM_JOB_PROGRESS
Definition: WM_api.h:1339
@ WM_JOB_TYPE_ALEMBIC
Definition: WM_api.h:1366
#define NC_SCENE
Definition: WM_types.h:328
#define ND_FRAME
Definition: WM_types.h:382
Scene scene
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:33
#define G(x, y, z)
static void export_startjob(void *customdata, short *stop, short *do_update, float *progress)
static void report_job_duration(const ExportJobData *data)
void ensure_usd_plugin_path_registered()
Definition: usd_common.cc:12
static void export_endjob(void *customdata)
Clock::time_point TimePoint
Definition: BLI_timeit.hh:14
void print_duration(Nanoseconds duration)
Definition: timeit.cc:10
std::chrono::nanoseconds Nanoseconds
Definition: BLI_timeit.hh:15
Definition: BKE_main.h:121
struct RenderData r
struct UnitSettings unit
bool visible_objects_only
Definition: usd.h:32
Definition: wm_jobs.c:57
float max
bool USD_export(bContext *C, const char *filepath, const USDExportParams *params, bool as_background_job)
int USD_get_version()
void WM_reportf(eReportType type, const char *format,...)
void WM_set_locked_interface(wmWindowManager *wm, bool lock)
void WM_jobs_start(wmWindowManager *wm, wmJob *wm_job)
Definition: wm_jobs.c:437
void WM_jobs_callbacks(wmJob *wm_job, wm_jobs_start_callback startjob, void(*initjob)(void *), void(*update)(void *), void(*endjob)(void *))
Definition: wm_jobs.c:351
void WM_jobs_customdata_set(wmJob *wm_job, void *customdata, void(*free)(void *))
Definition: wm_jobs.c:323
void WM_jobs_timer(wmJob *wm_job, double timestep, unsigned int note, unsigned int endnote)
Definition: wm_jobs.c:339
wmJob * WM_jobs_get(wmWindowManager *wm, wmWindow *win, const void *owner, const char *name, int flag, int job_type)
Definition: wm_jobs.c:184