Support custom kind value.

This commit is contained in:
Syoyo Fujita
2023-09-25 21:36:48 +09:00
parent dcfb2789fe
commit 0ff5f79630
7 changed files with 61 additions and 11 deletions

View File

@@ -453,7 +453,7 @@ std::string print_prim_metas(const PrimMeta &meta, const uint32_t indent) {
}
if (meta.kind) {
ss << pprint::Indent(indent) << "kind = " << quote(to_string(meta.kind.value())) << "\n";
ss << pprint::Indent(indent) << "kind = " << quote(meta.get_kind()) << "\n";
}
// TODO: UTF-8 ready pprint
@@ -1804,6 +1804,9 @@ std::string to_string(tinyusdz::Kind v) {
return "subcomponent";
} else if (v == tinyusdz::Kind::SceneLibrary) {
return "sceneLibrary";
} else if (v == tinyusdz::Kind::UserDef) {
// Should use PrimMeta::get_kind() to get actual Kind string value.
return "[[InternalError. UserDefKind]]";
} else {
return "[[InvalidKind]]";
}

View File

@@ -622,8 +622,11 @@ nonstd::optional<Kind> KindFromString(const std::string &str) {
} else if (str == "sceneLibrary") {
// https://developer.apple.com/documentation/arkit/usdz_schemas_for_ar/scenelibrary
return Kind::SceneLibrary;
} else if (str.empty()) {
return nonstd::nullopt;
} else {
return Kind::UserDef;
}
return nonstd::nullopt;
}
bool ValidatePrimElementName(const std::string &name) {
@@ -1313,6 +1316,7 @@ const PrimMeta &Prim::metas() const {
return EmptyStaticMeta::GetEmptyStaticMeta();
}
bool SetCustomDataByKey(const std::string &key, const MetaVariable &var,
CustomDataType &custom) {
// split by namespace
@@ -1581,6 +1585,19 @@ AssetInfo PrimMeta::get_assetInfo(bool *is_authored) const {
return ainfo;
}
const std::string PrimMeta::get_kind() const {
if (kind.has_value()) {
if (kind.value() == Kind::UserDef) {
return _kind_str;
} else {
return to_string(kind.value());
}
}
return "";
}
bool IsXformablePrim(const Prim &prim) {
uint32_t tyid = prim.type_id();

View File

@@ -90,6 +90,7 @@ enum class Purpose {
// USDZ extension: sceneLibrary
// https://developer.apple.com/documentation/arkit/usdz_schemas_for_ar/scenelibrary
//
enum class Kind {
Model,
Group,
@@ -97,6 +98,7 @@ enum class Kind {
Component,
Subcomponent,
SceneLibrary, // USDZ extension
UserDef, // Unknown or user defined Kind
Invalid
};
@@ -791,7 +793,9 @@ struct Payload {
struct PrimMetas {
nonstd::optional<bool> active; // 'active'
nonstd::optional<bool> hidden; // 'hidden'
nonstd::optional<Kind> kind; // 'kind'
nonstd::optional<Kind> kind; // 'kind'. user-defined kind value is stored in _kind_str;
std::string _kind_str;
nonstd::optional<Dictionary>
assetInfo; // 'assetInfo' // TODO: Use AssetInfo?
nonstd::optional<Dictionary> customData; // `customData`
@@ -804,6 +808,10 @@ struct PrimMetas {
nonstd::optional<bool> instanceable; // 'instanceable'
// String representation of Kind.
// For user-defined Kind, it returns `_kind_str`
const std::string get_kind() const;
//
// AssetInfo utility function
//

View File

@@ -942,7 +942,10 @@ class USDAReader::Impl {
// USDZ specific: https://developer.apple.com/documentation/arkit/usdz_schemas_for_ar/scenelibrary
out->kind = Kind::SceneLibrary;
} else {
PUSH_ERROR_AND_RETURN("Invalid token for `kind` metadataum.");
// NOTE: empty token allowed.
out->kind = Kind::UserDef;
out->_kind_str = tok.str();
}
DCOUT("Added kind: " << to_string(out->kind.value()));
} else {

View File

@@ -1825,13 +1825,26 @@ bool USDCReader::Impl::ParsePrimSpec(const crate::FieldValuePairVector &fvs,
}
} else if (fv.first == "kind") {
if (auto pv = fv.second.as<value::token>()) {
if (auto kv = KindFromString(pv->str())) {
primMeta.kind = kv.value();
} else {
PUSH_ERROR_AND_RETURN_TAG(
kTag, fmt::format("Invalid token for `kind` Prim metadata: `{}`",
pv->str()));
}
const value::token tok = (*pv);
if (tok.str() == "subcomponent") {
primMeta.kind = Kind::Subcomponent;
} else if (tok.str() == "component") {
primMeta.kind = Kind::Component;
} else if (tok.str() == "model") {
primMeta.kind = Kind::Model;
} else if (tok.str() == "group") {
primMeta.kind = Kind::Group;
} else if (tok.str() == "assembly") {
primMeta.kind = Kind::Assembly;
} else if (tok.str() == "sceneLibrary") {
// USDZ specific: https://developer.apple.com/documentation/arkit/usdz_schemas_for_ar/scenelibrary
primMeta.kind = Kind::SceneLibrary;
} else {
primMeta.kind = Kind::UserDef;
primMeta._kind_str = tok.str();
}
} else {
PUSH_ERROR_AND_RETURN_TAG(kTag,
"`kind` must be type `token`, but got type `"

View File

@@ -0,0 +1,6 @@
#usda 1.0
def "bora" (
kind = "muda"
) {
}

Binary file not shown.