9#include <pxr/base/gf/vec3f.h>
10#include <pxr/base/tf/pathUtils.h>
11#include <pxr/base/vt/array.h>
12#include <pxr/base/vt/value.h>
13#include <pxr/usd/usdVol/openVDBAsset.h>
14#include <pxr/usd/usdVol/volume.h>
55 const Volume *volume =
static_cast<Volume *
>(context.object->data);
61 Volume *volume =
static_cast<Volume *
>(context.object->data);
72 auto vdb_file_path = resolve_vdb_file(volume, has_modifiers);
73 if (!vdb_file_path.has_value()) {
76 "USD Export: failed to resolve .vdb file for object: %s",
82 if (
auto relative_vdb_file_path = construct_vdb_relative_file_path(*vdb_file_path)) {
83 vdb_file_path = relative_vdb_file_path;
88 "USD Export: couldn't construct relative file path for .vdb file, absolute path "
89 "will be used instead");
96 pxr::UsdVolVolume usd_volume = pxr::UsdVolVolume::Define(stage, volume_path);
103 const pxr::SdfPath grid_path = volume_path.AppendPath(pxr::SdfPath(grid_id));
104 pxr::UsdVolOpenVDBAsset usd_grid = pxr::UsdVolOpenVDBAsset::Define(stage, grid_path);
106 pxr::TfToken grid_name_token = pxr::TfToken(grid_name);
107 pxr::SdfAssetPath asset_path = pxr::SdfAssetPath(*vdb_file_path);
108 pxr::UsdAttribute attr_field = usd_grid.CreateFieldNameAttr(pxr::VtValue(),
true);
109 pxr::UsdAttribute attr_file = usd_grid.CreateFilePathAttr(pxr::VtValue(),
true);
110 if (!attr_field.HasValue()) {
111 attr_field.Set(grid_name_token, pxr::UsdTimeCode::Default());
113 if (!attr_file.HasValue()) {
114 attr_file.Set(asset_path, pxr::UsdTimeCode::Default());
120 usd_volume.CreateFieldRelationship(pxr::TfToken(grid_id), grid_path);
128std::optional<std::string> USDVolumeWriter::resolve_vdb_file(
const Volume *volume,
129 bool has_modifiers)
const
131 std::optional<std::string> vdb_file_path;
133 const bool needs_vdb_save = volume->
filepath[0] ==
'\0' || has_modifiers;
134 if (needs_vdb_save) {
139 vdb_file_path = construct_vdb_file_path(volume);
147 if (!vdb_file_path.has_value()) {
149 if (vdb_file_path->empty()) {
154 return vdb_file_path;
157std::optional<std::string> USDVolumeWriter::construct_vdb_file_path(
const Volume *volume)
const
160 if (usd_file_path.empty()) {
168 sizeof(usd_directory_path),
170 sizeof(usd_file_name));
172 if (usd_directory_path[0] ==
'\0' || usd_file_name[0] ==
'\0') {
176 const char *vdb_directory_name =
"volumes";
179 STRNCPY(vdb_directory_path, usd_directory_path);
180 BLI_strncat(vdb_directory_path, vdb_directory_name,
sizeof(vdb_directory_path));
189 if (!time.IsDefault()) {
190 const int frame = int(time.GetValue());
191 BLI_path_frame(vdb_file_name,
sizeof(vdb_file_name), frame, max_frame_digits);
193 BLI_strncat(vdb_file_name,
".vdb",
sizeof(vdb_file_name));
196 BLI_path_join(vdb_file_path,
sizeof(vdb_file_path), vdb_directory_path, vdb_file_name);
198 return vdb_file_path;
201std::optional<std::string> USDVolumeWriter::construct_vdb_relative_file_path(
202 const std::string &vdb_file_path)
const
205 if (usd_file_path.empty()) {
210 STRNCPY(relative_path, vdb_file_path.c_str());
221 std::string relative_path_processed = pxr::TfNormPath(relative_path + 2);
222 if (relative_path_processed[0] !=
'.') {
223 relative_path_processed.insert(0,
"./");
226 return relative_path_processed;
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
int BKE_volume_num_grids(const Volume *volume)
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)
std::optional< blender::Bounds< blender::float3 > > BKE_volume_min_max(const Volume *volume)
const char * BKE_volume_grids_frame_filepath(const Volume *volume)
const blender::bke::VolumeGridData * BKE_volume_grid_get(const Volume *volume, int grid_index)
void BKE_volume_unload(Volume *volume)
File and directory operations.
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
MINLINE int integer_digits_i(int i)
#define BLI_path_join(...)
void BLI_path_split_dir_file(const char *filepath, char *dir, size_t dir_maxncpy, char *file, size_t file_maxncpy) ATTR_NONNULL(1
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool void BLI_path_rel(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1)
bool BLI_path_frame(char *path, size_t path_maxncpy, int frame, int digits) ATTR_NONNULL(1)
char char size_t char * BLI_strncat(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
char * STRNCPY(char(&dst)[N], const char *src)
Scene * DEG_get_input_scene(const Depsgraph *graph)
@ eModifierType_MeshToVolume
@ eModifierType_VolumeDisplace
void author_extent(const pxr::UsdGeomBoundable &boundable, const pxr::UsdTimeCode time)
ReportList * reports() const
pxr::UsdTimeCode get_export_time_code() const
pxr::UsdUtilsSparseValueWriter usd_value_writer_
USDAbstractWriter(const USDExporterContext &usd_export_context)
std::string get_export_file_path() const
const USDExporterContext usd_export_context_
bool check_is_animated(const HierarchyContext &context) const override
void do_write(HierarchyContext &context) override
USDVolumeWriter(const USDExporterContext &ctx)
std::string get_name(const VolumeGridData &grid)
static bool has_varying_modifiers(const Object *ob)
std::string make_safe_name(const StringRef name, bool allow_unicode)
struct ModifierData * next