Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-usd

Source of release 0.9.4

0.9.4 / src / usda / reader / reader.lucb

## The usda reader: text layers into LayerData, following OpenUSD's text
## file format grammar (pxr/usd/sdf/textFileFormatParser.h) and its actions
## (textFileFormatParser.cpp, textParserHelpers.cpp, parserHelpers.cpp): the
## same specs, fields and values Sdf's parser makes, so both write back the
## same text and crate.
##
## Fragments, in ORDER:
##
## reader.lucb      the Reader state and the entry point
## scan.lucb        padding, comments, separators, keywords, identifiers
## atoms.lucb       numbers, strings, asset paths, path references
## typed.lucb       values of a type: scalars, tuples, arrays, dictionaries
## metadata.lucb    metadata blocks of layers, prims, attributes, relationships
## references.lucb  references, payloads, inherits, specializes, sublayers
## specs.lucb       the layer, prims, variant sets and variants
## properties.lucb  attributes and relationships
## splines.lucb     attribute splines
## array_edits.lucb array edits
from luce_std import math
import memory
from luce_std import utf8
import errors
import usda.numbers
from usda.quote import quote_into, asset_into
import sdf.identifiers
from sdf.names import Names, PathKind, absolute_root, relative_root, empty_path
from sdf.layer_data import LayerData, SpecType, FieldKeys
from sdf.values import Value, Dictionary, ListOp, ListItem, ListPart, TimeSamples, PayloadRecord, Spline, Knot, CurveType, Interpolation, ExtrapolationMode, Extrapolation, LoopParams, TangentAlgorithm, ArrayEdit, EditOp, end_index, specifier_def, specifier_over, specifier_class, variability_varying, variability_uniform, permission_public, permission_private
from sdf.value_types import ValueKind, Role, TypeName, find_type, element_bytes, components, scalar_of
from sdf.splines import settle
from sdf.schema import find_field, MetadataField, FieldForm, applies_layer, applies_prim, applies_attribute, applies_relationship
from luce_geocore.core.shared import Column
from luce_geocore.core.growing import Growing

## The message of the last failed read on this thread; errors view it.
local var message_text: u8[640]

## Read the usda text `data` into `layer` (an empty LayerData over the
## Names every layer of the load shares). Fails with `invalid` and a message
## naming the line on text that is not a layer.
pub func read_usda(data: const u8[], layer: LayerData*) -> !:
    var reader = Reader(data = data, at = 0, names = layer.names, layer = layer, keys = layer.keys, scratch = Growing[u8]())
    defer reader.scratch.close()
    try reader.read_layer()

struct Reader:
    var data: const u8[]
    var at: usize
    var names: Names*
    var layer: LayerData*
    var keys: FieldKeys
    ## Decoded string bytes.
    var scratch: Growing[u8]

    ## `message` prefixed with the cursor's line, for an error:
    ## `error(errors.invalid, self.where("..."))`.
    func where(message: str) -> str:
        var line: usize = 1
        var index: usize = 0
        let end = self.at if self.at < self.data.length else self.data.length
        while index < end:
            if self.data[index] == '\n': line += 1
            index += 1
        return strings.format(message_text, f"line {line}: {message}") catch failure:
            recover message