Blender  V2.93
usd_capi.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2019 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 #include "usd.h"
21 #include "usd_hierarchy_iterator.h"
22 
23 #include <pxr/base/plug/registry.h>
24 #include <pxr/pxr.h>
25 #include <pxr/usd/usd/stage.h>
26 #include <pxr/usd/usdGeom/tokens.h>
27 
28 #include "MEM_guardedalloc.h"
29 
30 #include "DEG_depsgraph.h"
31 #include "DEG_depsgraph_build.h"
32 #include "DEG_depsgraph_query.h"
33 
34 #include "DNA_scene_types.h"
35 
36 #include "BKE_appdir.h"
37 #include "BKE_blender_version.h"
38 #include "BKE_context.h"
39 #include "BKE_global.h"
40 #include "BKE_scene.h"
41 
42 #include "BLI_fileops.h"
43 #include "BLI_path_util.h"
44 #include "BLI_string.h"
45 
46 #include "WM_api.h"
47 #include "WM_types.h"
48 
49 namespace blender::io::usd {
50 
51 struct ExportJobData {
55 
58 
59  bool export_ok;
60 };
61 
63 {
64  static bool plugin_path_registered = false;
65  if (plugin_path_registered) {
66  return;
67  }
68  plugin_path_registered = true;
69 
70  /* Tell USD which directory to search for its JSON files. If 'datafiles/usd'
71  * does not exist, the USD library will not be able to read or write any files. */
72  const std::string blender_usd_datafiles = BKE_appdir_folder_id(BLENDER_DATAFILES, "usd");
73  /* The trailing slash indicates to the USD library that the path is a directory. */
74  pxr::PlugRegistry::GetInstance().RegisterPlugins(blender_usd_datafiles + "/");
75 }
76 
77 static void export_startjob(void *customdata,
78  /* Cannot be const, this function implements wm_jobs_start_callback.
79  * NOLINTNEXTLINE: readability-non-const-parameter. */
80  short *stop,
81  short *do_update,
82  float *progress)
83 {
84  ExportJobData *data = static_cast<ExportJobData *>(customdata);
85  data->export_ok = false;
86 
87  G.is_rendering = true;
88  WM_set_locked_interface(data->wm, true);
89  G.is_break = false;
90 
91  /* Construct the depsgraph for exporting. */
92  Scene *scene = DEG_get_input_scene(data->depsgraph);
93  if (data->params.visible_objects_only) {
95  }
96  else {
98  }
99  BKE_scene_graph_update_tagged(data->depsgraph, data->bmain);
100 
101  *progress = 0.0f;
102  *do_update = true;
103 
104  /* For restoring the current frame after exporting animation is done. */
105  const int orig_frame = CFRA;
106 
107  pxr::UsdStageRefPtr usd_stage = pxr::UsdStage::CreateNew(data->filename);
108  if (!usd_stage) {
109  /* This happens when the USD JSON files cannot be found. When that happens,
110  * the USD library doesn't know it has the functionality to write USDA and
111  * USDC files, and creating a new UsdStage fails. */
112  WM_reportf(
113  RPT_ERROR, "USD Export: unable to find suitable USD plugin to write %s", data->filename);
114  return;
115  }
116 
117  usd_stage->SetMetadata(pxr::UsdGeomTokens->upAxis, pxr::VtValue(pxr::UsdGeomTokens->z));
118  usd_stage->SetMetadata(pxr::UsdGeomTokens->metersPerUnit,
119  pxr::VtValue(scene->unit.scale_length));
120  usd_stage->GetRootLayer()->SetDocumentation(std::string("Blender v") +
122 
123  /* Set up the stage for animated data. */
124  if (data->params.export_animation) {
125  usd_stage->SetTimeCodesPerSecond(FPS);
126  usd_stage->SetStartTimeCode(scene->r.sfra);
127  usd_stage->SetEndTimeCode(scene->r.efra);
128  }
129 
130  USDHierarchyIterator iter(data->depsgraph, usd_stage, data->params);
131 
132  if (data->params.export_animation) {
133  /* Writing the animated frames is not 100% of the work, but it's our best guess. */
134  float progress_per_frame = 1.0f / std::max(1, (scene->r.efra - scene->r.sfra + 1));
135 
136  for (float frame = scene->r.sfra; frame <= scene->r.efra; frame++) {
137  if (G.is_break || (stop != nullptr && *stop)) {
138  break;
139  }
140 
141  /* Update the scene for the next frame to render. */
142  scene->r.cfra = static_cast<int>(frame);
143  scene->r.subframe = frame - scene->r.cfra;
145 
146  iter.set_export_frame(frame);
147  iter.iterate_and_write();
148 
149  *progress += progress_per_frame;
150  *do_update = true;
151  }
152  }
153  else {
154  /* If we're not animating, a single iteration over all objects is enough. */
155  iter.iterate_and_write();
156  }
157 
158  iter.release_writers();
159  usd_stage->GetRootLayer()->Save();
160 
161  /* Finish up by going back to the keyframe that was current before we started. */
162  if (CFRA != orig_frame) {
163  CFRA = orig_frame;
165  }
166 
167  data->export_ok = true;
168  *progress = 1.0f;
169  *do_update = true;
170 }
171 
172 static void export_endjob(void *customdata)
173 {
174  ExportJobData *data = static_cast<ExportJobData *>(customdata);
175 
176  DEG_graph_free(data->depsgraph);
177 
178  if (!data->export_ok && BLI_exists(data->filename)) {
179  BLI_delete(data->filename, false, false);
180  }
181 
182  G.is_rendering = false;
183  WM_set_locked_interface(data->wm, false);
184 }
185 
186 } // namespace blender::io::usd
187 
189  const char *filepath,
190  const USDExportParams *params,
191  bool as_background_job)
192 {
193  ViewLayer *view_layer = CTX_data_view_layer(C);
195 
197 
199  MEM_mallocN(sizeof(blender::io::usd::ExportJobData), "ExportJobData"));
200 
201  job->bmain = CTX_data_main(C);
202  job->wm = CTX_wm_manager(C);
203  job->export_ok = false;
204  BLI_strncpy(job->filename, filepath, sizeof(job->filename));
205 
206  job->depsgraph = DEG_graph_new(job->bmain, scene, view_layer, params->evaluation_mode);
207  job->params = *params;
208 
209  bool export_ok = false;
210  if (as_background_job) {
211  wmJob *wm_job = WM_jobs_get(
212  job->wm, CTX_wm_window(C), scene, "USD Export", WM_JOB_PROGRESS, WM_JOB_TYPE_ALEMBIC);
213 
214  /* setup job */
215  WM_jobs_customdata_set(wm_job, job, MEM_freeN);
216  WM_jobs_timer(wm_job, 0.1, NC_SCENE | ND_FRAME, NC_SCENE | ND_FRAME);
217  WM_jobs_callbacks(wm_job,
219  nullptr,
220  nullptr,
222 
223  WM_jobs_start(CTX_wm_manager(C), wm_job);
224  }
225  else {
226  /* Fake a job context, so that we don't need NULL pointer checks while exporting. */
227  short stop = 0, do_update = 0;
228  float progress = 0.0f;
229 
230  blender::io::usd::export_startjob(job, &stop, &do_update, &progress);
232  export_ok = job->export_ok;
233 
234  MEM_freeN(job);
235  }
236 
237  return export_ok;
238 }
239 
241 {
242  /* USD 19.11 defines:
243  *
244  * #define PXR_MAJOR_VERSION 0
245  * #define PXR_MINOR_VERSION 19
246  * #define PXR_PATCH_VERSION 11
247  * #define PXR_VERSION 1911
248  *
249  * So the major version is implicit/invisible in the public version number.
250  */
251  return PXR_VERSION;
252 }
@ BLENDER_DATAFILES
Definition: BKE_appdir.h:78
const char * BKE_appdir_folder_id(const int folder_id, const char *subfolder)
Definition: appdir.c:674
const char * BKE_blender_version_string(void)
Definition: blender.c:142
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1034
struct wmWindowManager * CTX_wm_manager(const bContext *C)
Definition: context.c:689
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1044
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1018
struct wmWindow * CTX_wm_window(const bContext *C)
Definition: context.c:699
void BKE_scene_graph_update_tagged(struct Depsgraph *depsgraph, struct Main *bmain)
Definition: scene.c:2713
void BKE_scene_graph_update_for_newframe(struct Depsgraph *depsgraph)
Definition: scene.c:2794
File and directory operations.
int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: storage.c:349
int BLI_delete(const char *file, bool dir, bool recursive) ATTR_NONNULL()
Definition: fileops.c:1037
#define FILE_MAX
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
Depsgraph * DEG_graph_new(struct Main *bmain, struct Scene *scene, struct ViewLayer *view_layer, eEvaluationMode mode)
Definition: depsgraph.cc:281
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
void DEG_graph_free(Depsgraph *graph)
Definition: depsgraph.cc:314
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 CFRA
#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:39
@ WM_JOB_TYPE_ALEMBIC
Definition: WM_api.h:751
@ WM_JOB_PROGRESS
Definition: WM_api.h:726
#define NC_SCENE
Definition: WM_types.h:279
#define ND_FRAME
Definition: WM_types.h:334
Scene scene
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
static void export_startjob(void *customdata, short *stop, short *do_update, float *progress)
Definition: usd_capi.cc:77
static void ensure_usd_plugin_path_registered()
Definition: usd_capi.cc:62
static void export_endjob(void *customdata)
Definition: usd_capi.cc:172
Definition: BKE_main.h:116
struct RenderData r
struct UnitSettings unit
Definition: wm_jobs.c:73
float max
bool USD_export(bContext *C, const char *filepath, const USDExportParams *params, bool as_background_job)
Definition: usd_capi.cc:188
int USD_get_version(void)
Definition: usd_capi.cc:240
#define G(x, y, z)
void WM_reportf(ReportType 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:450
wmJob * WM_jobs_get(wmWindowManager *wm, wmWindow *win, void *owner, const char *name, int flag, int job_type)
Definition: wm_jobs.c:196
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:372
void WM_jobs_customdata_set(wmJob *wm_job, void *customdata, void(*free)(void *))
Definition: wm_jobs.c:344
void WM_jobs_timer(wmJob *wm_job, double timestep, unsigned int note, unsigned int endnote)
Definition: wm_jobs.c:360