model/ase: 3ds Max ASCII Scene Export read + write (geometry, Multi/Sub-Object materials, texture channels, colours, normals, NODE_TM hierarchy, helpers, cameras, lights; checked against assimp)

This commit is contained in:
4DBug committed 2026-11-01 18:34:55 -06:00
1 parent 01c044805f
commit 0b6773db8b
12 files changed
+1814 -1

No files matched your search

+12
View File
@@ -722,6 +722,18 @@ local list: { Entry } = {
return require("@rosetta/model/3mf")
end,
},
{
name = "ase",
category = "model",
path = "model/ase",
extensions = { "ase", "ask" },
mime = { "model/x-ase", "text/plain" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/text/Text", "codecs/model/Mat4" },
caps = { decode = true, encode = true, text = true, materials = true, cameras = true, lights = true, vertexColors = true },
load = function()
return require("@rosetta/model/ase")
end,
},
{
name = "b3d",
category = "model",
+572
View File
@@ -0,0 +1,572 @@
--!strict
-- model/ase/Build: the parsed ASE tree into a Scene. Mesh vertices are stored in world space (3ds Max ASCII export):
-- they are moved into the node's frame by the inverse NODE_TM, and node matrices are made relative to *NODE_PARENT.
local Types = require("@rosetta/Types")
local Mat4 = require("@rosetta/codecs/model/Mat4")
local Parse = require("@rosetta/model/ase/Parse")
type Item = Parse.Item
local Build = {}
local S = Types.Scene
local function first(kids: { Item }?, key: string): Item?
if kids then
for _, it in kids do
if it.key == key then
return it
end
end
end
return nil
end
local function num(it: Item?, i: number?): number?
if not it then
return nil
end
local v = tonumber(it.args[i or 1])
if v and v == v and v ~= math.huge and v ~= -math.huge then
return v
end
return nil
end
local function get(kids: { Item }?, key: string, i: number?): number?
return num(first(kids, key), i)
end
local function str(kids: { Item }?, key: string): string?
local it = first(kids, key)
return if it then it.args[1] else nil
end
local function triple(kids: { Item }?, key: string): { number }?
local it = first(kids, key)
if not it then
return nil
end
local a, b, c = num(it, 1), num(it, 2), num(it, 3)
if a and b and c then
return { a, b, c }
end
return nil
end
-- *TM_ROW0..2 are the axes, *TM_ROW3 the translation (row vectors): the columns of a column-major matrix
local function nodeTm(kids: { Item }?): { number }
local tm = first(kids, "NODE_TM")
local m = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
if not tm then
return m
end
for r = 0, 3 do
local row = triple(tm.kids, "TM_ROW" .. r)
if row then
m[r * 4 + 1], m[r * 4 + 2], m[r * 4 + 3] = row[1], row[2], row[3]
end
end
return m
end
-- ------------------------------------------------------------------------------------------------ materials
local MAPS = { "MAP_DIFFUSE", "MAP_BUMP", "MAP_OPACITY", "MAP_SELFILLUM", "MAP_SPECULAR", "MAP_SHINE", "MAP_AMBIENT", "MAP_REFLECT", "MAP_REFRACT", "MAP_SHINESTRENGTH", "MAP_FILTERCOLOR" }
local function textureRef(scene: Types.Scene, map: Item, opts: any): (Types.TextureRef?, { [string]: any })
local k = map.kids
local info: { [string]: any } = {
name = str(k, "MAP_NAME"),
class = str(k, "MAP_CLASS"),
amount = get(k, "MAP_AMOUNT"),
bitmap = str(k, "BITMAP"),
uOffset = get(k, "UVW_U_OFFSET"),
vOffset = get(k, "UVW_V_OFFSET"),
uTiling = get(k, "UVW_U_TILING"),
vTiling = get(k, "UVW_V_TILING"),
angle = get(k, "UVW_ANGLE"),
}
local path = info.bitmap
if type(path) ~= "string" or path == "" then
return nil, info
end
local tex: Types.Texture = { name = path, uri = path }
if opts and type(opts.resolve) == "function" then
local ok, data = pcall(opts.resolve, path)
if ok and type(data) == "buffer" then
tex.data = data
end
end
table.insert(scene.textures, tex)
local ref: Types.TextureRef = { texture = #scene.textures }
local su, sv = info.uTiling or 1, info.vTiling or 1
local ou, ov = info.uOffset or 0, info.vOffset or 0
local ang = info.angle or 0
if su ~= 1 or sv ~= 1 or ou ~= 0 or ov ~= 0 or ang ~= 0 then
ref.transform = { offset = { ou, ov }, scale = { su, sv }, rotation = ang }
end
return ref, info
end
local function material(scene: Types.Scene, it: Item, opts: any): Types.Material
local k = it.kids
local m = S.material(str(k, "MATERIAL_NAME"))
local diffuse = triple(k, "MATERIAL_DIFFUSE") or { 0.8, 0.8, 0.8 }
local transparency = get(k, "MATERIAL_TRANSPARENCY") or 0
local shine = get(k, "MATERIAL_SHINE") or 0
local selfIllum = get(k, "MATERIAL_SELFILLUM") or 0
m.baseColor = { diffuse[1], diffuse[2], diffuse[3], 1 - transparency }
m.metallic = 0
m.roughness = math.clamp(1 - shine, 0, 1)
m.emissive = { diffuse[1] * selfIllum, diffuse[2] * selfIllum, diffuse[3] * selfIllum }
if transparency > 0 then
m.alphaMode = "blend"
end
m.doubleSided = first(k, "MATERIAL_TWOSIDED") ~= nil
local maps: { [string]: any } = {}
for _, key in MAPS do
local map = first(k, key)
if map then
local ref, info = textureRef(scene, map, opts)
maps[string.lower(string.sub(key, 5))] = info
if ref then
if key == "MAP_DIFFUSE" then
m.baseColorTexture = ref
elseif key == "MAP_BUMP" then
m.normalTexture = ref
elseif key == "MAP_SELFILLUM" then
m.emissiveTexture = ref
m.emissive = { 1, 1, 1 }
end
end
end
end
m.extras = {
ambient = triple(k, "MATERIAL_AMBIENT"),
diffuse = diffuse,
specular = triple(k, "MATERIAL_SPECULAR"),
shininess = shine,
shineStrength = get(k, "MATERIAL_SHINESTRENGTH"),
transparency = transparency,
selfIllum = selfIllum,
ase = { class = str(k, "MATERIAL_CLASS"), shading = str(k, "MATERIAL_SHADING"), maps = maps },
}
return m
end
-- ASE material index (0-based) -> { scene material per sub-material } (one entry when it has none)
local function materials(scene: Types.Scene, list: Item?, opts: any): { { number } }
local out: { { number } } = {}
if not list or not list.kids then
return out
end
for _, it in list.kids do
if it.key == "MATERIAL" then
local subs: { number } = {}
for _, sub in it.kids or {} do
if sub.key == "SUBMATERIAL" then
table.insert(scene.materials, material(scene, sub, opts))
table.insert(subs, #scene.materials)
end
end
if #subs == 0 then
table.insert(scene.materials, material(scene, it, opts))
subs[1] = #scene.materials
end
table.insert(out, subs)
end
end
return out
end
-- ------------------------------------------------------------------------------------------------ meshes
type Lists = { v: { number }, n: number }
local function vectors(kids: { Item }?, listKey: string, itemKey: string, comps: number, max: number): { number }
local out: { number } = {}
local list = first(kids, listKey)
if not list or not list.kids then
return out
end
for _, it in list.kids do
if it.key == itemKey then
local i = num(it, 1)
if i and i >= 0 and i < max and i % 1 == 0 then
for c = 1, comps do
out[i * comps + c] = num(it, c + 1) or 0
end
end
end
end
return out
end
local function corners(kids: { Item }?, listKey: string, itemKey: string, max: number): { number }
local out: { number } = {}
local list = first(kids, listKey)
if not list or not list.kids then
return out
end
for _, it in list.kids do
if it.key == itemKey then
local i = num(it, 1)
if i and i >= 0 and i < max and i % 1 == 0 then
out[i * 3 + 1], out[i * 3 + 2], out[i * 3 + 3] = num(it, 2) or 0, num(it, 3) or 0, num(it, 4) or 0
end
end
end
return out
end
type Face = { a: number, b: number, c: number, mtl: number, smoothing: string? }
-- *MESH_FACE i: A: a B: b C: c AB: e BC: e CA: e, then its *MESH_SMOOTHING / *MESH_MTLID siblings
local function faces(list: Item?, nv: number, maxFaces: number): ({ Face }?, string?)
local out: { Face } = {}
if not list or not list.kids then
return out, nil
end
local last: Face? = nil
for _, it in list.kids do
if it.key == "MESH_FACE" then
local s = table.concat(it.args, " ")
local i = tonumber(string.match(s, "^(%d+)"))
local a = tonumber(string.match(s, "A:%s*(%d+)"))
local b = tonumber(string.match(s, "B:%s*(%d+)"))
local c = tonumber(string.match(s, "C:%s*(%d+)"))
if not (i and a and b and c) then
return nil, "bad *MESH_FACE at byte " .. it.at
end
if a >= nv or b >= nv or c >= nv then
return nil, "face vertex index out of range at byte " .. it.at
end
if i >= maxFaces then
return nil, "limit: face index " .. i
end
local f: Face = { a = a, b = b, c = c, mtl = 0, smoothing = nil }
out[i + 1] = f
last = f
elseif it.key == "MESH_MTLID" and last then
last.mtl = math.max(0, math.floor(num(it) or 0))
elseif it.key == "MESH_SMOOTHING" and last then
last.smoothing = it.args[1]
end
end
for i = 1, #out do
if out[i] == nil then
out[i] = { a = 0, b = 0, c = 0, mtl = 0, smoothing = nil }
end
end
return out, nil
end
type Channel = { verts: { number }, tf: { number }, comps: number }
local function channel(kids: { Item }?, numKey: string, listKey: string, vertKey: string, tfKey: string, tfListKey: string, nf: number, maxV: number): Channel?
local n = get(kids, numKey) or 0
if n <= 0 then
return nil
end
n = math.min(n, maxV)
local verts = vectors(kids, listKey, vertKey, 3, n)
local tf = corners(kids, tfListKey, tfKey, nf)
return { verts = verts, tf = tf, comps = 3 }
end
local function writeVec(acc: Types.Accessor, i: number, x: number, y: number, z: number)
local d = acc.data
buffer.writef32(d, i * 12, x)
buffer.writef32(d, i * 12 + 4, y)
buffer.writef32(d, i * 12 + 8, z)
end
local function mesh(scene: Types.Scene, obj: Item, matMap: { { number } }, world: { number }, opts: any): (number?, string?)
local m = first(obj.kids, "MESH")
if not m or not m.kids then
return nil, nil
end
local k = m.kids
local maxV = opts.maxVertices or 2 ^ 24
local nv = get(k, "MESH_NUMVERTEX") or 0
local nf = get(k, "MESH_NUMFACES") or 0
if nv < 0 or nf < 0 or nv % 1 ~= 0 or nf % 1 ~= 0 then
return nil, "bad vertex or face count"
end
if nv > maxV or nf * 3 > maxV then
return nil, "limit: " .. nv .. " vertices, " .. nf .. " faces"
end
local pos = vectors(k, "MESH_VERTEX_LIST", "MESH_VERTEX", 3, nv)
local fl, ferr = faces(first(k, "MESH_FACE_LIST"), nv, nf)
if not fl then
return nil, ferr
end
if #fl == 0 then
return nil, nil
end
local uvs: { Channel } = {}
local ch0 = channel(k, "MESH_NUMTVERTEX", "MESH_TVERTLIST", "MESH_TVERT", "MESH_TFACE", "MESH_TFACELIST", #fl, maxV)
if ch0 then
uvs[1] = ch0
end
for _, it in k do
if it.key == "MESH_MAPPINGCHANNEL" and it.kids then
local idx = num(it) or 0
local ch = channel(it.kids, "MESH_NUMTVERTEX", "MESH_TVERTLIST", "MESH_TVERT", "MESH_TFACE", "MESH_TFACELIST", #fl, maxV)
if ch and idx >= 2 and idx <= 8 then
uvs[idx] = ch
end
end
end
local colors = channel(k, "MESH_NUMCVERTEX", "MESH_CVERTLIST", "MESH_VERTCOL", "MESH_CFACE", "MESH_CFACELIST", #fl, maxV)
-- *MESH_NORMALS: *MESH_FACENORMAL f, then *MESH_VERTEXNORMAL v x y z for each of its corners
local normals: { number }? = nil
local nlist = first(k, "MESH_NORMALS")
if nlist and nlist.kids then
local nn: { number } = {}
local face = -1
for _, it in nlist.kids do
if it.key == "MESH_FACENORMAL" then
face = num(it) or -1
elseif it.key == "MESH_VERTEXNORMAL" and face >= 0 and face < #fl then
local f = fl[face + 1]
local v = num(it)
local corner = if v == f.a then 0 elseif v == f.b then 1 elseif v == f.c then 2 else nil
if corner then
local o = (face * 3 + corner) * 3
nn[o + 1], nn[o + 2], nn[o + 3] = num(it, 2) or 0, num(it, 3) or 0, num(it, 4) or 0
end
end
end
normals = nn
end
-- faces by scene material (sub-material = *MESH_MTLID, the last one when out of range)
local ref = get(obj.kids, "MATERIAL_REF")
local subs: { number }? = if ref and ref >= 0 then matMap[math.min(ref, #matMap - 1) + 1] else nil
local groups: { [number]: { number } } = {}
local order: { number } = {}
for i, f in fl do
local key = 0
if subs and #subs > 0 then
key = subs[math.min(f.mtl, #subs - 1) + 1]
end
local g = groups[key]
if not g then
g = {}
groups[key] = g
table.insert(order, key)
end
table.insert(g, i)
end
table.sort(order)
local inv = Mat4.inverse(world)
local local_ = inv or S.IDENTITY
local nmat = Mat4.normalMatrix(local_)
local out: Types.Mesh = { name = str(obj.kids, "NODE_NAME"), primitives = {} }
for _, key in order do
local list = groups[key]
local count = #list * 3
local P = S.accessor(nil, "f32", 3, count)
local N = if normals then S.accessor(nil, "f32", 3, count) else nil
local UV: { [number]: Types.Accessor } = {}
for idx in uvs do
UV[idx] = S.accessor(nil, "f32", 2, count)
end
local C = if colors then S.accessor(nil, "f32", 4, count) else nil
local I = S.accessor(nil, "u32", 1, count)
local w = 0
for _, fi in list do
local f = fl[fi]
local fc = { f.a, f.b, f.c }
for corner = 0, 2 do
local vi = fc[corner + 1]
local x, y, z = Mat4.point(local_, pos[vi * 3 + 1] or 0, pos[vi * 3 + 2] or 0, pos[vi * 3 + 3] or 0)
writeVec(P, w, x, y, z)
if N and normals then
local o = ((fi - 1) * 3 + corner) * 3
local nx, ny, nz = normals[o + 1] or 0, normals[o + 2] or 0, normals[o + 3] or 0
local tx = nmat[1] * nx + nmat[5] * ny + nmat[9] * nz
local ty = nmat[2] * nx + nmat[6] * ny + nmat[10] * nz
local tz = nmat[3] * nx + nmat[7] * ny + nmat[11] * nz
local len = math.sqrt(tx * tx + ty * ty + tz * tz)
if len > 0 then
tx, ty, tz = tx / len, ty / len, tz / len
end
writeVec(N, w, tx, ty, tz)
end
for idx, ch in uvs do
local ti = ch.tf[(fi - 1) * 3 + corner + 1] or 0
local u, v = ch.verts[ti * 3 + 1] or 0, ch.verts[ti * 3 + 2] or 0
buffer.writef32(UV[idx].data, w * 8, u)
buffer.writef32(UV[idx].data, w * 8 + 4, 1 - v) -- 3ds Max v goes up
end
if C and colors then
local ci = colors.tf[(fi - 1) * 3 + corner + 1] or 0
local d = C.data
buffer.writef32(d, w * 16, colors.verts[ci * 3 + 1] or 1)
buffer.writef32(d, w * 16 + 4, colors.verts[ci * 3 + 2] or 1)
buffer.writef32(d, w * 16 + 8, colors.verts[ci * 3 + 3] or 1)
buffer.writef32(d, w * 16 + 12, 1)
end
buffer.writeu32(I.data, w * 4, w)
w += 1
end
end
local attrs: { [string]: Types.Accessor } = { POSITION = P }
if N then
attrs.NORMAL = N
end
for idx, acc in UV do
attrs["TEXCOORD_" .. (idx - 1)] = acc
end
if C then
attrs.COLOR_0 = C
end
local smoothing: { string } = {}
for j, fi in list do
smoothing[j] = fl[fi].smoothing or ""
end
local prim: Types.Primitive = {
attributes = attrs,
indices = I,
mode = "triangles",
material = if key > 0 then key else nil,
extras = { smoothing = smoothing },
}
table.insert(out.primitives, prim)
end
table.insert(scene.meshes, out)
return #scene.meshes, nil
end
-- ------------------------------------------------------------------------------------------------ objects
local LIGHTS: { [string]: "directional" | "point" | "spot" } = { Omni = "point", Target = "spot", Free = "spot", Directional = "directional", TargetDirectional = "directional" }
local function camera(scene: Types.Scene, obj: Item): number
local st = first(obj.kids, "CAMERA_SETTINGS")
local fov = get(st and st.kids, "CAMERA_FOV") or math.rad(45)
local cam: Types.Camera = {
name = str(obj.kids, "NODE_NAME"),
type = "perspective",
yfov = fov, -- 3ds Max stores the horizontal field of view (radians), kept as it is
znear = get(st and st.kids, "CAMERA_NEAR") or 0,
zfar = get(st and st.kids, "CAMERA_FAR"),
}
table.insert(scene.cameras, cam)
return #scene.cameras
end
local function light(scene: Types.Scene, obj: Item): number
local st = first(obj.kids, "LIGHT_SETTINGS")
local sk = st and st.kids
local typ = LIGHTS[str(obj.kids, "LIGHT_TYPE") or "Omni"] or "point"
local l: Types.Light = {
name = str(obj.kids, "NODE_NAME"),
type = typ,
color = triple(sk, "LIGHT_COLOR") or { 1, 1, 1 },
intensity = get(sk, "LIGHT_INTENS") or 1,
}
if typ == "spot" then
l.innerCone = math.rad((get(sk, "LIGHT_HOTSPOT") or 43) / 2)
l.outerCone = math.rad((get(sk, "LIGHT_FALLOFF") or 45) / 2)
end
table.insert(scene.lights, l)
return #scene.lights
end
local KINDS = { GEOMOBJECT = true, HELPEROBJECT = true, CAMERAOBJECT = true, LIGHTOBJECT = true, SHAPEOBJECT = true }
local function collect(kids: { Item }, into: { Item }, depth: number)
for _, it in kids do
if KINDS[it.key] then
table.insert(into, it)
elseif it.key == "GROUP" and it.kids and depth < 32 then
collect(it.kids, into, depth + 1)
end
end
end
function Build.scene(root: { Item }, opts: any): (Types.Scene?, string?)
local o = opts or {}
local scene = S.new()
local version = get(root, "3DSMAX_ASCIIEXPORT")
local sk = (first(root, "SCENE") or { kids = nil } :: any).kids
scene.meta.up = "z"
scene.meta.source = {
format = "ase",
version = version,
comment = str(root, "COMMENT"),
firstFrame = get(sk, "SCENE_FIRSTFRAME"),
lastFrame = get(sk, "SCENE_LASTFRAME"),
frameSpeed = get(sk, "SCENE_FRAMESPEED"),
ticksPerFrame = get(sk, "SCENE_TICKSPERFRAME"),
background = triple(sk, "SCENE_BACKGROUND_STATIC"),
ambient = triple(sk, "SCENE_AMBIENT_STATIC"),
}
local matMap = materials(scene, first(root, "MATERIAL_LIST"), o)
local objs: { Item } = {}
collect(root, objs, 0)
local byName: { [string]: number } = {}
local worlds: { { number } } = {}
local parents: { string? } = {}
local animated = false
for i, obj in objs do
local name = str(obj.kids, "NODE_NAME")
local world = nodeTm(obj.kids)
local node: Types.Node = { name = name, children = {} }
local extras: { [string]: any } = { class = string.lower((string.gsub(obj.key, "OBJECT$", ""))) }
if obj.key == "GEOMOBJECT" then
local mi, merr = mesh(scene, obj, matMap, world, o)
if merr then
return nil, merr
end
node.mesh = mi
elseif obj.key == "CAMERAOBJECT" then
node.camera = camera(scene, obj)
elseif obj.key == "LIGHTOBJECT" then
node.light = light(scene, obj)
elseif obj.key == "HELPEROBJECT" then
extras.helperClass = str(obj.kids, "HELPER_CLASS")
end
if first(obj.kids, "TM_ANIMATION") then
animated = true
end
node.extras = { ase = extras }
scene.nodes[i] = node
worlds[i] = world
parents[i] = str(obj.kids, "NODE_PARENT")
if name and byName[name] == nil then
byName[name] = i
end
end
for i, node in scene.nodes do
local pname = parents[i]
local p = if pname then byName[pname] else nil
if p and p ~= i then
-- a parent cycle (or a parent listed later naming this node) falls back to a root
local q, steps = p, 0
while q and q ~= i and steps < #objs do
local pn = parents[q]
q = if pn then byName[pn] else nil
steps += 1
end
if q == i then
p = nil
end
else
p = nil
end
local inv = if p then Mat4.inverse(worlds[p :: number]) else nil
node.matrix = if inv then S.mul(inv, worlds[i]) else worlds[i]
if p then
table.insert(scene.nodes[p :: number].children, i)
else
table.insert(scene.roots, i)
end
end
if animated then
(scene.meta.source :: any).animated = true
end
return scene, nil
end
return Build
+67
View File
@@ -0,0 +1,67 @@
--!strict
-- model/ase/Parse: ASE text into a tree of items (*KEY args { children }). Quoted strings carry no escapes (3ds Max
-- ASCII export). Several items may share a line (*MESH_FACE ... *MESH_SMOOTHING 1 *MESH_MTLID 0).
local Parse = {}
export type Item = { key: string, args: { string }, kids: { Item }?, at: number }
function Parse.tree(text: string, maxItems: number): ({ Item }?, string?)
local root: { Item } = {}
local stack: { { Item } } = { root }
local cur: Item? = nil
local p, n = 1, #text
local count = 0
local find, sub, byte = string.find, string.sub, string.byte
while p <= n do
local s = find(text, "[^ \t\r\n]", p)
if not s then
break
end
local c = byte(text, s)
if c == 34 then -- '"'
local e = find(text, "\"", s + 1, true)
local stop = e or (n + 1)
if cur then
table.insert(cur.args, sub(text, s + 1, stop - 1))
end
p = stop + 1
elseif c == 123 then -- '{'
if not cur then
return nil, "'{' without an item at byte " .. s - 1
end
if #stack > 64 then
return nil, "limit: nesting too deep"
end
local kids: { Item } = {}
cur.kids = kids
table.insert(stack, kids)
cur = nil
p = s + 1
elseif c == 125 then -- '}'
if #stack == 1 then
return nil, "unbalanced '}' at byte " .. s - 1
end
table.remove(stack)
cur = nil
p = s + 1
else
local e = find(text, "[ \t\r\n{}\"]", s) or (n + 1)
local word = sub(text, s, e - 1)
if c == 42 then -- '*'
count += 1
if count > maxItems then
return nil, "limit: over " .. maxItems .. " items"
end
local it: Item = { key = sub(word, 2), args = {}, kids = nil, at = s - 1 }
table.insert(stack[#stack], it)
cur = it
elseif cur then
table.insert(cur.args, word)
end
p = e
end
end
return root, nil
end
return Parse
+461
View File
@@ -0,0 +1,461 @@
--!strict
-- model/ase/Write: a Scene as ASE text (3DSMAX_ASCIIEXPORT 200). Vertices are written in world space with each node's
-- world NODE_TM; every scene material is a sub-material of one Multi/Sub-Object material (faces pick it by
-- *MESH_MTLID). Triangles, strips, fans and polygons (fanned) are written; points and lines are skipped.
local Types = require("@rosetta/Types")
local Mat4 = require("@rosetta/codecs/model/Mat4")
local Write = {}
local S = Types.Scene
local function f(v: number): string
if v ~= v or v == math.huge or v == -math.huge then
return "0.0000"
end
if v == math.floor(v) and math.abs(v) < 1e15 then
return string.format("%.4f", v)
end
return string.format("%.9g", v)
end
local function q(s: string): string
return "\"" .. string.gsub(s, "\"", "'") .. "\""
end
type Out = { lines: { string } }
local function put(out: Out, depth: number, s: string)
table.insert(out.lines, string.rep("\t", depth) .. s)
end
local function tri(prim: any): { number }?
local pos = prim.attributes and prim.attributes.POSITION
if type(pos) ~= "table" then
return nil
end
local n = pos.count
local idx: { number } = {}
if prim.indices then
local rd = S.reader(prim.indices)
if not rd then
return nil
end
for i = 0, prim.indices.count - 1 do
idx[i + 1] = rd(i, 0)
end
else
for i = 0, n - 1 do
idx[i + 1] = i
end
end
local mode = prim.mode or "triangles"
local out: { number } = {}
if mode == "triangles" then
for i = 1, #idx - 2, 3 do
table.insert(out, idx[i])
table.insert(out, idx[i + 1])
table.insert(out, idx[i + 2])
end
elseif mode == "triangle_strip" then
for i = 1, #idx - 2 do
if i % 2 == 1 then
table.insert(out, idx[i])
table.insert(out, idx[i + 1])
else
table.insert(out, idx[i + 1])
table.insert(out, idx[i])
end
table.insert(out, idx[i + 2])
end
elseif mode == "triangle_fan" then
for i = 2, #idx - 1 do
table.insert(out, idx[1])
table.insert(out, idx[i])
table.insert(out, idx[i + 1])
end
elseif mode == "polygons" and prim.faces then
local rd = S.reader(prim.faces)
local at = 1
if rd then
for fi = 0, prim.faces.count - 1 do
local size = rd(fi, 0)
for k = 1, size - 2 do
table.insert(out, idx[at])
table.insert(out, idx[at + k])
table.insert(out, idx[at + k + 1])
end
at += size
end
end
else
return nil
end
for _, v in out do
if v < 0 or v >= n then
return nil
end
end
return out
end
local function material(out: Out, d: number, key: string, index: number, m: any)
put(out, d, "*" .. key .. " " .. index .. " {")
local c = if type(m.baseColor) == "table" then m.baseColor else { 1, 1, 1, 1 }
local x = if type(m.extras) == "table" then m.extras else {}
local amb = if type(x.ambient) == "table" then x.ambient else { c[1] or 1, c[2] or 1, c[3] or 1 }
local spec = if type(x.specular) == "table" then x.specular else { 0.9, 0.9, 0.9 }
put(out, d + 1, "*MATERIAL_NAME " .. q(if type(m.name) == "string" then m.name else "Material #" .. index))
put(out, d + 1, "*MATERIAL_CLASS \"Standard\"")
put(out, d + 1, "*MATERIAL_AMBIENT " .. f(amb[1] or 0) .. "\t" .. f(amb[2] or 0) .. "\t" .. f(amb[3] or 0))
put(out, d + 1, "*MATERIAL_DIFFUSE " .. f(c[1] or 1) .. "\t" .. f(c[2] or 1) .. "\t" .. f(c[3] or 1))
put(out, d + 1, "*MATERIAL_SPECULAR " .. f(spec[1] or 0) .. "\t" .. f(spec[2] or 0) .. "\t" .. f(spec[3] or 0))
put(out, d + 1, "*MATERIAL_SHINE " .. f(if type(x.shininess) == "number" then x.shininess else 1 - (m.roughness or 1)))
put(out, d + 1, "*MATERIAL_SHINESTRENGTH " .. f(if type(x.shineStrength) == "number" then x.shineStrength else 0))
put(out, d + 1, "*MATERIAL_TRANSPARENCY " .. f(1 - (c[4] or 1)))
put(out, d + 1, "*MATERIAL_WIRESIZE 1.0000")
if m.doubleSided then
put(out, d + 1, "*MATERIAL_TWOSIDED")
end
put(out, d + 1, "*MATERIAL_SHADING Blinn")
put(out, d + 1, "*MATERIAL_SELFILLUM " .. f(if type(x.selfIllum) == "number" then x.selfIllum else 0))
local ref = m.baseColorTexture
local tex = if type(ref) == "table" and type(m._textures) == "table" then m._textures[ref.texture] else nil
if tex and type(tex.uri) == "string" then
local tr = if type(ref.transform) == "table" then ref.transform else {}
local off = if type(tr.offset) == "table" then tr.offset else { 0, 0 }
local sc = if type(tr.scale) == "table" then tr.scale else { 1, 1 }
put(out, d + 1, "*MAP_DIFFUSE {")
put(out, d + 2, "*MAP_NAME " .. q(tex.name or tex.uri))
put(out, d + 2, "*MAP_CLASS \"Bitmap\"")
put(out, d + 2, "*MAP_SUBNO 1")
put(out, d + 2, "*MAP_AMOUNT 1.0000")
put(out, d + 2, "*BITMAP " .. q(tex.uri))
put(out, d + 2, "*MAP_TYPE Screen")
put(out, d + 2, "*UVW_U_OFFSET " .. f(off[1] or 0))
put(out, d + 2, "*UVW_V_OFFSET " .. f(off[2] or 0))
put(out, d + 2, "*UVW_U_TILING " .. f(sc[1] or 1))
put(out, d + 2, "*UVW_V_TILING " .. f(sc[2] or 1))
put(out, d + 2, "*UVW_ANGLE " .. f(tr.rotation or 0))
put(out, d + 1, "}")
end
put(out, d, "}")
end
local function nodeTm(out: Out, name: string, m: { number })
put(out, 1, "*NODE_TM {")
put(out, 2, "*NODE_NAME " .. q(name))
put(out, 2, "*INHERIT_POS 0 0 0")
put(out, 2, "*INHERIT_ROT 0 0 0")
put(out, 2, "*INHERIT_SCL 0 0 0")
for r = 0, 3 do
put(out, 2, "*TM_ROW" .. r .. " " .. f(m[r * 4 + 1]) .. "\t" .. f(m[r * 4 + 2]) .. "\t" .. f(m[r * 4 + 3]))
end
local t, r, s = Mat4.decompose(m)
local w = math.clamp(r[4], -1, 1)
local angle = 2 * math.acos(w)
local sn = math.sqrt(math.max(0, 1 - w * w))
local ax = if sn > 1e-9 then { r[1] / sn, r[2] / sn, r[3] / sn } else { 0, 0, 0 }
put(out, 2, "*TM_POS " .. f(t[1]) .. "\t" .. f(t[2]) .. "\t" .. f(t[3]))
put(out, 2, "*TM_ROTAXIS " .. f(ax[1]) .. "\t" .. f(ax[2]) .. "\t" .. f(ax[3]))
put(out, 2, "*TM_ROTANGLE " .. f(angle))
put(out, 2, "*TM_SCALE " .. f(s[1]) .. "\t" .. f(s[2]) .. "\t" .. f(s[3]))
put(out, 2, "*TM_SCALEAXIS 0.0000\t0.0000\t0.0000")
put(out, 2, "*TM_SCALEAXISANG 0.0000")
put(out, 1, "}")
end
local function vec(rd: ((number, number) -> number)?, i: number, n: number, default: number): { number }
local v = {}
for c = 0, n - 1 do
v[c + 1] = if rd then rd(i, c) else default
end
return v
end
local function geom(out: Out, scene: any, mesh: any, world: { number }, matBase: { [number]: number }, hasMulti: boolean): boolean
local P: { number } = {}
local N: { number } = {}
local T: { [number]: { number } } = {}
local C: { number } = {}
local F: { number } = {}
local M: { number } = {}
local hasN, hasC = false, false
local uvSets: { [number]: boolean } = {}
for _, prim in mesh.primitives or {} do
for name in prim.attributes or {} do
local k = string.match(name, "^TEXCOORD_(%d)$")
if k then
uvSets[tonumber(k) :: number] = true
end
end
end
local nm = Mat4.normalMatrix(world)
for _, prim in mesh.primitives or {} do
local idx = tri(prim)
if idx then
local a = prim.attributes
local base = #P // 3
local rp = S.reader(a.POSITION)
local rn = if a.NORMAL then S.reader(a.NORMAL) else nil
local rc = if a.COLOR_0 then S.reader(a.COLOR_0) else nil
local cc = if a.COLOR_0 then a.COLOR_0.components else 0
if rp then
for i = 0, a.POSITION.count - 1 do
local x, y, z = Mat4.point(world, rp(i, 0), rp(i, 1), rp(i, 2))
table.insert(P, x)
table.insert(P, y)
table.insert(P, z)
local nv = vec(rn, i, 3, 0)
local tx = nm[1] * nv[1] + nm[5] * nv[2] + nm[9] * nv[3]
local ty = nm[2] * nv[1] + nm[6] * nv[2] + nm[10] * nv[3]
local tz = nm[3] * nv[1] + nm[7] * nv[2] + nm[11] * nv[3]
local len = math.sqrt(tx * tx + ty * ty + tz * tz)
if len > 0 then
tx, ty, tz = tx / len, ty / len, tz / len
end
table.insert(N, tx)
table.insert(N, ty)
table.insert(N, tz)
for k in uvSets do
local acc = a["TEXCOORD_" .. k]
local rt = if acc then S.reader(acc) else nil
local list = T[k] or {}
T[k] = list
table.insert(list, if rt then rt(i, 0) else 0)
table.insert(list, if rt then 1 - rt(i, 1) else 0)
end
local col = vec(rc, i, math.max(cc, 3), 1)
table.insert(C, col[1])
table.insert(C, col[2])
table.insert(C, col[3])
end
hasN = hasN or rn ~= nil
hasC = hasC or rc ~= nil
local mid = if prim.material and matBase[prim.material] then matBase[prim.material] else (if hasMulti then #scene.materials else 0)
for i = 1, #idx, 3 do
table.insert(F, base + idx[i])
table.insert(F, base + idx[i + 1])
table.insert(F, base + idx[i + 2])
table.insert(M, mid)
end
end
end
end
if #F == 0 then
return false
end
local nv, nf = #P // 3, #F // 3
put(out, 1, "*MESH {")
put(out, 2, "*TIMEVALUE 0")
put(out, 2, "*MESH_NUMVERTEX " .. nv)
put(out, 2, "*MESH_NUMFACES " .. nf)
put(out, 2, "*MESH_VERTEX_LIST {")
for i = 0, nv - 1 do
put(out, 3, "*MESH_VERTEX " .. i .. "\t" .. f(P[i * 3 + 1]) .. "\t" .. f(P[i * 3 + 2]) .. "\t" .. f(P[i * 3 + 3]))
end
put(out, 2, "}")
put(out, 2, "*MESH_FACE_LIST {")
for i = 0, nf - 1 do
put(out, 3, string.format("*MESH_FACE %d: A: %d B: %d C: %d AB: 1 BC: 1 CA: 1\t*MESH_SMOOTHING 1\t*MESH_MTLID %d", i, F[i * 3 + 1], F[i * 3 + 2], F[i * 3 + 3], M[i + 1]))
end
put(out, 2, "}")
local function uvBlock(d: number, list: { number })
put(out, d, "*MESH_NUMTVERTEX " .. nv)
put(out, d, "*MESH_TVERTLIST {")
for i = 0, nv - 1 do
put(out, d + 1, "*MESH_TVERT " .. i .. "\t" .. f(list[i * 2 + 1] or 0) .. "\t" .. f(list[i * 2 + 2] or 0) .. "\t0.0000")
end
put(out, d, "}")
put(out, d, "*MESH_NUMTVFACES " .. nf)
put(out, d, "*MESH_TFACELIST {")
for i = 0, nf - 1 do
put(out, d + 1, "*MESH_TFACE " .. i .. "\t" .. F[i * 3 + 1] .. "\t" .. F[i * 3 + 2] .. "\t" .. F[i * 3 + 3])
end
put(out, d, "}")
end
if T[0] then
uvBlock(2, T[0])
end
for k = 1, 7 do
if T[k] then
put(out, 2, "*MESH_MAPPINGCHANNEL " .. (k + 1) .. " {")
uvBlock(3, T[k])
put(out, 2, "}")
end
end
if hasC then
put(out, 2, "*MESH_NUMCVERTEX " .. nv)
put(out, 2, "*MESH_CVERTLIST {")
for i = 0, nv - 1 do
put(out, 3, "*MESH_VERTCOL " .. i .. "\t" .. f(C[i * 3 + 1]) .. "\t" .. f(C[i * 3 + 2]) .. "\t" .. f(C[i * 3 + 3]))
end
put(out, 2, "}")
put(out, 2, "*MESH_NUMCVFACES " .. nf)
put(out, 2, "*MESH_CFACELIST {")
for i = 0, nf - 1 do
put(out, 3, "*MESH_CFACE " .. i .. "\t" .. F[i * 3 + 1] .. "\t" .. F[i * 3 + 2] .. "\t" .. F[i * 3 + 3])
end
put(out, 2, "}")
end
if hasN then
put(out, 2, "*MESH_NORMALS {")
for i = 0, nf - 1 do
local a, b, c = F[i * 3 + 1], F[i * 3 + 2], F[i * 3 + 3]
local ux, uy, uz = P[b * 3 + 1] - P[a * 3 + 1], P[b * 3 + 2] - P[a * 3 + 2], P[b * 3 + 3] - P[a * 3 + 3]
local vx, vy, vz = P[c * 3 + 1] - P[a * 3 + 1], P[c * 3 + 2] - P[a * 3 + 2], P[c * 3 + 3] - P[a * 3 + 3]
local nx, ny, nz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
local len = math.sqrt(nx * nx + ny * ny + nz * nz)
if len > 0 then
nx, ny, nz = nx / len, ny / len, nz / len
end
put(out, 3, "*MESH_FACENORMAL " .. i .. "\t" .. f(nx) .. "\t" .. f(ny) .. "\t" .. f(nz))
for _, v in { a, b, c } do
put(out, 4, "*MESH_VERTEXNORMAL " .. v .. "\t" .. f(N[v * 3 + 1]) .. "\t" .. f(N[v * 3 + 2]) .. "\t" .. f(N[v * 3 + 3]))
end
end
put(out, 2, "}")
end
put(out, 1, "}")
return true
end
function Write.scene(scene: any, opts: any): (string?, string?)
local out: Out = { lines = {} }
local src = if type(scene.meta) == "table" and type(scene.meta.source) == "table" then scene.meta.source else {}
put(out, 0, "*3DSMAX_ASCIIEXPORT\t200")
put(out, 0, "*COMMENT " .. q(if opts and type(opts.comment) == "string" then opts.comment else "AsciiExport-Version 2,00 - rosetta"))
put(out, 0, "*SCENE {")
put(out, 1, "*SCENE_FILENAME \"\"")
put(out, 1, "*SCENE_FIRSTFRAME " .. math.floor(tonumber(src.firstFrame) or 0))
put(out, 1, "*SCENE_LASTFRAME " .. math.floor(tonumber(src.lastFrame) or 100))
put(out, 1, "*SCENE_FRAMESPEED " .. math.floor(tonumber(src.frameSpeed) or 30))
put(out, 1, "*SCENE_TICKSPERFRAME " .. math.floor(tonumber(src.ticksPerFrame) or 160))
put(out, 1, "*SCENE_BACKGROUND_STATIC 0.0000\t0.0000\t0.0000")
put(out, 1, "*SCENE_AMBIENT_STATIC 0.0000\t0.0000\t0.0000")
put(out, 0, "}")
local mats = scene.materials or {}
local nmat = #mats
local matBase: { [number]: number } = {}
-- a default sub-material for primitives without one, when other primitives have one
local needDefault = false
for _, mesh in scene.meshes or {} do
for _, prim in mesh.primitives or {} do
if not (prim.material and mats[prim.material]) then
needDefault = true
end
end
end
put(out, 0, "*MATERIAL_LIST {")
if nmat > 0 then
put(out, 1, "*MATERIAL_COUNT 1")
put(out, 1, "*MATERIAL 0 {")
put(out, 2, "*MATERIAL_NAME \"Multimaterial\"")
put(out, 2, "*MATERIAL_CLASS \"Multi/Sub-Object\"")
put(out, 2, "*NUMSUBMTLS " .. (nmat + (if needDefault then 1 else 0)))
for i, m in mats do
matBase[i] = i - 1
local mm = table.clone(m)
mm._textures = scene.textures
material(out, 2, "SUBMATERIAL", i - 1, mm)
end
if needDefault then
material(out, 2, "SUBMATERIAL", nmat, S.material("Default"))
end
put(out, 1, "}")
else
put(out, 1, "*MATERIAL_COUNT 0")
end
put(out, 0, "}")
-- world matrices and unique names
local nodes = scene.nodes or {}
local worlds: { [number]: { number } } = {}
local parentOf: { [number]: number } = {}
local order: { number } = {}
local function walk(i: number, parent: { number }, depth: number)
local node = nodes[i]
if not node or worlds[i] or depth > 256 then
return
end
worlds[i] = S.mul(parent, S.nodeMatrix(node))
table.insert(order, i)
for _, c in node.children or {} do
if nodes[c] and not worlds[c] then
parentOf[c] = i
walk(c, worlds[i], depth + 1)
end
end
end
local roots = scene.roots or {}
if #roots == 0 then
for i in nodes do
table.insert(roots, i)
end
end
for _, r in roots do
walk(r, S.IDENTITY, 0)
end
local names: { [number]: string } = {}
local used: { [string]: boolean } = {}
for _, i in order do
local base = if type(nodes[i].name) == "string" and nodes[i].name ~= "" then nodes[i].name else "Object" .. i
local name, k = base, 1
while used[name] do
k += 1
name = base .. "_" .. k
end
used[name] = true
names[i] = name
end
for _, i in order do
local node = nodes[i]
local w = worlds[i]
local mesh = if node.mesh then (scene.meshes or {})[node.mesh] else nil
local cam = if node.camera then (scene.cameras or {})[node.camera] else nil
local li = if node.light then (scene.lights or {})[node.light] else nil
local key = if mesh then "GEOMOBJECT" elseif cam then "CAMERAOBJECT" elseif li then "LIGHTOBJECT" else "HELPEROBJECT"
put(out, 0, "*" .. key .. " {")
put(out, 1, "*NODE_NAME " .. q(names[i]))
if parentOf[i] then
put(out, 1, "*NODE_PARENT " .. q(names[parentOf[i]]))
end
if key == "HELPEROBJECT" then
put(out, 1, "*HELPER_CLASS \"Dummy\"")
elseif key == "CAMERAOBJECT" then
put(out, 1, "*CAMERA_TYPE Free")
elseif key == "LIGHTOBJECT" then
put(out, 1, "*LIGHT_TYPE " .. (if li.type == "spot" then "Free" elseif li.type == "directional" then "Directional" else "Omni"))
end
nodeTm(out, names[i], w)
if mesh then
geom(out, scene, mesh, w, matBase, nmat > 0)
put(out, 1, "*PROP_MOTIONBLUR 0")
put(out, 1, "*PROP_CASTSHADOW 1")
put(out, 1, "*PROP_RECVSHADOW 1")
if nmat > 0 then
put(out, 1, "*MATERIAL_REF 0")
end
elseif cam then
put(out, 1, "*CAMERA_SETTINGS {")
put(out, 2, "*TIMEVALUE 0")
put(out, 2, "*CAMERA_NEAR " .. f(cam.znear or 0))
put(out, 2, "*CAMERA_FAR " .. f(cam.zfar or 1000))
put(out, 2, "*CAMERA_FOV " .. f(cam.yfov or math.rad(45)))
put(out, 2, "*CAMERA_TDIST 100.0000")
put(out, 1, "}")
elseif li then
local c = if type(li.color) == "table" then li.color else { 1, 1, 1 }
put(out, 1, "*LIGHT_SETTINGS {")
put(out, 2, "*TIMEVALUE 0")
put(out, 2, "*LIGHT_COLOR " .. f(c[1] or 1) .. "\t" .. f(c[2] or 1) .. "\t" .. f(c[3] or 1))
put(out, 2, "*LIGHT_INTENS " .. f(li.intensity or 1))
put(out, 2, "*LIGHT_HOTSPOT " .. f(math.deg(li.innerCone or 0) * 2))
put(out, 2, "*LIGHT_FALLOFF " .. f(math.deg(li.outerCone or math.pi / 4) * 2))
put(out, 1, "}")
end
put(out, 0, "}")
end
return table.concat(out.lines, "\n") .. "\n", nil
end
return Write
+121
View File
@@ -0,0 +1,121 @@
--!strict
-- model/ase: 3ds Max ASCII Scene Export (*3DSMAX_ASCIIEXPORT 200), read + write; checked against assimp's ASE loader.
-- Supports: geometry objects (texture channels, vertex colours, explicit normals, Multi/Sub-Object materials by
-- *MESH_MTLID), NODE_TM hierarchies, helpers, cameras, lights, material maps (paths; data through opts.resolve),
-- UTF-16 files. Not supported: *TM_ANIMATION tracks (meta.source.animated is set), shapes (nodes only), bones.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
local Bytes = require("@rosetta/core/Bytes")
local Types = require("@rosetta/Types")
local Text = require("@rosetta/codecs/text/Text")
local Parse = require("@rosetta/model/ase/Parse")
local Build = require("@rosetta/model/ase/Build")
local Write = require("@rosetta/model/ase/Write")
-- ------------------------------------------------------------------------------------------------ contract
local Ase = {
name = "ase",
category = "model",
extensions = { "ase", "ask" },
mime = { "model/x-ase", "text/plain" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/text/Text", "codecs/model/Mat4" },
caps = { decode = true, encode = true, text = true, materials = true, cameras = true, lights = true, vertexColors = true },
}
-- ------------------------------------------------------------------------------------------------ types
export type DecodeOptions = {
maxBytes: number?, -- default 2^28
maxItems: number?, -- default 2^24 (*KEY items)
maxVertices: number?, -- default 2^24 (per object)
resolve: ((string) -> buffer?)?, -- texture bitmaps by path
}
export type EncodeOptions = {
comment: string?,
}
-- ------------------------------------------------------------------------------------------------ constants
local MAGIC = "*3DSMAX_ASCIIEXPORT"
-- ------------------------------------------------------------------------------------------------ sniff
local function utf16(r: Bytes.Reader): (boolean?, boolean)
local h = r:peek(2)
if h == "\255\254" then
return true, false
elseif h == "\254\255" then
return true, true
elseif #h == 2 and string.byte(h, 2) == 0 and string.byte(h, 1) == 42 then
return true, false
elseif #h == 2 and string.byte(h, 1) == 0 and string.byte(h, 2) == 42 then
return true, true
end
return nil, false
end
local function text(r: Bytes.Reader, n: number?): string
local wide, be = utf16(r)
local len = r.limit - r.pos
if n then
len = math.min(len, n)
end
if wide then
local skip = if r:peek(2) == "\255\254" or r:peek(2) == "\254\255" then 2 else 0
return Text.utf16(r.buf, r.pos + skip, len - skip, be)
end
return Text.toUtf8(buffer.readstring(r.buf, r.pos, len))
end
function Ase.sniff(r: Bytes.Reader): number
local h = string.match(text(r, 256), "^%s*(%*[%w_]+)")
if h == MAGIC then
return 1
end
return 0
end
-- ------------------------------------------------------------------------------------------------ decode
function Ase.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
local o: any = opts or {}
if r.limit - r.pos > (o.maxBytes or 2 ^ 28) then
return nil, Err.new(Err.LIMIT, "file is over the byte limit")
end
local s = text(r)
if not string.match(s, "^%s*%*") then
return nil, Err.new(Err.CORRUPT, "not an ASE file")
end
local ok, root, msg = pcall(Parse.tree, s, o.maxItems or 2 ^ 24)
if not ok then
return nil, Err.new(Err.CORRUPT, "bad data: " .. tostring(root))
end
if not root then
return nil, Err.from(msg)
end
local ok2, scene, msg2 = pcall(Build.scene, root, o)
if not ok2 then
return nil, Err.from(tostring(scene))
end
if not scene then
return nil, Err.from(msg2)
end
if #scene.nodes == 0 and (scene.meta.source :: any).version == nil then
return nil, Err.new(Err.CORRUPT, "no *3DSMAX_ASCIIEXPORT header and no objects")
end
return scene, nil
end
-- ------------------------------------------------------------------------------------------------ encode
function Ase.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err?)
if type(scene) ~= "table" or type(scene.nodes) ~= "table" then
return nil, Err.new(Err.ARG, "needs a Scene")
end
local ok, s, msg = pcall(Write.scene, scene, opts)
if not ok then
return nil, Err.new(Err.ARG, "bad scene: " .. tostring(s))
end
if not s then
return nil, Err.new(Err.ARG, msg or "bad scene")
end
return buffer.fromstring(s), nil
end
return Ase