Blender V4.3
usd_scene_delegate.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7#include "BLI_fileops.h"
8#include "BLI_path_utils.hh"
9#include "BLI_string.h"
10
11#include "BKE_appdir.hh"
12#include "BKE_report.hh"
13
15
16#include "WM_api.hh"
17#include "WM_types.hh"
18
19#include "usd.hh"
20#include "usd_private.hh"
21
22using namespace blender::io::usd;
23
24namespace blender::io::hydra {
25
26USDSceneDelegate::USDSceneDelegate(pxr::HdRenderIndex *render_index,
27 pxr::SdfPath const &delegate_id,
28 const bool use_materialx)
29 : render_index_(render_index), delegate_id_(delegate_id), use_materialx(use_materialx)
30{
31 /* Temporary directory to write any additional files to, like image or VDB files. */
33 SNPRINTF(unique_name, "%p", this);
34
35 char dir_path[FILE_MAX];
37 dir_path, sizeof(dir_path), BKE_tempdir_session(), "usd_scene_delegate", unique_name);
39
40 char file_path[FILE_MAX];
41 BLI_path_join(file_path, sizeof(file_path), dir_path, "scene.usdc");
42
43 temp_dir_ = dir_path;
44 temp_file_ = file_path;
45}
46
48{
49 BLI_delete(temp_dir_.c_str(), true, true);
50}
51
53{
55 params.use_instancing = true;
56 params.relative_paths = false; /* Unnecessary. */
57 params.export_textures = false; /* Don't copy all textures, is slow. */
58 params.export_subdiv = USD_SUBDIV_TESSELLATE;
59 params.evaluation_mode = DEG_get_mode(depsgraph);
60 params.generate_preview_surface = !use_materialx;
61 params.generate_materialx_network = use_materialx;
62
63 /* NOTE: Since the reports list will be `nullptr` here, reports generated by export code from
64 * this call will only be printed to console. */
65 wmJobWorkerStatus worker_status = {};
66 ReportList worker_reports = {};
67 BKE_reports_init(&worker_reports, RPT_PRINT | RPT_STORE);
68 worker_status.reports = &worker_reports;
69 params.worker_status = &worker_status;
70
71 /* Create clean directory for export. */
72 BLI_delete(temp_dir_.c_str(), true, true);
73 BLI_dir_create_recursive(temp_dir_.c_str());
74
75 /* Free previous delegate and stage first to save memory. */
76 delegate_.reset();
77 stage_.Reset();
78
79 /* Convert depsgraph to stage + additional file in temp directory. */
80 stage_ = io::usd::export_to_stage(params, depsgraph, temp_file_.c_str());
81 delegate_ = std::make_unique<pxr::UsdImagingDelegate>(render_index_, delegate_id_);
82 delegate_->Populate(stage_->GetPseudoRoot());
83
84 WM_reports_from_reports_move(nullptr, &worker_reports);
85
86 BKE_reports_free(&worker_reports);
87}
88
89} // namespace blender::io::hydra
void BKE_reports_free(ReportList *reports)
Definition report.cc:69
void BKE_reports_init(ReportList *reports, int flag)
Definition report.cc:54
File and directory operations.
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
Definition fileops_c.cc:391
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
#define FILE_MAXFILE
#define FILE_MAX
#define BLI_path_join(...)
#define SNPRINTF(dst, format,...)
Definition BLI_string.h:597
eEvaluationMode DEG_get_mode(const Depsgraph *graph)
USDSceneDelegate(pxr::HdRenderIndex *render_index, pxr::SdfPath const &delegate_id, bool use_materialx)
const Depsgraph * depsgraph
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
@ USD_SUBDIV_TESSELLATE
Definition usd.hh:83
pxr::UsdStageRefPtr export_to_stage(const USDExportParams &params, Depsgraph *depsgraph, const char *filepath)
static void unique_name(bNode *node)
ReportList * reports
Definition WM_types.hh:985
void * BKE_tempdir_session
Definition stubs.c:38
void WM_reports_from_reports_move(wmWindowManager *wm, ReportList *reports)