Skip to content

Commit

Permalink
Support having multiple values in Attribute(W.i.p.)
Browse files Browse the repository at this point in the history
  • Loading branch information
syoyo committed Apr 17, 2024
1 parent a817933 commit 30ed6a2
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 44 deletions.
125 changes: 90 additions & 35 deletions src/pprinter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1190,25 +1190,102 @@ std::string print_prop(const Property &prop, const std::string &prop_name,
} else if (prop.is_attribute() || prop.is_connection()) {
const Attribute &attr = prop.get_attribute();

ss << pprint::Indent(indent);
//
// May print multiple times.
// e.g.
// float var = 1.0
// float var.timeSamples = ...
// float var.connect = <...>
//
// timeSamples and connect cannot have attrMeta
//

if (attr.has_value()) {

ss << pprint::Indent(indent);

if (prop.has_custom()) {
ss << "custom ";
}

if (attr.variability() == Variability::Uniform) {
ss << "uniform ";
} else if (attr.is_varying_authored()) {
// For Attribute, `varying` is the default variability and does not shown
// in USDA do nothing
}

std::string ty;

ty = attr.type_name();
ss << ty << " " << prop_name;

if (prop.is_empty()) {
// Nothing to do
} else {
// has value content

ss << " = ";

if (attr.is_blocked()) {
ss << "None";
} else {
// is_scalar
ss << value::pprint_value(attr.get_var().value_raw());
}
}

if (prop.has_custom()) {
ss << "custom ";
if (prop.get_attribute().metas().authored()) {
ss << " (\n"
<< print_attr_metas(prop.get_attribute().metas(), indent + 1)
<< pprint::Indent(indent) << ")";
}

ss << "\n";
}

if (attr.variability() == Variability::Uniform) {
ss << "uniform ";
} else if (attr.is_varying_authored()) {
// For Attribute, `varying` is the default variability and does not shown
// in USDA do nothing
if (attr.has_timesamples() && (attr.variability() != Variability::Uniform)) {

ss << pprint::Indent(indent);

if (prop.has_custom()) {
ss << "custom ";
}

std::string ty;

ty = attr.type_name();
ss << ty << " " << prop_name;

ss << ".timeSamples";

ss << " = ";

ss << print_timesamples(attr.get_var().ts_raw(), indent);

ss << "\n";
}

std::string ty;
if (attr.has_connection()) {

ty = attr.type_name();
ss << ty << " " << prop_name;
ss << pprint::Indent(indent);

if (prop.has_custom()) {
ss << "custom ";
}

if (attr.variability() == Variability::Uniform) {
ss << "uniform ";
} else if (attr.is_varying_authored()) {
// For Attribute, `varying` is the default variability and does not shown
// in USDA do nothing
}

std::string ty;

ty = attr.type_name();
ss << ty << " " << prop_name;

if (attr.is_connection()) {
ss << ".connect = ";

const std::vector<Path> &paths = attr.connections();
Expand All @@ -1219,32 +1296,10 @@ std::string print_prop(const Property &prop, const std::string &prop_name,
} else {
ss << paths;
}
} else if (prop.is_empty()) {
// Nothing to do
} else {
// has value content

if (attr.get_var().is_timesamples()) {
ss << ".timeSamples";
}
ss << " = ";

if (attr.get_var().is_timesamples()) {
ss << print_timesamples(attr.get_var().ts_raw(), indent);
} else if (attr.is_blocked()) {
ss << "None";
} else {
// is_scalar
ss << value::pprint_value(attr.get_var().value_raw());
}
ss << "\n";
}

if (prop.get_attribute().metas().authored()) {
ss << " (\n"
<< print_attr_metas(prop.get_attribute().metas(), indent + 1)
<< pprint::Indent(indent) << ")";
}
ss << "\n";
} else {
ss << "[Invalid Property] " << prop_name << "\n";
}
Expand Down
46 changes: 43 additions & 3 deletions src/prim-types.hh
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,10 @@ class TypedAttribute {
return nonstd::nullopt;
}

bool has_connection() const {
return _paths.size();
}

// TODO: Supply set_connection_empty()?

void set_value_empty() { _value_empty = true; }
Expand Down Expand Up @@ -2202,7 +2206,9 @@ enum class TimeSampleInterpolation {
#endif

// Attribute is a struct to hold generic attribute of a property(e.g. primvar)
// of Prim
// of Prim.
// It can have multiple values(default value, timeSamples and connection) at once.
//
// TODO: Refactor
class Attribute {

Expand Down Expand Up @@ -2370,7 +2376,14 @@ class Attribute {

void set_blocked(bool onoff) { _var.set_blocked(onoff); }

bool is_blocked() const { return _var.is_blocked(); }
bool is_blocked() const {
if (has_timesamples()) {
return false;
}

return _var.is_blocked();
}
bool has_blocked() const { return _var.is_blocked(); }

Variability &variability() { return _variability; }
Variability variability() const { return _variability; }
Expand All @@ -2381,20 +2394,43 @@ class Attribute {

bool is_varying_authored() const { return _varying_authored; }

bool is_connection() const { return _paths.size(); }
bool is_connection() const {
if (has_timesamples()) {
return false;
}

if (has_blocked()) {
return false;
}

return _paths.size();
}

bool has_connection() const {
return _paths.size();
}

bool is_value() const {
if (is_connection()) {
return false;
}

if (is_timesamples()) {
return false;
}

if (is_blocked()) {
return false;
}

return true;
}

// check if Attribute has default value
bool has_value() const {
return _var.has_value();
}

bool is_timesamples() const {
if (!is_value()) {
return false;
Expand All @@ -2403,6 +2439,10 @@ class Attribute {
return _var.is_timesamples();
}

bool has_timesamples() const {
return _var.has_timesamples();
}

void set_connection(const Path &path) {
_paths.clear();
_paths.push_back(path);
Expand Down
34 changes: 28 additions & 6 deletions src/primvar.hh
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,24 @@ struct PrimVar {
bool _blocked{false}; // ValueBlocked.
value::TimeSamples _ts; // For TimeSamples value.

bool has_value() const {
// ValueBlock is treated as having a value.
if (_blocked) {
return true;
}
return _value.type_id() != value::TypeId::TYPE_ID_INVALID;
}

bool has_timesamples() const {
return _ts.size();
}

bool is_scalar() const {
return _ts.empty();
}

bool is_timesamples() const {
return _ts.size();
return !has_value() && _ts.size();
}

bool is_blocked() const {
Expand All @@ -64,11 +76,13 @@ struct PrimVar {
}

bool is_valid() const {
if (is_timesamples()) {
return _ts.type_id() != value::TypeId::TYPE_ID_INVALID;
} else {
return _value.type_id() != value::TypeId::TYPE_ID_INVALID;
if (has_timesamples()) {
if (_ts.type_id() == value::TypeId::TYPE_ID_INVALID) {
return false;
}
}

return has_value();
}

std::string type_name() const {
Expand Down Expand Up @@ -170,10 +184,13 @@ struct PrimVar {

template <class T>
void set_value(const T &v) {
_ts.clear();
_value = v;
}

void clear_value() {
_value = nullptr;
}

void set_timesamples(const value::TimeSamples &v) {
_ts = v;
}
Expand All @@ -182,6 +199,10 @@ struct PrimVar {
_ts = std::move(v);
}

void clear_timesamples() {
_ts.clear();
}

template <typename T>
void set_timesample(double t, const T &v) {
_ts.add_sample(t, v);
Expand All @@ -191,6 +212,7 @@ struct PrimVar {
_ts.add_sample(t, v);
}


#if 0 // TODO
///
/// Get typed TimesSamples
Expand Down
7 changes: 7 additions & 0 deletions tests/usda/multi-attribute-001.usda
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#usda 1.0

def "bora" {
float val = 100.5
float val.timeSamples = {0: 1.0, 2: 2.0, 3: 3.0}
#float val.connect = </bora.value>
}

0 comments on commit 30ed6a2

Please sign in to comment.