model formats (mca..x3d): comments are specs only
This commit is contained in:
1 parent
0bd7b87d2b
commit
b952c10222
49 files changed
+310
-936
No files matched your search
@@ -1,40 +1,33 @@
|
||||
--!strict
|
||||
-- model/usda/Parse: USD ASCII (Sdf text format, "#usda 1.0") -> a layer of prim specs. New code, from the Sdf text
|
||||
-- grammar as Pixar's USD writes and reads it: layer metadata, def / over / class prims with metadata, attributes
|
||||
-- (custom / uniform / varying / config, scalar and array types, defaults, .timeSamples, .connect, metadata), relationships,
|
||||
-- list ops (prepend / append / add / delete / reorder), variant sets, dictionaries, strings ("", '', triple quoted),
|
||||
-- asset paths (@...@, @@@...@@@), paths (<...>), comments (#, //, /* */). Numeric arrays (points, indices) take a fast
|
||||
-- path into flat number arrays. .spline blocks and unknown constructs are skipped by bracket matching.
|
||||
-- Errors raise a table { usda = message, at = byte offset } inside this module; Parse.layer catches them (pcall) and
|
||||
-- returns nil, message, offset - nothing escapes.
|
||||
-- model/usda/Parse: USD ASCII ("#usda 1.0") to prim specs, from the Sdf text grammar as Pixar's USD writes and reads
|
||||
-- it; new code. .spline blocks and unknown constructs are skipped by bracket matching. Never throws.
|
||||
local Parse = {}
|
||||
|
||||
-- values: numbers, booleans, strings, and these tagged tables
|
||||
export type Arr = { t: "arr", w: number, v: { number } } -- numeric array, w numbers per element (tuples flattened)
|
||||
export type Arr = { t: "arr", w: number, v: { number } } -- w numbers per element (tuples flattened)
|
||||
export type Value = any
|
||||
|
||||
export type Prop = {
|
||||
name: string,
|
||||
rel: boolean, -- a relationship
|
||||
typeName: string?, -- "point3f", "token", ... (attributes)
|
||||
rel: boolean,
|
||||
typeName: string?,
|
||||
array: boolean?,
|
||||
uniform: boolean?,
|
||||
custom: boolean?,
|
||||
value: Value?, -- the default value (attributes) or the targets (relationships: { path strings })
|
||||
value: Value?, -- relationships: { path strings }
|
||||
hasValue: boolean?,
|
||||
samples: { times: { number }, values: { Value } }?,
|
||||
connect: { string }?, -- connection target paths (attributes)
|
||||
connect: { string }?,
|
||||
meta: { [string]: Value }?,
|
||||
}
|
||||
export type Prim = {
|
||||
specifier: string, -- "def" | "over" | "class"
|
||||
specifier: string,
|
||||
typeName: string?,
|
||||
name: string,
|
||||
meta: { [string]: Value },
|
||||
props: { [string]: Prop },
|
||||
order: { string }, -- property names in file order
|
||||
order: { string },
|
||||
children: { Prim },
|
||||
variantSets: { [string]: { [string]: Prim } }?, -- set -> variant -> a prim body (props / children)
|
||||
variantSets: { [string]: { [string]: Prim } }?,
|
||||
}
|
||||
export type Layer = { meta: { [string]: Value }, prims: { Prim } }
|
||||
|
||||
@@ -51,12 +44,10 @@ local function fail(msg: string, at: number): never
|
||||
error({ usda = msg, at = at }, 0)
|
||||
end
|
||||
|
||||
-- the parser over one text; positions are 1-based string indices
|
||||
local function parser(s: string, maxBytes: number)
|
||||
local n = #s
|
||||
local depth = 0
|
||||
|
||||
-- whitespace and comments (newlines included)
|
||||
local function ws(i: number): number
|
||||
while true do
|
||||
local _, e = sfind(s, "^[ \t\r\n]+", i)
|
||||
@@ -64,13 +55,13 @@ local function parser(s: string, maxBytes: number)
|
||||
i = e + 1
|
||||
end
|
||||
local c = sbyte(s, i)
|
||||
if c == 35 then -- #
|
||||
if c == 35 then
|
||||
local nl = sfind(s, "\n", i, true)
|
||||
i = if nl then nl + 1 else n + 1
|
||||
elseif c == 47 and sbyte(s, i + 1) == 47 then -- //
|
||||
elseif c == 47 and sbyte(s, i + 1) == 47 then
|
||||
local nl = sfind(s, "\n", i, true)
|
||||
i = if nl then nl + 1 else n + 1
|
||||
elseif c == 47 and sbyte(s, i + 1) == 42 then -- /* */
|
||||
elseif c == 47 and sbyte(s, i + 1) == 42 then
|
||||
local _, ce = sfind(s, "*/", i + 2, true)
|
||||
if not ce then
|
||||
fail("unterminated comment", i)
|
||||
@@ -105,7 +96,7 @@ local function parser(s: string, maxBytes: number)
|
||||
end
|
||||
return (string.gsub(str, "\\(x?)(.)", function(x: string, c: string): string
|
||||
if x == "x" then
|
||||
return "\\x" .. c -- left alone (rare)
|
||||
return "\\x" .. c
|
||||
end
|
||||
return ESC[c] or c
|
||||
end))
|
||||
@@ -148,7 +139,6 @@ local function parser(s: string, maxBytes: number)
|
||||
return v, (e :: number) + 1
|
||||
end
|
||||
end
|
||||
-- inf / -inf / nan
|
||||
local w, e2 = sfind(s, "^[-+]?%a+", i)
|
||||
if w then
|
||||
local word = string.lower(ssub(s, w, e2))
|
||||
@@ -163,7 +153,6 @@ local function parser(s: string, maxBytes: number)
|
||||
return nil, i
|
||||
end
|
||||
|
||||
-- skip a balanced (), [], {} block starting at i (strings and comments respected)
|
||||
local function skipBalanced(i: number): number
|
||||
local open = sbyte(s, i)
|
||||
local level = 0
|
||||
@@ -202,7 +191,6 @@ local function parser(s: string, maxBytes: number)
|
||||
local value: (i: number) -> (Value, number)
|
||||
local metadata: (i: number) -> ({ [string]: Value }, number)
|
||||
|
||||
-- [ ... ]: the fast path for numeric arrays (flat numbers + tuple width), else a generic list
|
||||
local function list(i: number): (Value, number)
|
||||
local close = sfind(s, "]", i + 1, true)
|
||||
if close then
|
||||
@@ -216,7 +204,7 @@ local function parser(s: string, maxBytes: number)
|
||||
w += 1
|
||||
end
|
||||
elseif sfind(body, "[%(%)]") then
|
||||
w = -1 -- nested tuples: the generic path
|
||||
w = -1
|
||||
end
|
||||
if w > 0 then
|
||||
local out: { number } = {}
|
||||
@@ -240,7 +228,6 @@ local function parser(s: string, maxBytes: number)
|
||||
end
|
||||
end
|
||||
end
|
||||
-- generic
|
||||
local items = {}
|
||||
i += 1
|
||||
while true do
|
||||
@@ -256,7 +243,7 @@ local function parser(s: string, maxBytes: number)
|
||||
v, i = value(i)
|
||||
table.insert(items, v)
|
||||
i = ws(i)
|
||||
if ssub(s, i, i) == "(" then -- per-item metadata (subLayers offsets, ...)
|
||||
if ssub(s, i, i) == "(" then
|
||||
local _
|
||||
_, i = metadata(i)
|
||||
i = ws(i)
|
||||
@@ -268,7 +255,6 @@ local function parser(s: string, maxBytes: number)
|
||||
fail("expected ',' or ']' in a list", i)
|
||||
end
|
||||
end
|
||||
-- all numbers, or all equal-width numeric tuples -> arr
|
||||
local w: number? = nil
|
||||
local flat: { number } = {}
|
||||
for _, it in items do
|
||||
@@ -326,7 +312,6 @@ local function parser(s: string, maxBytes: number)
|
||||
return { t = "tuple", v = items }, i
|
||||
end
|
||||
|
||||
-- { type key = value ... } (metadata dictionaries)
|
||||
local function dict(i: number): (Value, number)
|
||||
local out: { [string]: Value } = {}
|
||||
i += 1
|
||||
@@ -403,7 +388,7 @@ local function parser(s: string, maxBytes: number)
|
||||
end
|
||||
local a: { [string]: any } = { t = "asset", v = path }
|
||||
local j = ws(i)
|
||||
if ssub(s, j, j) == "<" then -- references = @file@</prim>
|
||||
if ssub(s, j, j) == "<" then
|
||||
local e = sfind(s, ">", j + 1, true)
|
||||
if not e then
|
||||
fail("unterminated path", j)
|
||||
@@ -444,7 +429,6 @@ local function parser(s: string, maxBytes: number)
|
||||
return v, i
|
||||
end
|
||||
|
||||
-- ( key = value ; "doc" ; prepend key = value ... )
|
||||
function metadata(i: number): ({ [string]: Value }, number)
|
||||
local out: { [string]: Value } = {}
|
||||
i = expect(i, "(")
|
||||
@@ -481,9 +465,7 @@ local function parser(s: string, maxBytes: number)
|
||||
local v
|
||||
v, i = value(i + 1)
|
||||
if op == "delete" then
|
||||
-- deletions only matter for composition
|
||||
elseif op and op ~= "reorder" then
|
||||
-- prepend / append / add: merge into one list
|
||||
local prev = out[key :: string]
|
||||
if type(prev) == "table" and prev.t == "list" and type(v) == "table" and v.t == "list" then
|
||||
for _, x in v.v do
|
||||
@@ -499,7 +481,6 @@ local function parser(s: string, maxBytes: number)
|
||||
end
|
||||
end
|
||||
|
||||
-- time samples: { time: value, ... }
|
||||
local function samples(i: number): ({ times: { number }, values: { Value } }, number)
|
||||
local times: { number }, values: { Value } = {}, {}
|
||||
i = expect(i, "{")
|
||||
@@ -523,7 +504,7 @@ local function parser(s: string, maxBytes: number)
|
||||
i = expect(i, ":")
|
||||
local v
|
||||
v, i = value(i)
|
||||
table.insert(times, t :: number) -- fail() threw on nil
|
||||
table.insert(times, t :: number)
|
||||
table.insert(values, v)
|
||||
end
|
||||
return { times = times, values = values }, i
|
||||
@@ -562,7 +543,6 @@ local function parser(s: string, maxBytes: number)
|
||||
fail("expected a prim type or name", i)
|
||||
end
|
||||
i = ws(i)
|
||||
-- dotted schema names (rare)
|
||||
while ssub(s, i, i) == "." do
|
||||
local more
|
||||
more, i = ident(i + 1)
|
||||
@@ -618,7 +598,7 @@ local function parser(s: string, maxBytes: number)
|
||||
end
|
||||
end
|
||||
|
||||
-- one property: [listop] [custom] [uniform|varying|config] (rel name | type[] name)[.connect|.timeSamples|...]
|
||||
-- [listop] [custom] [uniform|varying|config] (rel name | type[] name)[.connect|.timeSamples|...]
|
||||
local function property(i: number, first: string, p: Prim): number
|
||||
local word: string? = first
|
||||
local op: string? = nil
|
||||
@@ -685,7 +665,6 @@ local function parser(s: string, maxBytes: number)
|
||||
local v
|
||||
v, i = value(i)
|
||||
if op == "delete" then
|
||||
-- composition only
|
||||
elseif suffix == "connect" then
|
||||
pr.connect = targets(v)
|
||||
elseif isRel then
|
||||
@@ -755,7 +734,6 @@ local function parser(s: string, maxBytes: number)
|
||||
end
|
||||
end
|
||||
|
||||
-- the whole layer
|
||||
local function layer(): Layer
|
||||
local i = 1
|
||||
if ssub(s, 1, 3) == "\239\187\191" then
|
||||
@@ -799,7 +777,6 @@ local function parser(s: string, maxBytes: number)
|
||||
return layer
|
||||
end
|
||||
|
||||
-- layer(text, opts?) -> Layer | nil, message, offset (0-based)
|
||||
function Parse.layer(text: string, maxBytes: number?): (Layer?, string?, number?)
|
||||
local run = parser(text, maxBytes or 2 ^ 30)
|
||||
local ok, res = pcall(run)
|
||||
@@ -812,7 +789,6 @@ function Parse.layer(text: string, maxBytes: number?): (Layer?, string?, number?
|
||||
return nil, "parse error: " .. tostring(res), nil
|
||||
end
|
||||
|
||||
-- version(text) -> the "#usda <version>" string
|
||||
function Parse.version(text: string): string?
|
||||
return smatch(text, "^\239?\187?\191?#usda%s+([%d%.]+)")
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user