From 9a1d0afc2ae5c7c8d330a061ed721cfc6a538173 Mon Sep 17 00:00:00 2001 From: Syoyo Fujita Date: Sat, 28 Oct 2023 23:24:26 +0900 Subject: [PATCH] [USDA parser] Fix parsing `variantSet` statement when it contains comments line. [tusdcat] Fix composition logic. --- examples/tusdcat/main.cc | 98 ++++++++++---------- src/ascii-parser.cc | 8 +- tests/usda/variantSet-comment-block-001.usda | 31 +++++++ 3 files changed, 88 insertions(+), 49 deletions(-) create mode 100644 tests/usda/variantSet-comment-block-001.usda diff --git a/examples/tusdcat/main.cc b/examples/tusdcat/main.cc index 0359725e7..67c2871ac 100644 --- a/examples/tusdcat/main.cc +++ b/examples/tusdcat/main.cc @@ -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)) { @@ -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; } } diff --git a/src/ascii-parser.cc b/src/ascii-parser.cc index 5429baeb5..d5b8df95e 100644 --- a/src/ascii-parser.cc +++ b/src/ascii-parser.cc @@ -4336,6 +4336,10 @@ bool AsciiParser::ParseVariantSet(const int64_t primIdx, Rewind(1); } + if (!SkipCommentAndWhitespaceAndNewline()) { + return false; + } + // string std::string variantName; if (!ReadBasicType(&variantName)) { @@ -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; } diff --git a/tests/usda/variantSet-comment-block-001.usda b/tests/usda/variantSet-comment-block-001.usda new file mode 100644 index 000000000..b4916bff8 --- /dev/null +++ b/tests/usda/variantSet-comment-block-001.usda @@ -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@ + ) { + string geomname = "./isCoral.usd" + + } + #"isCoral1" ( + # kind = "component" + # prepend payload = @./isCoral1.usd@ + #) { + } + } +}