Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-usd

Source of release 0.9.2

0.9.2 / src / flatten / metadata.lucb

## Composed metadata, after UsdStage's MetadataValueComposer and its special
## cases (stage.cpp: _ComposeGeneralMetadataImpl, _GetPrimSpecifierImpl,
## _GetPrimTypeNameImpl, _GetAttrVariabilityImpl, _GetPropCustomImpl): the
## strongest opinion wins, except that dictionaries merge (stronger keys
## first) and list ops compose over weaker ones, ending as the explicit
## list they give applied to nothing. A prim's specifier is its strongest
## def (or class), else over; its type the strongest non-empty one; a
## property's custom and variability come from the weakest opinion.
## Composition arcs, values, children and read-only fields are not
## metadata (_IsPrivateFieldKey).

let private_names: str[20] = ["inheritPaths", "layerRelocates", "payload", "references", "specializes", "subLayers",
                              "subLayerOffsets", "variantSelection", "variantSetNames", "clips", "clipSets", "default", "timeSamples", "spline",
                              "connectionPaths", "targetPaths", "primChildren", "properties", "variantChildren", "variantSetChildren"]
let private_children: str[5] = ["targetChildren", "connectionChildren", "expressionChildren", "mapperChildren", "mapperArgChildren"]

## The fields Sdf's schema defines for each spec type (schema.cpp's
## _Define); plugin metadata comes from sdf/schema's table.
let layer_core: str[20] = ["colorConfiguration", "colorManagementSystem", "comment", "customLayerData", "defaultPrim",
                           "documentation", "endTimeCode", "expressionVariables", "framesPerSecond", "framePrecision", "hasOwnedSubLayers",
                           "owner", "sessionOwner", "startTimeCode", "timeCodesPerSecond", "endFrame", "startFrame", "primChildren",
                           "layerRelocates", "primOrder"]
let prim_core: str[35] = ["specifier", "comment", "inheritPaths", "specializes", "primChildren", "primOrder", "properties",
                          "propertyOrder", "references", "relocates", "variantSelection", "variantSetChildren", "variantSetNames", "active",
                          "assetInfo", "clips", "clipSets", "customData", "displayGroupOrder", "displayName", "documentation", "hidden",
                          "instanceable", "kind", "payload", "permission", "prefix", "prefixSubstitutions", "suffix", "suffixSubstitutions",
                          "symmetricPeer", "symmetryArguments", "symmetryFunction", "typeName", "apiSchemas"]
let property_core: str[16] = ["custom", "variability", "comment", "default", "assetInfo", "customData", "displayGroup",
                              "displayName", "documentation", "hidden", "permission", "prefix", "suffix", "symmetricPeer", "symmetryArguments",
                              "symmetryFunction"]
let attribute_core: str[10] = ["typeName", "spline", "connectionChildren", "connectionPaths", "displayUnit", "timeSamples",
                               "allowedTokens", "arraySizeConstraint", "colorSpace", "limits"]
let relationship_core: str[3] = ["targetChildren", "targetPaths", "noLoadHint"]

