Blender V5.3
usd_scene_index.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
5#include "usd_scene_index.hh"
6
7#include <pxr/usdImaging/usdImaging/sceneIndices.h>
8
9#include "BLI_fileops.hh"
10#include "BLI_path_utils.hh"
11#include "BLI_string.hh"
12
13#include "BKE_appdir.hh"
14#include "BKE_report.hh"
15
17
18#include "WM_api.hh"
19#include "WM_types.hh"
20
21#include "usd.hh"
22#include "usd_private.hh"
23
24namespace blender {
25
26using namespace blender::io::usd;
27
28namespace io::hydra {
29
30USDSceneIndex::USDSceneIndex(pxr::HdRenderIndex *render_index,
31 pxr::SdfPath const &delegate_id,
32 const bool use_materialx)
33 : render_index_(render_index), delegate_id_(delegate_id), use_materialx(use_materialx)
34{
35 /* Temporary directory to write any additional files to, like image or VDB files. */
36 char unique_name[FILE_MAXFILE];
37 SNPRINTF(unique_name, "%p", this);
38
39 char dir_path[FILE_MAX];
40 BLI_path_join(dir_path, sizeof(dir_path), BKE_tempdir_session(), "usd_scene_index", unique_name);
42
43 char file_path[FILE_MAX];
44 BLI_path_join(file_path, sizeof(file_path), dir_path, "scene.usdc");
45
46 temp_dir_ = dir_path;
47 temp_file_ = file_path;
48}
49
51{
52 if (final_scene_index_) {
53 render_index_->RemoveSceneIndex(final_scene_index_);
54 }
55 BLI_delete(temp_dir_.c_str(), true, true);
56}
57
58void USDSceneIndex::populate(Depsgraph *depsgraph)
59{
60 USDExportParams params;
61 params.use_instancing = true;
62 params.relative_paths = false; /* Unnecessary. */
63 params.export_textures = false; /* Don't copy all textures, is slow. */
65 params.evaluation_mode = DEG_get_mode(depsgraph);
66 params.generate_preview_surface = !use_materialx;
67 params.generate_materialx_network = use_materialx;
68
69 /* NOTE: Since the reports list will be `nullptr` here, reports generated by export code from
70 * this call will only be printed to console. */
71 wmJobWorkerStatus worker_status = {};
72 ReportList worker_reports = {};
73 BKE_reports_init(&worker_reports, RPT_PRINT | RPT_STORE);
74 worker_status.reports = &worker_reports;
75 params.worker_status = &worker_status;
76
77 /* Create clean directory for export. */
78 BLI_delete(temp_dir_.c_str(), true, true);
79 BLI_dir_create_recursive(temp_dir_.c_str());
80
81 /* Free previous delegate and stage first to save memory. */
82 if (final_scene_index_) {
83 render_index_->RemoveSceneIndex(final_scene_index_);
84 final_scene_index_.Reset();
85 stage_scene_index_.Reset();
86 }
87 stage_.Reset();
88
89 /* Convert depsgraph to stage + additional file in temp directory. */
90 stage_ = io::usd::export_to_stage(params, depsgraph, temp_file_.c_str());
91
92 /* Build imaging scene index chain and insert into the render index. */
93 pxr::UsdImagingCreateSceneIndicesInfo info;
94 info.stage = stage_;
95 pxr::UsdImagingSceneIndices indices = pxr::UsdImagingCreateSceneIndices(info);
96 stage_scene_index_ = indices.stageSceneIndex;
97 final_scene_index_ = indices.finalSceneIndex;
98 render_index_->InsertSceneIndex(final_scene_index_, delegate_id_);
99
100 WM_reports_from_reports_move(nullptr, &worker_reports);
101
102 BKE_reports_free(&worker_reports);
103}
104
105} // namespace io::hydra
106} // namespace blender
File and directory operations.
#define FILE_MAXFILE
#define FILE_MAX
#define BLI_path_join(...)
#define SNPRINTF(dst, format,...)
USDSceneIndex(pxr::HdRenderIndex *render_index, pxr::SdfPath const &delegate_id, bool use_materialx)
void populate(Depsgraph *depsgraph)
pxr::UsdStageRefPtr export_to_stage(const USDExportParams &params, Depsgraph *depsgraph, const char *filepath)
BMesh Iterators.
void BKE_reports_free(ReportList *reports)
Definition report.cc:106
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
Definition fileops_c.cc:416
void BKE_reports_init(ReportList *reports, int flag)
Definition report.cc:91
const char * BKE_tempdir_session() ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
Definition appdir.cc:1283
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
eEvaluationMode DEG_get_mode(const Depsgraph *graph)
void WM_reports_from_reports_move(wmWindowManager *wm, ReportList *reports)
enum eEvaluationMode evaluation_mode
Definition usd.hh:149
SubdivExportMode export_subdiv
Definition usd.hh:148
wmJobWorkerStatus * worker_status
Definition usd.hh:184