--!strict -- model/usda: USD ASCII layers (.usda, text .usd) and USDZ packages of them, from the Sdf text format and the UsdGeom / -- UsdShade / UsdLux docs (OpenUSD 26.x), checked against pxr and Blender's exporter. Not supported: USDC crate files, -- composition arcs (listed in meta.source.unsupported), UsdSkel, blend shapes, curves / points / NURBS, instancers, -- MaterialX, writing USDZ. -- ------------------------------------------------------------------------------------------------ requires local Err = require("@rosetta/core/Err") local Bytes = require("@rosetta/core/Bytes") local Types = require("@rosetta/Types") local Zip = require("@rosetta/containers/Zip") local Parse = require("@rosetta/model/usda/Parse") local Build = require("@rosetta/model/usda/Build") local Write = require("@rosetta/model/usda/Write") -- ------------------------------------------------------------------------------------------------ contract local Usda = { name = "usda", category = "model", extensions = { "usda", "usd", "usdz" }, mime = { "model/vnd.usda", "model/vnd.usdz+zip" }, deps = { "core/Err", "core/Bytes", "Types", "containers/Zip", "codecs/model/Mat4" }, caps = { decode = true, encode = true, materials = true, animation = true, cameras = true, lights = true, polygons = true, vertexColors = true, usdz = "read", usdc = false }, } -- ------------------------------------------------------------------------------------------------ types export type DecodeOptions = { triangulate: boolean?, -- default true flipV: boolean?, -- default true resolve: ((uri: string) -> any)?, files: { [string]: any }?, maxVertices: number?, -- default 2^25 maxBytes: number?, -- default 2^30 } export type EncodeOptions = { files: { [string]: any }?, -- as _. name: string?, format: string?, -- "usdz" -> UNSUPPORTED upAxis: string?, -- default: from scene.meta.up metersPerUnit: number?, -- default scene.meta.unit or 1 } -- ------------------------------------------------------------------------------------------------ sniff local function isUsdz(r: Bytes.Reader): boolean if r:peek(4) ~= "PK\3\4" then return false end local lo, hi = r:peekU8(26), r:peekU8(27) if lo < 0 or hi < 0 then return false end local nameLen = lo + hi * 256 local name = string.lower(r:peek(30 + nameLen)) name = string.sub(name, 31) return string.find(name, "%.usd[ac]?$") ~= nil end function Usda.sniff(r: Bytes.Reader): number local head = r:peek(8) if string.sub(head, 1, 5) == "#usda" or string.sub(head, 1, 8) == "\239\187\191#usda" then return 1 end if head == "PXR-USDC" then return 0.9 -- USDC magic, so the error says it is unsupported end if isUsdz(r) then return 0.95 end return 0 end -- ------------------------------------------------------------------------------------------------ decode local function fetcher(opts: DecodeOptions?, package: any): (uri: string) -> buffer? return function(uri: string): buffer? local candidates = { uri } local slashed = string.gsub(uri, "\\", "/") local stripped = string.gsub(slashed, "^%./", "") if slashed ~= uri then table.insert(candidates, slashed) end if stripped ~= slashed then table.insert(candidates, stripped) end -- packaged assets: "model.usdz[textures/a.png]" local inner = string.match(uri, "%[(.+)%]$") if inner then table.insert(candidates, inner) end for _, u in candidates do if package then local data = package:read(u) if data then return data end end local v: any = nil if opts and type((opts :: DecodeOptions).files) == "table" then v = ((opts :: DecodeOptions).files :: { [string]: any })[u] end if v == nil and opts and type((opts :: DecodeOptions).resolve) == "function" then local ok, res = pcall((opts :: DecodeOptions).resolve :: (uri: string) -> any, u) v = if ok then res else nil end if type(v) == "string" then return buffer.fromstring(v) elseif type(v) == "buffer" then return v end end return nil end end function Usda.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?) local head = r:peek(8) if head == "PXR-USDC" then return nil, Err.new(Err.UNSUPPORTED, "USDC binary crate files (convert with usdcat to .usda)") end local text: string local package: any = nil local usdz = false if string.sub(head, 1, 4) == "PK\3\4" then local archive, zerr = Zip.open(r) if not archive then return nil, zerr or Err.new(Err.CORRUPT, "bad USDZ package") end local entries = archive:list() local root = nil for _, e in entries do if not e.dir and string.find(string.lower(e.name), "%.usd[ac]?$") then root = e break end end if not root then return nil, Err.new(Err.CORRUPT, "the USDZ package holds no USD layer") end local data, rerr = archive:read(root) if not data then return nil, rerr end if buffer.len(data) >= 8 and buffer.readstring(data, 0, 8) == "PXR-USDC" then return nil, Err.new(Err.UNSUPPORTED, "USDZ holding a USDC binary crate layer") end text = buffer.tostring(data) package = archive usdz = true else text = buffer.readstring(r.buf, r.base, r.limit - r.base) end if not Parse.version(text) then return nil, Err.new(Err.CORRUPT, "no #usda header") end local layer, msg, at = Parse.layer(text, opts and opts.maxBytes) if not layer then if msg == "array over the size limit" then return nil, Err.new(Err.LIMIT, msg :: string, at) end local code = if at and at >= #text - 1 then Err.TRUNCATED else Err.CORRUPT if msg and string.find(msg, "^unterminated") then code = Err.TRUNCATED end return nil, Err.new(code, msg or "parse error", if usdz then nil else at) end local scene, err = Build.scene(layer, { triangulate = opts and opts.triangulate, flipV = opts and opts.flipV, maxVertices = opts and opts.maxVertices, fetch = fetcher(opts, package), }) if not scene then if err == "LIMIT" then return nil, Err.new(Err.LIMIT, "a mesh over maxVertices") end return nil, Err.new(Err.CORRUPT, err or "bad scene") end local src: any = scene.meta.source src.version = Parse.version(text) src.usdz = usdz or nil return scene, nil end -- ------------------------------------------------------------------------------------------------ encode function Usda.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err?) if type(scene) ~= "table" or scene.kind ~= "scene" then return nil, Err.new(Err.ARG, "needs a Scene") end if opts and opts.format == "usdz" then return nil, Err.new(Err.UNSUPPORTED, "writing USDZ; write .usda") end local ok, text = pcall(Write.text, scene, opts) if not ok then return nil, Err.new(Err.ARG, "bad scene: " .. tostring(text)) end return buffer.fromstring(text :: any), nil end -- ------------------------------------------------------------------------------------------------ extras function Usda.parse(text: string): (Parse.Layer?, Err.Err?) local layer, msg, at = Parse.layer(text) if not layer then return nil, Err.new(Err.CORRUPT, msg or "parse error", at) end return layer, nil end return Usda