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>
52 const Volume *volume = id_cast<Volume *>(context.object->data);
58 Volume *volume = id_cast<Volume *>(context.object->data);
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",
79 if (
auto relative_vdb_file_path = construct_vdb_relative_file_path(*vdb_file_path)) {
80 vdb_file_path = relative_vdb_file_path;
85 "USD Export: couldn't construct relative file path for .vdb file, absolute path "
86 "will be used instead");
93 pxr::UsdVolVolume usd_volume = pxr::UsdVolVolume::Define(stage, volume_path);
100 const pxr::SdfPath grid_path = volume_path.AppendPath(pxr::SdfPath(grid_id));
101 pxr::UsdVolOpenVDBAsset usd_grid = pxr::UsdVolOpenVDBAsset::Define(stage, grid_path);
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());
110 if (!attr_file.HasValue()) {
111 attr_file.Set(asset_path, pxr::UsdTimeCode::Default());
117 usd_volume.CreateFieldRelationship(pxr::TfToken(grid_id), grid_path);
125std::optional<std::string> USDVolumeWriter::resolve_vdb_file(
const Volume *volume,
126 bool has_modifiers)
const
128 std::optional<std::string> vdb_file_path;
130 const bool needs_vdb_save = volume->
filepath[0] ==
'\0' || has_modifiers;
131 if (needs_vdb_save) {
136 vdb_file_path = construct_vdb_file_path(volume);
144 if (!vdb_file_path.has_value()) {
146 if (vdb_file_path->empty()) {
151 return vdb_file_path;
154std::optional<std::string> USDVolumeWriter::construct_vdb_file_path(
const Volume *volume)
const
157 if (usd_file_path.empty()) {
165 sizeof(usd_directory_path),
167 sizeof(usd_file_name));
169 if (usd_directory_path[0] ==
'\0' || usd_file_name[0] ==
'\0') {
173 const char *vdb_directory_name =
"volumes";
176 STRNCPY(vdb_directory_path, usd_directory_path);
177 BLI_strncat(vdb_directory_path, vdb_directory_name,
sizeof(vdb_directory_path));
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);
190 BLI_strncat(vdb_file_name,
".vdb",
sizeof(vdb_file_name));
193 BLI_path_join(vdb_file_path,
sizeof(vdb_file_path), vdb_directory_path, vdb_file_name);
195 return vdb_file_path;
198std::optional<std::string> USDVolumeWriter::construct_vdb_relative_file_path(
199 const std::string &vdb_file_path)
const
202 if (usd_file_path.empty()) {
207 STRNCPY(relative_path, vdb_file_path.c_str());
218 std::string relative_path_processed = pxr::TfNormPath(relative_path + 2);
219 if (relative_path_processed[0] !=
'.') {
220 relative_path_processed.insert(0,
"./");
223 return relative_path_processed;
File and directory operations.
#define BLI_path_join(...)
constexpr IndexRange()=default
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)
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)
char * STRNCPY(char(&dst)[N], const char *src)
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()
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
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
struct ModifierData * next
ListBaseT< ModifierData > modifiers
const USDExportParams & export_params
const pxr::SdfPath usd_path
const pxr::UsdStageRefPtr stage