extend Flattener:
    ## Whether a field is not metadata to flatten: private, or not valid on
    ## the spec type (SdfSpec::SetInfo refuses it).
    func private_field(key: u32, kind: SpecType) -> bool:
        let text = self.names.text_of(key)
        for name in private_names:
            if name == text: return true
        for name in private_children:
            if name == text: return true
        return not valid_field(text, kind)

    ## The composed `active` (true when unauthored).
    func is_active(index: const PrimIndex*, opinions: const Opinion[]) -> bool!:
        for opinion in opinions:
            if let found = self.composer.layers.layer(opinion.layer).get(opinion.spec, self.out.keys.active): return found.as_bool()
        return true

    ## Every metadata field of the prim's specs, composed, on spec `spec`.
    func prim_metadata(index: const PrimIndex*, opinions: const Opinion[], spec: usize) -> !:
        let keys = &self.out.keys
        var fields = Growing[u32]()
        defer fields.close()
        for opinion in opinions:
            for field in self.composer.layers.layer(opinion.layer).spec(opinion.spec).fields.view():
                if not self.private_field(field.name, SpecType.prim): try add_unique(&fields, field.name)
        try add_unique(&fields, keys.specifier)
        for key in fields.view():
            if key == keys.specifier:
                try self.out.set(spec, key, Value.scalar(ValueKind.specifier, self.specifier(index, opinions)))
                continue
            if key == keys.type_name:
                let name = self.type_name(opinions)
                if name != 0: try self.out.set(spec, key, Value.scalar(ValueKind.token, (u64)name))
                continue
            if let value = try self.composed_value(index, opinions, key):
                try self.out.set(spec, key, value)

    ## The strongest def specifier; else the strongest class one, but a
    ## class due to a direct inherit only when nothing defines the prim
    ## (classness is not inherited); else over.
    func specifier(index: const PrimIndex*, opinions: const Opinion[]) -> u64:
        var resolved = specifier_over
        for opinion in opinions:
            let found = self.composer.layers.layer(opinion.layer).get(opinion.spec, self.out.keys.specifier) else continue
            if found.bits == specifier_over: continue
            if found.bits != specifier_class or not index.due_to_direct_inherit(opinion.node): return found.bits
            resolved = specifier_class
        return resolved

    ## The strongest non-empty type name other than __AnyType__ (0 for
    ## none).
    func type_name(opinions: const Opinion[]) -> u32:
        for opinion in opinions:
            let found = self.composer.layers.layer(opinion.layer).get(opinion.spec, self.out.keys.type_name) else continue
            let name = found.as_token()
            if name != 0 and self.names.text_of(name) != "__AnyType__": return name
        return 0

    ## The weakest opinion's own value of `key` (none: the field's fallback).
    func weakest_spec_field(opinions: const Opinion[], key: u32) -> const Value*?:
        if opinions.length == 0: return none
        let last = opinions[opinions.length - 1]
        return self.composer.layers.layer(last.layer).get(last.spec, key)

    ## The composed value of metadata field `key` (none when unauthored):
    ## each opinion moved into stage terms first.
    func composed_value(index: const PrimIndex*, opinions: const Opinion[], key: u32) -> Value?!:
        var value = Value.marker(ValueKind.empty)
        errdefer value.release()
        for opinion in opinions:
            let found = self.composer.layers.layer(opinion.layer).get(opinion.spec, key) else continue
            var next = try self.to_stage(index, opinion, found)
            if value.kind == ValueKind.empty:
                value = next
            else:
                let merged = compose_over(&value, &next) catch failure:
                    next.release()
                    error(failure.code, failure.message)
                next.release()
                value.release()
                value = merged
            if not composes_over(&value): break
        if value.kind == ValueKind.empty: return none
        let done = value
        value = Value.marker(ValueKind.empty)
        return try finalized(done)

    ## A field value moved into stage terms (_FieldValueToStageXf): asset
    ## paths anchored at their layer, time codes and sample times shifted by
    ## the layer's offset.
    func to_stage(index: const PrimIndex*, opinion: Opinion, value: const Value*) -> Value!:
        match value.kind:
            .asset:
                if value.array: return try self.anchored_assets(index, opinion, value)
                let evaluated = try self.evaluated_asset(index, opinion, value.as_token())
                return Value.scalar(ValueKind.asset, (u64)(try self.anchor_asset(evaluated, opinion.layer)))
            .time_code:
                let offset = self.layer_offset(index, opinion)
                if value.array:
                    let times = f64[]((f64*)value.bytes().data, value.count)
                    var column = try Column[u8].allocate(value.count * 8)
                    let out = f64[]((f64*)column.span().data, value.count)
                    for at in 0..<times.length: out[at] = offset.apply(times[at])
                    return Value.of_array(ValueKind.time_code, value.count, column.take())
                return Value.scalar(ValueKind.time_code, offset.apply(value.as_double()).bits())
            .dictionary:
                # Dictionary values move too (VtValueTryTransform recurses).
                let held = value.dictionary() else return value.share()
                let made = try Dictionary.create()
                var owner = Value.of_object(ValueKind.dictionary, (void*)made)
                errdefer owner.release()
                for at in 0..<held.keys.count:
                    try made.put(held.keys.items[at], try self.to_stage(index, opinion, &held.values.items[at]))
                return owner
            _: return value.share()

    ## The offset from the opinion's layer into stage time.
    func layer_offset(index: const PrimIndex*, opinion: Opinion) -> Offset:
        let stack = self.composer.stacks.stack(index.node(opinion.node).stack)
        return index.offset_to_root(opinion.node).times(stack.offsets.items[opinion.position])

    ## An asset array with its paths anchored, up to the first empty one
    ## (SdfAnchorAssetPaths stops there).
    func anchored_assets(index: const PrimIndex*, opinion: Opinion, value: const Value*) -> Value!:
        let ids = u32[]((u32*)value.bytes().data, value.count)
        var column = try Column[u8].allocate(value.count * 4)
        errdefer column.release()
        let out = u32[]((u32*)column.span().data, value.count)
        var anchoring = true
        for at in 0..<ids.length:
            out[at] = ids[at]
            if not anchoring: continue
            let evaluated = try self.evaluated_asset(index, opinion, ids[at])
            if self.names.text_of(evaluated).length == 0:
                anchoring = false
                continue
            out[at] = try self.anchor_asset(evaluated, opinion.layer)
        return Value.of_array(ValueKind.asset, value.count, column.take())

    ## An asset path expression evaluated with the expression variables of
    ## the layer stack that authored it (_AnchorOrResolveAssetPaths); the
    ## path itself when it is no expression, fails, or gives None.
    func evaluated_asset(index: const PrimIndex*, opinion: Opinion, asset: u32) -> u32!:
        let text = self.names.text_of(asset)
        if not is_expression(text): return asset
        let stack = self.composer.stacks.stack(index.node(opinion.node).stack)
        var result = try evaluate(self.names, text, &stack.variables)
        defer result.close()
        if result.failed or result.kind != ExpressionKind.string: return asset
        return (u32)result.number

    ## An asset path as flattening anchors it (SdfAnchorAssetPaths): anchored
    ## at the layer when that differs from the path on its own, except that
    ## a search path whose anchored file does not exist stays as it is.
    func anchor_asset(asset: u32, layer: usize) -> u32!:
        let text = self.names.text_of(asset)
        if text.length == 0: return asset
        var anchored = Growing[u8]()
        defer anchored.close()
        try ar.anchored(text, self.composer.layers.identifier(layer), &anchored)
        let search = text.bytes[0] != '.' and text.bytes[0] != '/'
        if search and not ar.exists((str)anchored.view()): return asset
        var plain = Growing[u8]()
        defer plain.close()
        try ar.normalize_into(text, &plain)
        if (str)plain.view() == (str)anchored.view(): return asset
        return try self.names.token((str)anchored.view())

