Skip to content

Commit

Permalink
Add workaround to co-exist default value and timeSamples when reading
Browse files Browse the repository at this point in the history
Attribute from USDC.
  • Loading branch information
syoyo committed Apr 17, 2024
1 parent eeac474 commit bdedcc0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
11 changes: 11 additions & 0 deletions src/prim-reconstruct.cc
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ static ParseResult ParseTypedAttribute(std::set<std::string> &table, /* inout */
// e.g. "float radius = None"
target.set_blocked(true);
} else if (attr.variability() == Variability::Uniform) {
DCOUT("Property is uniform: " << name);
// e.g. "float radius = 1.2"
if (!attr.get_var().is_scalar()) {
ret.code = ParseResult::ResultCode::VariabilityMismatch;
Expand All @@ -373,6 +374,7 @@ static ParseResult ParseTypedAttribute(std::set<std::string> &table, /* inout */
}

} else if (attr.get_var().is_timesamples()) {
DCOUT("Property is timesamples: " << name);
// e.g. "float radius.timeSamples = {0: 1.2, 1: 2.3}"

Animatable<T> anim;
Expand All @@ -387,6 +389,7 @@ static ParseResult ParseTypedAttribute(std::set<std::string> &table, /* inout */
return ret;
}
} else if (attr.get_var().is_scalar()) {
DCOUT("Property is scalar: " << name);
if (auto pv = attr.get_value<T>()) {
target.set_value(pv.value());
} else {
Expand Down Expand Up @@ -624,9 +627,11 @@ static ParseResult ParseTypedAttribute(std::set<std::string> &table, /* inout */
DCOUT("Adding typed attribute: " << name);

if (attr.is_blocked()) {
DCOUT("Attribute is blocked: " << name);
// e.g. "uniform float radius = None"
target.set_blocked(true);
} else if (attr.variability() == Variability::Uniform) {
DCOUT("Attribute is uniform: " << name);
// e.g. "uniform float radius = 1.2"
if (!attr.get_var().is_scalar()) {
ret.code = ParseResult::ResultCode::VariabilityMismatch;
Expand All @@ -643,6 +648,7 @@ static ParseResult ParseTypedAttribute(std::set<std::string> &table, /* inout */
}

} else if (attr.get_var().is_timesamples()) {
DCOUT("Attribute is timesamples: " << name);
// e.g. "float radius.timeSamples = {0: 1.2, 1: 2.3}"

Animatable<T> anim;
Expand All @@ -657,6 +663,7 @@ static ParseResult ParseTypedAttribute(std::set<std::string> &table, /* inout */
return ret;
}
} else if (attr.get_var().is_scalar()) {
DCOUT("Attribute is scalar: " << name);
if (auto pv = attr.get_var().get_value<T>()) {
target.set_value(pv.value());
} else {
Expand Down Expand Up @@ -1532,9 +1539,13 @@ nonstd::expected<T, std::string> EnumHandler(

} // namespace

// Work around until https://github.com/syoyo/tinyusdz/issues/154
// clear table to allow the latter attribute can overwrite previous definition.
#define PARSE_TYPED_ATTRIBUTE(__table, __prop, __name, __klass, __target) { \
ParseResult ret = ParseTypedAttribute(__table, __prop.first, __prop.second, __name, __target); \
if (ret.code == ParseResult::ResultCode::Success || ret.code == ParseResult::ResultCode::AlreadyProcessed) { \
/* FIXME: workaround. clear table */ \
__table.erase(__name); \
continue; /* got it */\
} else if (ret.code == ParseResult::ResultCode::Unmatched) { \
/* go next */ \
Expand Down
2 changes: 1 addition & 1 deletion src/primvar.hh
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ struct PrimVar {

template <class T>
void set_value(const T &v) {
_ts.clear();
//_ts.clear(); // timeSamples and (defaut) value should co-exist.
_value = v;
}

Expand Down
25 changes: 19 additions & 6 deletions src/usdc-reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,8 @@ bool USDCReader::Impl::ParseProperty(const SpecType spec_type,
Property::Type propType{Property::Type::EmptyAttrib};
Attribute attr;

bool is_scalar{false};
bool has_default{false};
bool has_timesamples{false};

value::Value scalar;
Relationship rel;
Expand Down Expand Up @@ -932,7 +933,7 @@ bool USDCReader::Impl::ParseProperty(const SpecType spec_type,
// Set scalar
// TODO: Easier CrateValue to Attribute.var conversion
scalar = fv.second.get_raw();
is_scalar = true;
has_default = true;

// TODO: Handle UnregisteredValue in crate-reader.cc
// UnregisteredValue is represented as string.
Expand All @@ -956,6 +957,7 @@ bool USDCReader::Impl::ParseProperty(const SpecType spec_type,
primvar::PrimVar var;
var.set_timesamples(pv.value());
attr.set_var(std::move(var));
has_timesamples = true;
} else {
PUSH_ERROR_AND_RETURN_TAG(kTag,
"`timeSamples` is not TimeSamples data.");
Expand Down Expand Up @@ -1233,7 +1235,7 @@ bool USDCReader::Impl::ParseProperty(const SpecType spec_type,
(void)hasConnectionPaths;
#endif

if (is_scalar) {
if (has_default) {
if (typeName) {
if (scalar.type_id() == value::TypeTraits<value::ValueBlock>::type_id()) {
// nothing to do
Expand Down Expand Up @@ -1261,9 +1263,20 @@ bool USDCReader::Impl::ParseProperty(const SpecType spec_type,
}
}
}
primvar::PrimVar var;
var.set_value(scalar);
attr.set_var(std::move(var));

if (has_timesamples) {
DCOUT("add scalar");
// overwrite
primvar::PrimVar var = attr.get_var();
var.set_value(scalar);
DCOUT("var.is_timesamples = " << var.is_timesamples());
attr.set_var(std::move(var));

} else {
primvar::PrimVar var;
var.set_value(scalar);
attr.set_var(std::move(var));
}

if (scalar.type_id() == value::TypeTraits<value::ValueBlock>::type_id()) {
if (typeName) {
Expand Down

0 comments on commit bdedcc0

Please sign in to comment.