Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-usd

Source of release 0.9.1

0.9.1 / src / usda / writer / writer.lucb

## The usda writer: LayerData to text exactly as OpenUSD's usda file format
## writes it (pxr/usd/sdf/usdaFileFormat.cpp _WriteLayer and the
## Sdf_Write* functions of fileIO_Common.h/.cpp), so a layer usdcat prints
## and a layer luce-usd prints are the same bytes.
##
## Fragments, in ORDER:
##
## writer.lucb      the Writer state, the entry point, indentation
## order.lucb       TfDictionaryLessThan and which fields are metadata
## layer.lucb       the header, layer metadata and root prims
## prims.lucb       prims, their metadata and bodies, variant sets, variants
## properties.lucb  attributes, relationships, connections and targets
## values.lucb      values: scalars, tuples, arrays, dictionaries, list ops
## arrays.lucb      numeric arrays, long ones in parallel chunks
## preformat.lucb   the layer's other arrays, formatted together ahead
## splines.lucb     attribute splines
## array_edits.lucb array edits
import memory
import errors
import usda.numbers
from usda.quote import quote_into, asset_into
from dictionary_order import dictionary_less
from sdf.names import Names, PathKind, absolute_root, relative_root, empty_path
from sdf.layer_data import LayerData, Spec, SpecType, FieldKeys, Field
from sdf.values import Value, Dictionary, ListOp, ListItem, ListPart, TimeSamples, Spline, CurveType, Interpolation, ExtrapolationMode, Extrapolation, TangentAlgorithm, ArrayEdit, EditOp, EditInstruction, 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, serialization_name, element_bytes, components, scalar_of
from luce_geocore.core.shared import Column
from luce_geocore.core.growing import Growing

## Append `layer` as usda text to `out`.
pub func write_usda(layer: const LayerData*, out: Growing[u8]*) -> !:
    var preformatted = Preformatted()
    defer preformatted.close()
    try preformatted.gather(layer)
    var writer = Writer(layer = layer, names = layer.names, keys = layer.keys, out = out, preformatted = &preformatted, header = out.count, minor = 0)
    try writer.write_layer()
    # Sdf_TextOutput rewrites the header when something needed a newer
    # version: 1.1 tangent algorithms, 1.2 array edits, 1.3 spline loop
    # boundaries.
    if writer.minor > 0: out.items[writer.header + 8] = '0' + writer.minor

struct Writer:
    var layer: const LayerData*
    var names: Names*
    var keys: FieldKeys
    var out: Growing[u8]*
    var preformatted: const Preformatted*
    ## Where `#usda 1.0` starts, and the minor version the text needs.
    var header: usize
    var minor: u8

    func put(text: str) -> !:
        try self.out.reserve(self.out.count + text.length)
        memory.copy(self.out.items[self.out.count..], text.bytes, text.length)
        self.out.count += text.length

    ## RequestWriteVersionUpgrade to 1.`minor`.
    func need_version(minor: u8):
        if minor > self.minor: self.minor = minor

    func byte(value: u8) -> !:
        try self.out.push(value)

    ## Four spaces per level.
    func indent(levels: usize) -> !:
        for _ in 0..<levels: try self.put("    ")

    func token_text(token: u32) -> !:
        try self.put(self.names.text_of(token))

    func quoted(text: str) -> !:
        try quote_into(self.out, text)

    ## `<path>`.
    func path_ref(path: u32) -> !:
        try self.byte('<')
        try self.names.write_path(path, self.out)
        try self.byte('>')

    func text(token: u32) -> str:
        return self.names.text_of(token)