Blender V5.3
usd_writer_volume.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
7#include "usd_utils.hh"
8
9#include <pxr/base/tf/pathUtils.h>
10#include <pxr/base/vt/value.h>
11#include <pxr/usd/usdVol/openVDBAsset.h>
12#include <pxr/usd/usdVol/volume.h>
13
14#include "DNA_scene_types.h"
15#include "DNA_volume_types.h"
16
17#include "BKE_report.hh"
18#include "BKE_volume.hh"
19
20#include "BLI_fileops.hh"
21#include "BLI_index_range.hh"
22#include "BLI_math_base_c.hh"
23#include "BLI_path_utils.hh"
24#include "BLI_string.hh"
25
27
28namespace blender::io::usd {
29
30static bool has_varying_modifiers(const Object *ob)
31{
32 /* These modifiers may vary the Volume either over time or by deformation/transformation. */
33 ModifierData *md = ob->modifiers.first();
34 while (md) {
35 if (ELEM(md->type,
39 {
40 return true;
41 }
42 md = md->next;
43 }
44
45 return false;
46}
47
49
51{
52 const Volume *volume = id_cast<Volume *>(context.object->data);
53 return volume->is_sequence || has_varying_modifiers(context.object);
54}
55
57{
58 Volume *volume = id_cast<Volume *>(context.object->data);
60 return;
61 }
62
63 const int num_grids = BKE_volume_num_grids(volume);
64 if (!num_grids) {
65 return;
66 }
67
68 const bool has_modifiers = has_varying_modifiers(context.object);
69 auto vdb_file_path = resolve_vdb_file(volume, has_modifiers);
70 if (!vdb_file_path.has_value()) {
73 "USD Export: failed to resolve .vdb file for object: %s",
74 volume->id.name + 2);
75 return;
76 }
77
79 if (auto relative_vdb_file_path = construct_vdb_relative_file_path(*vdb_file_path)) {
80 vdb_file_path = relative_vdb_file_path;
81 }
82 else {
85 "USD Export: couldn't construct relative file path for .vdb file, absolute path "
86 "will be used instead");
87 }
88 }
89
90 const pxr::UsdTimeCode time = get_export_time_code();
91 const pxr::SdfPath &volume_path = usd_export_context_.usd_path;
92 pxr::UsdStageRefPtr stage = usd_export_context_.stage;
93 pxr::UsdVolVolume usd_volume = pxr::UsdVolVolume::Define(stage, volume_path);
94
95 for (const int i : IndexRange(num_grids)) {
96 const bke::VolumeGridData *grid = BKE_volume_grid_get(volume, i);
97 const std::string grid_name = bke::volume_grid::get_name(*grid);
98 const std::string grid_id = make_safe_name(grid_name,
100 const pxr::SdfPath grid_path = volume_path.AppendPath(pxr::SdfPath(grid_id));
101 pxr::UsdVolOpenVDBAsset usd_grid = pxr::UsdVolOpenVDBAsset::Define(stage, grid_path);
102
103 pxr::TfToken grid_name_token = pxr::TfToken(grid_name);
104 pxr::SdfAssetPath asset_path = pxr::SdfAssetPath(*vdb_file_path);
105 pxr::UsdAttribute attr_field = usd_grid.CreateFieldNameAttr(pxr::VtValue(), true);
106 pxr::UsdAttribute attr_file = usd_grid.CreateFilePathAttr(pxr::VtValue(), true);
107 if (!attr_field.HasValue()) {
108 attr_field.Set(grid_name_token, pxr::UsdTimeCode::Default());
109 }
110 if (!attr_file.HasValue()) {
111 attr_file.Set(asset_path, pxr::UsdTimeCode::Default());
112 }
113
114 usd_value_writer_.SetAttribute(attr_field, grid_name_token, time);
115 usd_value_writer_.SetAttribute(attr_file, asset_path, time);
116
117 usd_volume.CreateFieldRelationship(pxr::TfToken(grid_id), grid_path);
118 }
119
120 this->author_extent(usd_volume, BKE_volume_min_max(volume), time);
121
122 BKE_volume_unload(volume);
123}
124
125std::optional<std::string> USDVolumeWriter::resolve_vdb_file(const Volume *volume,
126 bool has_modifiers) const
127{
128 std::optional<std::string> vdb_file_path;
129
130 const bool needs_vdb_save = volume->filepath[0] == '\0' || has_modifiers;
131 if (needs_vdb_save) {
132 /* Entering this section means that the Volume object contains OpenVDB data that is not
133 * obtained solely from external `.vdb` files but is generated or modified inside of Blender.
134 * Write this data as a new `.vdb` files. */
135
136 vdb_file_path = construct_vdb_file_path(volume);
137 if (!BKE_volume_save(
138 volume, usd_export_context_.bmain, nullptr, vdb_file_path.value_or("").c_str()))
139 {
140 return std::nullopt;
141 }
142 }
143
144 if (!vdb_file_path.has_value()) {
145 vdb_file_path = BKE_volume_grids_frame_filepath(volume);
146 if (vdb_file_path->empty()) {
147 return std::nullopt;
148 }
149 }
150
151 return vdb_file_path;
152}
153
154std::optional<std::string> USDVolumeWriter::construct_vdb_file_path(const Volume *volume) const
155{
156 const std::string usd_file_path = get_export_file_path();
157 if (usd_file_path.empty()) {
158 return std::nullopt;
159 }
160
161 char usd_directory_path[FILE_MAX];
162 char usd_file_name[FILE_MAXFILE];
163 BLI_path_split_dir_file(usd_file_path.c_str(),
164 usd_directory_path,
165 sizeof(usd_directory_path),
166 usd_file_name,
167 sizeof(usd_file_name));
168
169 if (usd_directory_path[0] == '\0' || usd_file_name[0] == '\0') {
170 return std::nullopt;
171 }
172
173 const char *vdb_directory_name = "volumes";
174
175 char vdb_directory_path[FILE_MAX];
176 STRNCPY(vdb_directory_path, usd_directory_path);
177 BLI_strncat(vdb_directory_path, vdb_directory_name, sizeof(vdb_directory_path));
178 BLI_dir_create_recursive(vdb_directory_path);
179
181 const int max_frame_digits = std::max(2, integer_digits_i(abs(scene->r.efra)));
182
183 char vdb_file_name[FILE_MAXFILE];
184 STRNCPY(vdb_file_name, volume->id.name + 2);
185 const pxr::UsdTimeCode time = get_export_time_code();
186 if (!time.IsDefault()) {
187 const int frame = int(time.GetValue());
188 BLI_path_frame(vdb_file_name, sizeof(vdb_file_name), frame, max_frame_digits);
189 }
190 BLI_strncat(vdb_file_name, ".vdb", sizeof(vdb_file_name));
191
192 char vdb_file_path[FILE_MAX];
193 BLI_path_join(vdb_file_path, sizeof(vdb_file_path), vdb_directory_path, vdb_file_name);
194
195 return vdb_file_path;
196}
197
198std::optional<std::string> USDVolumeWriter::construct_vdb_relative_file_path(
199 const std::string &vdb_file_path) const
200{
201 const std::string usd_file_path = get_export_file_path();
202 if (usd_file_path.empty()) {
203 return std::nullopt;
204 }
205
206 char relative_path[FILE_MAX];
207 STRNCPY(relative_path, vdb_file_path.c_str());
208 BLI_path_rel(relative_path, usd_file_path.c_str());
209 if (!BLI_path_is_rel(relative_path)) {
210 return std::nullopt;
211 }
212
213 /* Following code was written with an assumption that Blender's relative paths start with
214 * `//` characters as well as have OS dependent slashes. Inside of USD files those relative
215 * paths should start with either `./` or `../` characters and have always forward slashes (`/`)
216 * separating directories. This is the convention used in USD documentation (and it seems
217 * to be used in other DCC packages as well). */
218 std::string relative_path_processed = pxr::TfNormPath(relative_path + 2);
219 if (relative_path_processed[0] != '.') {
220 relative_path_processed.insert(0, "./");
221 }
222
223 return relative_path_processed;
224}
225
226} // namespace blender::io::usd
Volume data-block.
File and directory operations.
#define FILE_MAXFILE
#define FILE_MAX
#define BLI_path_join(...)
#define ELEM(...)
constexpr IndexRange()=default
void author_extent(const pxr::UsdGeomBoundable &boundable, const pxr::UsdTimeCode time)
pxr::UsdTimeCode get_export_time_code() const
pxr::UsdUtilsSparseValueWriter usd_value_writer_
USDAbstractWriter(const USDExporterContext &usd_export_context)
const USDExporterContext usd_export_context_
bool check_is_animated(const HierarchyContext &context) const override
void do_write(HierarchyContext &context) override
USDVolumeWriter(const USDExporterContext &ctx)
#define abs
std::string get_name(const VolumeGridData &grid)
std::string time(const std::tm &date_time, TimeFormat format=TimeFormat::H24)
static bool has_varying_modifiers(const Object *ob)
std::string make_safe_name(const StringRef name, bool allow_unicode)
Definition usd_utils.cc:83
char * STRNCPY(char(&dst)[N], const char *src)
@ RPT_WARNING
Definition BKE_report.hh:40
std::optional< Bounds< float3 > > BKE_volume_min_max(const Volume *volume)
int BKE_volume_num_grids(const Volume *volume)
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
Definition fileops_c.cc:416
MINLINE int integer_digits_i(int i)
@ eModifierType_VolumeDisplace
@ eModifierType_MeshToVolume
const bke::VolumeGridData * BKE_volume_grid_get(const Volume *volume, int grid_index)
bool BLI_path_frame(char *path, size_t path_maxncpy, int frame, int digits) ATTR_NONNULL(1)
const char * BKE_volume_grids_frame_filepath(const Volume *volume)
void BKE_volume_unload(Volume *volume)
bool void BLI_path_rel(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1)
bool BKE_volume_save(const Volume *volume, const Main *bmain, ReportList *reports, const char *filepath)
bool BKE_volume_load(const Volume *volume, const Main *bmain)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
Scene * DEG_get_input_scene(const Depsgraph *graph)
void BLI_path_split_dir_file(const char *filepath, char *dir, size_t dir_maxncpy, char *file, size_t file_maxncpy) ATTR_NONNULL(1
char char size_t char * BLI_strncat(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
ustring name
Definition graph/node.h:178
char name[258]
Definition DNA_ID.h:433
struct ModifierData * next
ListBaseT< ModifierData > modifiers