scene/usd_plugin.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | /// @file usd_plugin.cpp | ||
| 3 | /// @brief Ernie's own schemas as a codeless USD schema plugin: `generatedSchema.usda`, its | ||
| 4 | /// classes flattened as `usdGenSchema` writes them, and `plugInfo.json`. | ||
| 5 | /// | ||
| 6 | /// References: | ||
| 7 | /// - Pixar, "Generating New Schema Classes" (codeless schemas), | ||
| 8 | /// https://openusd.org/release/tut_generating_new_schema.html | ||
| 9 | #include "ernie/scene/json_value.h" | ||
| 10 | #include "ernie/scene/reference.h" | ||
| 11 | #include "ernie/util/text.h" | ||
| 12 | |||
| 13 | #include <array> | ||
| 14 | #include <string> | ||
| 15 | |||
| 16 | namespace ernie { | ||
| 17 | namespace { | ||
| 18 | |||
| 19 | /// @return JSON as USD's text format writes a value: arrays as tuples, or with `list` as lists. | ||
| 20 | 158 | std::string UsdText(const Json& json, bool list = false) { | |
| 21 | 158 | if (json.is_boolean()) { | |
| 22 | 11 | return json.get<bool>() ? "1" : "0"; | |
| 23 | } | ||
| 24 | 152 | if (!json.is_array()) { | |
| 25 | 122 | return json.dump(); | |
| 26 | } | ||
| 27 | 56 | const std::string items = Join(json, ", ", [](const Json& item) { return UsdText(item); }); | |
| 28 | 60 | return list ? "[" + items + "]" : "(" + items + ")"; | |
| 29 | 30 | } | |
| 30 | |||
| 31 | /// @return A fallback in USD's text format, or empty when it has none there (relationships, empty | ||
| 32 | /// assets). | ||
| 33 | 147 | std::string UsdFallback(const AttributeDef& def, const Value& fallback) { | |
| 34 | 147 | const bool none = fallback.Empty() || fallback.Is<Path>() || fallback.Is<std::vector<Path>>(); | |
| 35 | 134 | if (none || def.type == ColumnType::Tensor) { | |
| 36 | 13 | return {}; | |
| 37 | } | ||
| 38 | 134 | const Json json = ToJson(fallback); | |
| 39 | 134 | if (def.meta.asset) { | |
| 40 | 2 | return json.get<std::string>().empty() ? "" : "@" + json.get<std::string>() + "@"; | |
| 41 | } | ||
| 42 | 132 | if (def.type == ColumnType::Matrix) { | |
| 43 | // USD's rows are its row vectors: the columns of Ernie's matrices. | ||
| 44 | ✗ | Json columns = Json::array(); | |
| 45 | ✗ | for (std::size_t column = 0; column < 4; ++column) { | |
| 46 | ✗ | columns.push_back(Json::array()); | |
| 47 | ✗ | for (const Json& row : json) { | |
| 48 | ✗ | columns.back().push_back(row[column]); | |
| 49 | } | ||
| 50 | } | ||
| 51 | ✗ | return UsdText(columns); | |
| 52 | ✗ | } | |
| 53 | 132 | return UsdText(json, def.type == ColumnType::FloatArray || def.type == ColumnType::TokenArray); | |
| 54 | 134 | } | |
| 55 | |||
| 56 | 161 | std::string Quoted(std::string_view text) { | |
| 57 | 322 | return "\"\"\"" + std::string(text) + "\"\"\""; | |
| 58 | } | ||
| 59 | |||
| 60 | 26 | std::string Strings(const auto& texts) { | |
| 61 | 125 | return "[" + Join(texts, ", ", [](std::string_view text) { return Json(text).dump(); }) + "]"; | |
| 62 | } | ||
| 63 | |||
| 64 | 147 | std::string Property(const SchemaDef& schema, const AttributeDef& def) { | |
| 65 | 441 | std::string out = " " + UsdTypeName(def) + " " + std::string(def.name); | |
| 66 | 294 | if (const std::string fallback = UsdFallback(def, schema.Fallback(def)); !fallback.empty()) { | |
| 67 | 132 | out += " = " + fallback; | |
| 68 | ✗ | } | |
| 69 | 147 | out += " (\n"; | |
| 70 | 147 | if (!def.meta.options.empty()) { | |
| 71 | 72 | out += " allowedTokens = " + Strings(def.meta.options) + "\n"; | |
| 72 | } | ||
| 73 | 294 | return out + " doc = " + Quoted(def.meta.doc) + "\n )\n"; | |
| 74 | 279 | } | |
| 75 | |||
| 76 | /// @return The plugin type name of a schema, as `UsdLuxBoundableLightBase` or `ErnieLaserLight`. | ||
| 77 | 21 | std::string TypeName(const SchemaDef& schema) { | |
| 78 | 21 | const std::string name(schema.name); | |
| 79 | 21 | if (IsErnieSchema(name)) { | |
| 80 | 14 | return name.starts_with("Ernie") ? name : "Ernie" + name; | |
| 81 | } | ||
| 82 | 7 | const SchemaFamily* family = FindFamily(name); | |
| 83 | 14 | return std::string(family != nullptr ? family->usdModule : "Usd") + name; | |
| 84 | 21 | } | |
| 85 | |||
| 86 | 14 | std::string Class(const SchemaDef& schema) { | |
| 87 | 14 | const std::string name(schema.name); | |
| 88 | 14 | std::string out = schema.kind == SchemaKind::Concrete | |
| 89 | 49 | ? "class " + name + " \"" + name + "\" (\n" | |
| 90 | 21 | : "class \"" + name + "\" (\n"; | |
| 91 | 14 | if (const std::vector<std::string_view> builtins = schema.AllBuiltinApis(); !builtins.empty()) { | |
| 92 | 6 | out += " prepend apiSchemas = " + Strings(builtins) + "\n"; | |
| 93 | 14 | } | |
| 94 | 42 | out += " doc = " + Quoted(schema.doc) + "\n)\n{\n"; | |
| 95 | 161 | for (const AttributeDef* def : schema.AllAttributes()) { | |
| 96 | 294 | out += Property(schema, *def); | |
| 97 | ✗ | } | |
| 98 | 14 | return out + "}\n\n"; | |
| 99 | 14 | } | |
| 100 | |||
| 101 | 14 | Json PlugInfoType(const SchemaDef& schema) { | |
| 102 | 14 | static constexpr std::array<std::string_view, 4> kKinds{"abstractTyped", "concreteTyped", | |
| 103 | "singleApplyAPI", "multipleApplyAPI"}; | ||
| 104 | 14 | const bool typed = schema.kind == SchemaKind::Abstract || schema.kind == SchemaKind::Concrete; | |
| 105 | 14 | const std::string base = !typed ? "UsdAPISchemaBase" | |
| 106 | 7 | : schema.base != nullptr ? TypeName(*schema.base) | |
| 107 | 14 | : "UsdTyped"; | |
| 108 | 28 | Json type{{"alias", {{"UsdSchemaBase", schema.name}}}, | |
| 109 | {"autoGenerated", true}, | ||
| 110 | 42 | {"bases", Json::array({base})}, | |
| 111 | 14 | {"schemaIdentifier", schema.name}, | |
| 112 | 266 | {"schemaKind", kKinds[static_cast<std::size_t>(schema.kind)]}}; | |
| 113 | 14 | if (!schema.appliesTo.empty()) { | |
| 114 | ✗ | type["apiSchemaCanOnlyApplyTo"] = schema.appliesTo; | |
| 115 | } | ||
| 116 | 14 | return type; | |
| 117 | 252 | } | |
| 118 | |||
| 119 | } // namespace | ||
| 120 | |||
| 121 | 1 | UsdSchemaPlugin ErnieUsdSchemaPlugin() { | |
| 122 | 1 | UsdSchemaPlugin plugin; | |
| 123 | 1 | plugin.generatedSchema = "#usda 1.0\n(\n \"WARNING: THIS FILE IS GENERATED BY ernie schema. " | |
| 124 | 1 | "DO NOT EDIT.\"\n)\n\n"; | |
| 125 | 1 | Json types = Json::object(); | |
| 126 | 66 | for (const SchemaDef* schema : Schemas()) { | |
| 127 | 65 | if (IsErnieSchema(schema->name)) { | |
| 128 | 28 | plugin.generatedSchema += Class(*schema); | |
| 129 | 14 | types[TypeName(*schema)] = PlugInfoType(*schema); | |
| 130 | } | ||
| 131 | } | ||
| 132 | 24 | const Json info{{"Plugins", Json::array({{{"Info", {{"Types", std::move(types)}}}, | |
| 133 | {"LibraryPath", ""}, | ||
| 134 | {"Name", "ernie"}, | ||
| 135 | {"ResourcePath", "."}, | ||
| 136 | {"Root", "."}, | ||
| 137 | 4 | {"Type", "resource"}}})}}; | |
| 138 | 1 | plugin.plugInfo = PrintJson(info); | |
| 139 | 1 | return plugin; | |
| 140 | 31 | } | |
| 141 | |||
| 142 | } // namespace ernie | ||
| 143 |