Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-usd

Source of release 0.9.4

0.9.4 / src / usda / writer / order.lucb

## Orders and field classes: TfDictionaryLessThan (pxr/base/tf/
## stringUtils.cpp), which sorts metadata and properties case-insensitively
## with digit runs compared by value; and which of a spec's fields the
## writer puts in its metadata section (Sdf_IsMetadataField: every field
## that is metadata for the spec's type, and every field the type does not
## know, which may be plugin metadata).

## The fields each spec type knows that are not metadata.
let prim_fields: str[13] = ["specifier", "comment", "inheritPaths", "specializes", "primChildren", "primOrder",
    "properties", "propertyOrder", "references", "relocates", "variantSelection", "variantSetChildren",
    "variantSetNames"]
let attribute_fields: str[10] = ["custom", "variability", "comment", "default", "typeName", "spline",
    "connectionChildren", "connectionPaths", "displayUnit", "timeSamples"]
let relationship_fields: str[6] = ["custom", "variability", "comment", "default", "targetChildren", "targetPaths"]
let layer_fields: str[6] = ["comment", "primChildren", "layerRelocates", "primOrder", "subLayers", "subLayerOffsets"]

## Whether field `name` belongs in the metadata section of a `kind` spec.
func is_metadata(kind: SpecType, name: str) -> bool:
    match kind:
        .prim, .variant:
            for field in prim_fields:
                if field == name: return false
        .attribute:
            for field in attribute_fields:
                if field == name: return false
        .relationship:
            for field in relationship_fields:
                if field == name: return false
        .pseudo_root:
            for field in layer_fields:
                if field == name: return false
        _: return true
    return true

extend Writer:
    ## The fields of `spec` for its metadata section, sorted by name
    ## (dictionary order, or byte order for the layer), without `comment`.
    func metadata_fields(spec: const Spec*, extra: FieldFilter, fields: Growing[u32]*) -> !:
        fields.clear()
        for field in spec.fields.view():
            let name = self.text(field.name)
            if field.name == self.keys.comment: continue
            if field.name == self.keys.type_name and (spec.kind == SpecType.prim or spec.kind == SpecType.variant): continue
            if is_metadata(spec.kind, name) or extra(self.keys, field.name): try fields.push(field.name)
        let dictionary = spec.kind != SpecType.pseudo_root
        # Insertion sort: metadata sections are short.
        for index in 1..<fields.count:
            let current = fields.items[index]
            var at = index
            while at > 0 and self.less(current, fields.items[at - 1], dictionary):
                fields.items[at] = fields.items[at - 1]
                at -= 1
            fields.items[at] = current

    func less(left: u32, right: u32, dictionary: bool) -> bool:
        let a = self.text(left)
        let b = self.text(right)
        if dictionary: return dictionary_less(a, b)
        return byte_less(a, b)

## Which extra fields a spec type writes in its metadata section.
type FieldFilter = func(FieldKeys, u32) -> bool

func byte_less(a: str, b: str) -> bool:
    let common = a.length if a.length < b.length else b.length
    for index in 0..<common:
        if a.bytes[index] != b.bytes[index]: return a.bytes[index] < b.bytes[index]
    return a.length < b.length