## Whether a value composes over weaker ones (a dictionary or a list op).
func composes_over(value: const Value*) -> bool:
    return value.kind == ValueKind.dictionary or is_list_op(value.kind)

## `strong` composed over `weak` (VtValueTryComposeOver): dictionaries
## merge, list ops of one kind compose; otherwise the weaker value stays.
func compose_over(strong: const Value*, weak: const Value*) -> Value!:
    if strong.kind == ValueKind.dictionary and weak.kind == ValueKind.dictionary:
        let a = strong.dictionary() else return weak.share()
        let b = weak.dictionary() else return strong.share()
        return Value.of_object(ValueKind.dictionary, (void*)(try merged_dictionary(a, b)))
    if is_list_op(strong.kind) and strong.kind == weak.kind:
        let a = strong.list_op() else return weak.share()
        let b = weak.list_op() else return strong.share()
        let composed = try compose_list_ops(a, b)
        if let made = composed: return Value.of_object(strong.kind, (void*)made)
        return strong.share()
    return weak.share()

## A dictionary with `strong`'s entries over `weak`'s, nested dictionaries
## merged (VtDictionaryOverRecursive).
func merged_dictionary(strong: const Dictionary*, weak: const Dictionary*) -> Dictionary*!:
    let made = try Dictionary.create()
    var owner = Value.of_object(ValueKind.dictionary, (void*)made)
    errdefer owner.release()
    for at in 0..<weak.keys.count: try made.put(weak.keys.items[at], weak.values.items[at].share())
    for at in 0..<strong.keys.count:
        let key = strong.keys.items[at]
        let value = &strong.values.items[at]
        if let under = made.find(key):
            if value.kind == ValueKind.dictionary and under.kind == ValueKind.dictionary:
                let a = value.dictionary() else continue
                let b = under.dictionary() else continue
                try made.put(key, Value.of_object(ValueKind.dictionary, (void*)(try merged_dictionary(a, b))))
                continue
        try made.put(key, value.share())
    return made

## A composed value made final: list ops as their explicit lists.
func finalized(value: Value) -> Value!:
    var owned = value
    defer owned.release()
    if is_list_op(owned.kind):
        let list = owned.list_op() else return owned.share()
        return Value.of_object(owned.kind, (void*)(try explicit_list(list)))
    return owned.share()

## Whether Sdf's schema (with the plugin metadata) lets `name` be set on a
## spec of `kind`.
func valid_field(name: str, kind: SpecType) -> bool:
    match kind:
        .pseudo_root:
            if listed_in(layer_core, name): return true
        .prim:
            if listed_in(prim_core, name): return true
        .attribute:
            if listed_in(property_core, name) or listed_in(attribute_core, name): return true
        .relationship:
            if listed_in(property_core, name) or listed_in(relationship_core, name): return true
        _: return false
    let field = find_field(name) else return false
    let mask = applies_layer if kind == SpecType.pseudo_root else (applies_prim if kind == SpecType.prim else (applies_attribute if kind == SpecType.attribute else applies_relationship))
    return field.applies & mask != 0

func listed_in(list: const str[], name: str) -> bool:
    for known in list:
        if known == name: return true
    return false