mirror of
https://github.com/lighttransport/tinyusdz.git
synced 2026-01-18 01:11:17 +01:00
Fix typo.
Implementing Variant resolver(W.I.P.)
This commit is contained in:
@@ -49,7 +49,7 @@ int main(int argc, char **argv) {
|
||||
bool has_flatten{false};
|
||||
bool has_relative{false};
|
||||
bool has_extract_variants{false};
|
||||
|
||||
|
||||
constexpr int kMaxIteration = 128;
|
||||
|
||||
std::string filepath;
|
||||
@@ -196,12 +196,12 @@ int main(int argc, char **argv) {
|
||||
src_layer = std::move(composited_layer);
|
||||
}
|
||||
|
||||
// TODO: Find more better way to Recursively resolve references/payload
|
||||
// TODO: Find more better way to Recursively resolve references/payload/variants
|
||||
bool all_resolved = true;
|
||||
for (int i = 0; i < kMaxIteration; i++) {
|
||||
|
||||
if (comp_features.references) {
|
||||
if (!src_layer.check_unresoled_references()) {
|
||||
if (!src_layer.check_unresolved_references()) {
|
||||
all_resolved = true;
|
||||
} else {
|
||||
all_resolved = false;
|
||||
@@ -224,7 +224,7 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
|
||||
if (comp_features.payload) {
|
||||
if (!src_layer.check_unresoled_payload()) {
|
||||
if (!src_layer.check_unresolved_payload()) {
|
||||
all_resolved = true;
|
||||
} else {
|
||||
all_resolved = false;
|
||||
@@ -247,7 +247,7 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
|
||||
// TODO... more composition features
|
||||
|
||||
|
||||
|
||||
if (all_resolved) {
|
||||
std::cout << "# of composition resolve iteration: " << (i + 1) << "\n";
|
||||
|
||||
@@ -1005,7 +1005,7 @@ bool HasReferences(const Layer &layer, const bool force_check,
|
||||
return layer.has_unresolved_references();
|
||||
}
|
||||
|
||||
return layer.check_unresoled_references(options.max_depth);
|
||||
return layer.check_unresolved_references(options.max_depth);
|
||||
}
|
||||
|
||||
bool HasPayload(const Layer &layer, const bool force_check,
|
||||
@@ -1014,7 +1014,7 @@ bool HasPayload(const Layer &layer, const bool force_check,
|
||||
return layer.has_unresolved_payload();
|
||||
}
|
||||
|
||||
return layer.check_unresoled_payload(options.max_depth);
|
||||
return layer.check_unresolved_payload(options.max_depth);
|
||||
}
|
||||
|
||||
namespace {
|
||||
@@ -1157,7 +1157,7 @@ bool ExtractVariantsRec(uint32_t depth, const std::string &root_path,
|
||||
}
|
||||
|
||||
vsetdict[item.first] = variantStmtNames;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (vsetdict.size()) {
|
||||
@@ -1218,4 +1218,94 @@ bool ExtractVariants(const Stage &stage, Dictionary *dict, std::string *err) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool VariantSelectPrimSpec(PrimSpec &dst, const PrimSpec &src,
|
||||
const std::map<std::string, std::string> &variant_selection, std::string *warn,
|
||||
std::string *err) {
|
||||
|
||||
if (src.metas().variants && src.metas().variantSets) {
|
||||
// ok
|
||||
} else if (src.metas().variants) {
|
||||
if (warn) {
|
||||
(*warn) += "`variants` are authored, but `variantSets` is not authored.\n";
|
||||
}
|
||||
dst = src;
|
||||
dst.metas().variants.reset();
|
||||
dst.metas().variantSets.reset();
|
||||
dst.variantSets().clear();
|
||||
return true;
|
||||
} else if (src.metas().variantSets) {
|
||||
if (warn) {
|
||||
(*warn) += "`variantSets` are authored, but `variants` is not authored.\n";
|
||||
}
|
||||
dst = src;
|
||||
dst.metas().variants.reset();
|
||||
dst.metas().variantSets.reset();
|
||||
dst.variantSets().clear();
|
||||
// nothing to do.
|
||||
return true;
|
||||
}
|
||||
|
||||
const auto &variantSetMeta = src.metas().variantSets.value();
|
||||
|
||||
const ListEditQual qual = variantSetMeta.first;
|
||||
(void)qual;
|
||||
|
||||
dst = src;
|
||||
|
||||
for (const auto &item : variant_selection) {
|
||||
|
||||
// Check if variantSet name exists in metadata.
|
||||
const auto it = std::find_if(variantSetMeta.second.begin(), variantSetMeta.second.end(), [&](const std::string &name) {
|
||||
if (name == item.first) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
if (it == variantSetMeta.second.end()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (dst.variantSets().count(item.first)) {
|
||||
const auto &vss = dst.variantSets().at(item.first);
|
||||
|
||||
if (vss.variantSet.count(item.second)) {
|
||||
const PrimSpec &vs = vss.variantSet.at(item.second);
|
||||
|
||||
//
|
||||
// Promote variant content to this Prim.
|
||||
// Some notes:
|
||||
// - local metadataum and property always wins.
|
||||
//
|
||||
|
||||
// over-like operation
|
||||
dst.metas().update_from(vs.metas());
|
||||
|
||||
for (const auto &prop : vs.props()) {
|
||||
// local wins
|
||||
if (!dst.props().count(prop.first)) {
|
||||
dst.props().emplace(prop.first, prop.second);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO:
|
||||
// - [ ] primChildren
|
||||
// - [ ] update `primChildren` and `properties` metadataum if required.
|
||||
if (err) {
|
||||
(*err) += "TODO: implement.\n";
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
dst.variantSets().clear();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
} // namespace tinyusdz
|
||||
|
||||
@@ -165,12 +165,12 @@ bool ListVariantSelectionMaps(const Layer &layer, VariantSelectorMap &m);
|
||||
///
|
||||
/// @param[inout] dst PrimSpec where selected variant are written.
|
||||
/// @param[in] src Source PrimSpec. Source PrimSpec.
|
||||
/// @param[in] variant_selection Variant Selection list. key = variantSet name, value = variant name.
|
||||
///
|
||||
/// @return true upon success. false when error(e.g. no corresponding
|
||||
/// `variant_name` exists in `src` PrimSpec).
|
||||
/// @return true upon success. false when error. No error when any of variant_selection does not exist in `src` PrimSpec.
|
||||
///
|
||||
bool VariantSelectPrimSpec(PrimSpec &dst, const PrimSpec &src,
|
||||
const std::string &variant_name, std::string *warn,
|
||||
const std::map<std::string, std::string> &variant_selection, std::string *warn,
|
||||
std::string *err);
|
||||
|
||||
///
|
||||
|
||||
@@ -1849,6 +1849,30 @@ bool HasPayloadRec(uint32_t depth,
|
||||
|
||||
}
|
||||
|
||||
bool HasVariantRec(uint32_t depth,
|
||||
const PrimSpec &primspec,
|
||||
const uint32_t max_depth = 1024*128) {
|
||||
if (depth > max_depth) {
|
||||
// too deep
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: Also check if PrimSpec::variantSets is empty?
|
||||
if (primspec.metas().variants && primspec.metas().variantSets) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (auto &child : primspec.children()) {
|
||||
if (HasPayloadRec(depth + 1, child,
|
||||
max_depth)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool Layer::find_primspec_at(const Path &path, const PrimSpec **ps, std::string *err) {
|
||||
@@ -1910,7 +1934,7 @@ bool Layer::find_primspec_at(const Path &path, const PrimSpec **ps, std::string
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Layer::check_unresoled_references(const uint32_t max_depth) const {
|
||||
bool Layer::check_unresolved_references(const uint32_t max_depth) const {
|
||||
|
||||
bool ret = false;
|
||||
|
||||
@@ -1926,7 +1950,7 @@ bool Layer::check_unresoled_references(const uint32_t max_depth) const {
|
||||
return _has_unresolved_references;
|
||||
}
|
||||
|
||||
bool Layer::check_unresoled_payload(const uint32_t max_depth) const {
|
||||
bool Layer::check_unresolved_payload(const uint32_t max_depth) const {
|
||||
|
||||
bool ret = false;
|
||||
|
||||
@@ -1942,4 +1966,20 @@ bool Layer::check_unresoled_payload(const uint32_t max_depth) const {
|
||||
return _has_unresolved_payload;
|
||||
}
|
||||
|
||||
bool Layer::check_unresolved_variant(const uint32_t max_depth) const {
|
||||
|
||||
bool ret = false;
|
||||
|
||||
for (const auto &item : _prim_specs) {
|
||||
if (HasVariantRec(/* depth */0,
|
||||
item.second, max_depth)) {
|
||||
ret = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_has_unresolved_variant = ret;
|
||||
return _has_unresolved_variant;
|
||||
}
|
||||
|
||||
} // namespace tinyusdz
|
||||
|
||||
@@ -3424,7 +3424,7 @@ struct Layer {
|
||||
/// @param[in] max_depth Maximum PrimSpec traversal depth.
|
||||
/// @returns true if PrimSpec tree contains any (unresolved) `references`. false if not.
|
||||
///
|
||||
bool check_unresoled_references(const uint32_t max_depth = 1024 * 1024) const;
|
||||
bool check_unresolved_references(const uint32_t max_depth = 1024 * 1024) const;
|
||||
|
||||
///
|
||||
/// Check if PrimSpec tree contains any `payload` and cache the result.
|
||||
@@ -3432,7 +3432,7 @@ struct Layer {
|
||||
/// @param[in] max_depth Maximum PrimSpec traversal depth.
|
||||
/// @returns true if PrimSpec tree contains any (unresolved) `payload`. false if not.
|
||||
///
|
||||
bool check_unresoled_payload(const uint32_t max_depth = 1024 * 1024) const;
|
||||
bool check_unresolved_payload(const uint32_t max_depth = 1024 * 1024) const;
|
||||
|
||||
///
|
||||
/// Check if PrimSpec tree contains any `variant` and cache the result.
|
||||
@@ -3440,7 +3440,7 @@ struct Layer {
|
||||
/// @param[in] max_depth Maximum PrimSpec traversal depth.
|
||||
/// @returns true if PrimSpec tree contains any (unresolved) `variant`. false if not.
|
||||
///
|
||||
bool check_unresoled_variant(const uint32_t max_depth = 1024 * 1024) const;
|
||||
bool check_unresolved_variant(const uint32_t max_depth = 1024 * 1024) const;
|
||||
|
||||
///
|
||||
/// Check if PrimSpec tree contains any Prim with `over` specifier and cache the result.
|
||||
|
||||
15
tests/usda/variantSet-listop-append-001.usda
Normal file
15
tests/usda/variantSet-listop-append-001.usda
Normal file
@@ -0,0 +1,15 @@
|
||||
#usda 1.0
|
||||
|
||||
def Xform "Implicits" (
|
||||
variants = { string shapeVariant = "Capsule" }
|
||||
append variantSets = "shapeVariant"
|
||||
)
|
||||
{
|
||||
float myval = 1.0
|
||||
|
||||
variantSet "shapeVariant" = {
|
||||
"Capsule" {
|
||||
float myval = 2.0
|
||||
}
|
||||
}
|
||||
}
|
||||
18
tests/usda/variantSet-nometa-001.usda
Normal file
18
tests/usda/variantSet-nometa-001.usda
Normal file
@@ -0,0 +1,18 @@
|
||||
#usda 1.0
|
||||
|
||||
def "bora" (
|
||||
)
|
||||
{
|
||||
variantSet "shapeVariant" = {
|
||||
"Capsule" {
|
||||
def Skeleketon "myskel" {
|
||||
}
|
||||
}
|
||||
"Cone" {
|
||||
def Xform "myxform" {
|
||||
def Mesh "mymesh" {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
13
tests/usda/variants-only-001.usda
Normal file
13
tests/usda/variants-only-001.usda
Normal file
@@ -0,0 +1,13 @@
|
||||
#usda 1.0
|
||||
(
|
||||
defaultPrim = "hello"
|
||||
)
|
||||
|
||||
def Xform "hello" (
|
||||
variants = {
|
||||
string shadingVariant = "green"
|
||||
}
|
||||
)
|
||||
{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user