Fix: USD export: missing animation frames.

This fixes a bug where only the time sample for the first frame
is written to a USD animation.

Replaced the hard-coded USDExporterContext::time_code value with a
USDExporterContext::get_time_code function wrapper which is called
from USDAbstractWriter::get_export_time_code() to query the current
frame when writing an animation.

Pull Request: https://projects.blender.org/blender/blender/pulls/111248
This commit is contained in:
Michael Kowalski
2023-08-24 14:12:07 +02:00
committed by Michael Kowalski
parent 577c0b4b46
commit 5bbeb927a1
4 changed files with 15 additions and 4 deletions

View File

@@ -49,6 +49,7 @@ void MaterialData::init()
/* Create temporary in memory stage. */
pxr::UsdStageRefPtr stage = pxr::UsdStage::CreateInMemory();
pxr::UsdTimeCode time = pxr::UsdTimeCode::Default();
auto get_time_code = [time]() { return time; };
pxr::SdfPath material_library_path("/_materials");
pxr::SdfPath material_path = material_library_path.AppendChild(
pxr::TfToken(prim_id.GetElementString()));
@@ -63,7 +64,7 @@ void MaterialData::init()
scene_delegate_->depsgraph,
stage,
material_library_path,
time,
get_time_code,
export_params,
image_cache_file_path()};

View File

@@ -8,6 +8,8 @@
#include <pxr/usd/sdf/path.h>
#include <pxr/usd/usd/common.h>
#include <functional>
struct Depsgraph;
struct Main;
@@ -20,7 +22,13 @@ struct USDExporterContext {
Depsgraph *depsgraph;
const pxr::UsdStageRefPtr stage;
const pxr::SdfPath usd_path;
pxr::UsdTimeCode time_code;
/**
* Wrap a function which returns the current time code
* for export. This is necessary since the context
* may be used for exporting an animation over a sequece
* of frames.
*/
std::function<pxr::UsdTimeCode()> get_time_code;
const USDExportParams &export_params;
std::string export_file_path;
};

View File

@@ -78,9 +78,10 @@ USDExporterContext USDHierarchyIterator::create_usd_export_context(const Hierarc
* `pxr::UsdStage::CreateNew` function). */
const pxr::SdfLayerHandle root_layer = stage_->GetRootLayer();
const std::string export_file_path = root_layer->GetRealPath();
auto get_time_code = [this]() { return this->export_time_; };
return USDExporterContext{
bmain_, depsgraph_, stage_, path, export_time_, params_, export_file_path};
bmain_, depsgraph_, stage_, path, get_time_code, params_, export_file_path};
}
AbstractHierarchyWriter *USDHierarchyIterator::create_transform_writer(

View File

@@ -59,7 +59,8 @@ std::string USDAbstractWriter::get_export_file_path() const
pxr::UsdTimeCode USDAbstractWriter::get_export_time_code() const
{
if (is_animated_) {
return usd_export_context_.time_code;
BLI_assert(usd_export_context_.get_time_code);
return usd_export_context_.get_time_code();
}
/* By using the default timecode USD won't even write a single `timeSample` for non-animated
* data. Instead, it writes it as non-timesampled. */