Skip to content

Commit

Permalink
[USDA parser] Fix parsing variantSet statement when it contains com…
Browse files Browse the repository at this point in the history
…ments line.

[tusdcat] Fix composition logic.
  • Loading branch information
syoyo committed Oct 28, 2023
1 parent 47fe53c commit 9a1d0af
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 49 deletions.
98 changes: 51 additions & 47 deletions examples/tusdcat/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,15 @@ int main(int argc, char **argv) {
}

// TODO: Find more better way to Recursively resolve references/payload/variants
bool all_resolved = true;
for (int i = 0; i < kMaxIteration; i++) {

bool has_unresolved = false;

if (comp_features.references) {
if (!src_layer.check_unresolved_references()) {
all_resolved = true;
std::cout << "# iter " << i << ": no unresolved references.\n";
} else {
all_resolved = false;
has_unresolved = true;

tinyusdz::Layer composited_layer;
if (!tinyusdz::CompositeReferences(resolver, src_layer, &composited_layer, &warn, &err)) {
Expand All @@ -225,79 +226,82 @@ int main(int argc, char **argv) {

if (comp_features.payload) {
if (!src_layer.check_unresolved_payload()) {
all_resolved = true;
std::cout << "# iter " << i << ": no unresolved payload.\n";
} else {
all_resolved = false;
}
has_unresolved = true;

tinyusdz::Layer composited_layer;
if (!tinyusdz::CompositePayload(resolver, src_layer, &composited_layer, &warn, &err)) {
std::cerr << "Failed to composite `payload`: " << err << "\n";
return -1;
}
tinyusdz::Layer composited_layer;
if (!tinyusdz::CompositePayload(resolver, src_layer, &composited_layer, &warn, &err)) {
std::cerr << "Failed to composite `payload`: " << err << "\n";
return -1;
}

if (warn.size()) {
std::cout << "WARN: " << warn << "\n";
}
if (warn.size()) {
std::cout << "WARN: " << warn << "\n";
}

std::cout << "# `payload` composited\n";
std::cout << composited_layer << "\n";
std::cout << "# `payload` composited\n";
std::cout << composited_layer << "\n";

src_layer = std::move(composited_layer);
src_layer = std::move(composited_layer);
}
}

if (comp_features.inherits) {
if (!src_layer.check_unresolved_inherits()) {
all_resolved = true;
std::cout << "# iter " << i << ": no unresolved inherits.\n";
} else {
all_resolved = false;
}
has_unresolved = true;

tinyusdz::Layer composited_layer;
if (!tinyusdz::CompositeInherits(src_layer, &composited_layer, &warn, &err)) {
std::cerr << "Failed to composite `inherits`: " << err << "\n";
return -1;
}
tinyusdz::Layer composited_layer;
if (!tinyusdz::CompositeInherits(src_layer, &composited_layer, &warn, &err)) {
std::cerr << "Failed to composite `inherits`: " << err << "\n";
return -1;
}

if (warn.size()) {
std::cout << "WARN: " << warn << "\n";
}
if (warn.size()) {
std::cout << "WARN: " << warn << "\n";
}

std::cout << "# `inherits` composited\n";
std::cout << composited_layer << "\n";
std::cout << "# `inherits` composited\n";
std::cout << composited_layer << "\n";

src_layer = std::move(composited_layer);
src_layer = std::move(composited_layer);
}
}

if (comp_features.variantSets) {
if (!src_layer.check_unresolved_variant()) {
all_resolved = true;
std::cout << "# iter " << i << ": no unresolved variant.\n";
} else {
all_resolved = false;
}
has_unresolved = true;

tinyusdz::Layer composited_layer;
if (!tinyusdz::CompositeVariant(src_layer, &composited_layer, &warn, &err)) {
std::cerr << "Failed to composite `variantSet`: " << err << "\n";
return -1;
}
tinyusdz::Layer composited_layer;
if (!tinyusdz::CompositeVariant(src_layer, &composited_layer, &warn, &err)) {
std::cerr << "Failed to composite `variantSet`: " << err << "\n";
return -1;
}

if (warn.size()) {
std::cout << "WARN: " << warn << "\n";
}
if (warn.size()) {
std::cout << "WARN: " << warn << "\n";
}

std::cout << "# `variantSet` composited\n";
std::cout << composited_layer << "\n";
std::cout << "# `variantSet` composited\n";
std::cout << composited_layer << "\n";

src_layer = std::move(composited_layer);
src_layer = std::move(composited_layer);
}
}

// TODO
// - [ ] specializes
// - [ ] `class` Prim?

if (all_resolved) {
std::cout << "# of composition resolve iteration: " << (i + 1) << "\n";
std::cout << "# has_unresolved_references: " << src_layer.check_unresolved_references() << "\n";
std::cout << "# all resolved? " << !has_unresolved << "\n";

if (!has_unresolved) {
std::cout << "# of composition iteration to resolve fully: " << (i + 1) << "\n";
break;
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/ascii-parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4336,6 +4336,10 @@ bool AsciiParser::ParseVariantSet(const int64_t primIdx,
Rewind(1);
}

if (!SkipCommentAndWhitespaceAndNewline()) {
return false;
}

// string
std::string variantName;
if (!ReadBasicType(&variantName)) {
Expand Down Expand Up @@ -4440,12 +4444,12 @@ bool AsciiParser::ParseVariantSet(const int64_t primIdx,
DCOUT(fmt::format("Done parse ParsePrimProps."));
}

if (!SkipWhitespaceAndNewline()) {
if (!SkipCommentAndWhitespaceAndNewline()) {
return false;
}
}

if (!SkipWhitespaceAndNewline()) {
if (!SkipCommentAndWhitespaceAndNewline()) {
return false;
}

Expand Down
31 changes: 31 additions & 0 deletions tests/usda/variantSet-comment-block-001.usda
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#usda 1.0
(
defaultPrim = "isCoral"
metersPerUnit = 0.0254
upAxis = "Y"
)

def Xform "isCoral"
{
def Xform "geometry" (
variants = {
string model = "isCoral"
}
prepend variantSets = "model"
)
{
variantSet "model" = {
"isCoral" (
kind = "component"
#prepend payload = @./isCoral.usd@</isCoral>
) {
string geomname = "./isCoral.usd"

}
#"isCoral1" (
# kind = "component"
# prepend payload = @./isCoral1.usd@</isCoral1>
#) {
}
}
}

0 comments on commit 9a1d0af

Please sign in to comment.