9#include <pxr/base/tf/pathUtils.h>
10#include <pxr/usd/usdVol/openVDBAsset.h>
11#include <pxr/usd/usdVol/volume.h>
32 const Volume *volume =
static_cast<Volume *
>(context.object->data);
38 Volume *volume =
static_cast<Volume *
>(context.object->data);
48 auto vdb_file_path = resolve_vdb_file(volume);
49 if (!vdb_file_path.has_value()) {
52 "USD Export: failed to resolve .vdb file for object: %s",
58 if (
auto relative_vdb_file_path = construct_vdb_relative_file_path(*vdb_file_path)) {
59 vdb_file_path = relative_vdb_file_path;
64 "USD Export: couldn't construct relative file path for .vdb file, absolute path "
65 "will be used instead");
72 pxr::UsdVolVolume usd_volume = pxr::UsdVolVolume::Define(
stage, volume_path);
79 const pxr::SdfPath grid_path = volume_path.AppendPath(pxr::SdfPath(grid_id));
80 pxr::UsdVolOpenVDBAsset usd_grid = pxr::UsdVolOpenVDBAsset::Define(
stage, grid_path);
81 usd_grid.GetFieldNameAttr().Set(pxr::TfToken(grid_name), timecode);
82 usd_grid.GetFilePathAttr().Set(pxr::SdfAssetPath(*vdb_file_path), timecode);
83 usd_volume.CreateFieldRelationship(pxr::TfToken(grid_id), grid_path);
87 const pxr::VtArray<pxr::GfVec3f> volume_extent = {pxr::GfVec3f(&bounds->min.x),
88 pxr::GfVec3f(&bounds->max.x)};
89 usd_volume.GetExtentAttr().Set(volume_extent, timecode);
95std::optional<std::string> USDVolumeWriter::resolve_vdb_file(
const Volume *volume)
const
97 std::optional<std::string> vdb_file_path;
98 if (volume->filepath[0] ==
'\0') {
103 vdb_file_path = construct_vdb_file_path(volume);
111 if (!vdb_file_path.has_value()) {
113 if (vdb_file_path->empty()) {
118 return vdb_file_path;
121std::optional<std::string> USDVolumeWriter::construct_vdb_file_path(
const Volume *volume)
const
124 if (usd_file_path.empty()) {
132 sizeof(usd_directory_path),
134 sizeof(usd_file_name));
136 if (usd_directory_path[0] ==
'\0' || usd_file_name[0] ==
'\0') {
140 const char *vdb_directory_name =
"volumes";
143 STRNCPY(vdb_directory_path, usd_directory_path);
144 BLI_strncat(vdb_directory_path, vdb_directory_name,
sizeof(vdb_directory_path));
148 STRNCPY(vdb_file_name, volume->id.name + 2);
150 if (!timecode.IsDefault()) {
151 const int frame =
int(timecode.GetValue());
153 BLI_path_frame(vdb_file_name,
sizeof(vdb_file_name), frame, num_frame_digits);
155 BLI_strncat(vdb_file_name,
".vdb",
sizeof(vdb_file_name));
158 BLI_path_join(vdb_file_path,
sizeof(vdb_file_path), vdb_directory_path, vdb_file_name);
160 return vdb_file_path;
163std::optional<std::string> USDVolumeWriter::construct_vdb_relative_file_path(
164 const std::string &vdb_file_path)
const
167 if (usd_file_path.empty()) {
172 STRNCPY(relative_path, vdb_file_path.c_str());
183 std::string relative_path_processed = pxr::TfNormPath(relative_path + 2);
184 if (relative_path_processed[0] !=
'.') {
185 relative_path_processed.insert(0,
"./");
188 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)
#define STRNCPY(dst, src)
char char size_t char * BLI_strncat(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
ReportList * reports() const
pxr::UsdTimeCode get_export_time_code() const
std::string get_export_file_path() const
const USDExporterContext usd_export_context_
virtual bool check_is_animated(const HierarchyContext &context) const override
virtual void do_write(HierarchyContext &context) override
USDVolumeWriter(const USDExporterContext &ctx)
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
std::string get_name(const VolumeGridData &grid)
std::string make_safe_name(const std::string &name, bool allow_unicode)
const USDExportParams & export_params
const pxr::SdfPath usd_path
const pxr::UsdStageRefPtr stage
ccl_device_inline int abs(int x)