model/godot + data/tres + codecs/text/GodotVariant: Godot text scenes (.tscn / .escn) read + write against Godot 4's loader, .tres resources as data

docs/STYLE.md: the layout block lists codecs/text/GodotVariant (DOCS lint).
This commit is contained in:
4DBug committed 2026-11-01 19:25:15 -06:00
1 parent 9c02ebdee5
commit 8f26e32a71
24 files changed
+3649 -5

No files matched your search

+24
View File
@@ -306,6 +306,29 @@ local xml = assert(F.saveString(p, { format = "plist" }))
assert(string.find(xml, "<key>CFBundleName</key>\n\t<string>Demo</string>", 1, true))
```
## Godot resources (.tres)
Godot text resources load as a table: `type` (the resource class), `format`, `uid`, `scriptClass`, `external` (the
`ext_resource` list: `id`, `type`, `path`, `uid`), `subresources` (`id`, `type`, `properties`) and `properties` (the
`[resource]` section, in file order). Godot values that Luau lacks are `{ type, args }` tables: `Vector3(1, 2, 3)` is
`{ type = "Vector3", args = { 1, 2, 3 } }`, likewise `Color`, `Transform3D`, `PackedFloat32Array`, `NodePath`,
`&"StringName"` (`type = "StringName"`), `SubResource("id")` and `ExtResource("id")`; `Object(Class, ...)` has `class`
and its properties in `args[1]`. Godot dictionaries keep their order and may have number keys. Godot tells integers
from floats, so integral floats read from a file (`1.0`) are written back as floats.
`F.format("tres")` and the codec `F._internal("codecs/text/GodotVariant")` (`value`, `format`, `isCtor`, `ctor`)
help with the values. Scenes (`.tscn`) are models: see [the scene guide](scene.md).
```luau
local res = assert(F.data.load(tresText))
assert(res.type == "StandardMaterial3D" and res.external[1].path == "res://brick.png")
local c = res.properties.albedo_color
assert(c.type == "Color" and c.args[1] == 0.8)
assert(res.properties.albedo_texture.type == "ExtResource")
res.properties.metallic = 0.5
local text = assert(F.saveString(res, { format = "tres" }))
assert(string.find(text, "metallic = 0.5", 1, true) and string.find(text, "roughness = 1.0", 1, true))
```
## Save and convert
Data values have no `kind`, so saving needs `opts.format` or a destination extension (an `XmlNode` document too):
@@ -341,6 +364,7 @@ is an error there), CSV needs rows, INI needs sections of scalars, JSON has no b
| cbor (`.cbor`) | yes | yes | values, buffers, Dates, tagged values |
| plist (`.plist`) | XML + binary | XML | values, buffers, Dates |
| nbt (`.nbt`, `.dat`, `.mcstructure`), snbt (`.snbt`) | yes | yes | typed values: see [Minecraft and voxels](voxel.md) |
| tres (`.tres`) | yes | yes | Godot resources: sections, Variant values as `{ type, args }` |
Full capability list: [FORMATS.md](../FORMATS.md).
+35
View File
@@ -739,6 +739,40 @@ local again = assert(F.load(F.save(scene, nil, { format = "ase" })))
assert(#again.meshes[again.nodes[2].mesh].primitives == 2)
```
## Godot scenes (TSCN / ESCN)
Godot text scenes load as a Y-up Scene (one node per `[node]`, the scene root first), checked against Godot 4's own
loader. Node3D transforms come from `transform = Transform3D(...)` (or `position` / `rotation` / `scale`); a
MeshInstance3D gets its ArrayMesh sub-resource (Godot 4.2+ surfaces, and the `"arrays"` surfaces of Godot 3 / Blender
`.escn` files) or its primitive mesh (Box, Plane, Quad, Sphere and Cylinder are built as Godot builds them), with
`surface_material_override/N` applied. StandardMaterial3D / SpatialMaterial become PBR materials (Godot's sRGB
albedo and emission converted to linear; the raw values in `material.extras.godot`); textures are `ExtResource`
paths (`res://` dropped) with their bytes through `opts.resolve`. Camera3D (`fov` is vertical) and Omni / Spot /
Directional lights map to cameras and lights (`intensity` = `light_energy`). Instanced scenes
(`instance=ExtResource(...)`) are read through `opts.resolve` and grafted under their node. Godot's front faces are
clockwise: indices are reversed into glTF's counter-clockwise winding (and back on save). Every node keeps its Godot
type and raw properties in `node.extras.godot`, so 2D trees (Node2D, Sprite2D, Control) are kept there too.
```luau
local scene = assert(F.load(tscnText, { resolve = function(path) return nil end }))
assert(scene.meta.source.format == "godot" and scene.meta.up == "y")
local box = scene.nodes[2]
assert(box.name == "Box" and box.extras.godot.type == "MeshInstance3D")
assert(box.matrix[13] == 2) -- Transform3D origin x
local mat = scene.materials[scene.meshes[box.mesh].primitives[1].material]
assert(mat.extras.godot.albedo[1] == 1 and mat.metallic == 0.5)
assert(scene.cameras[1].type == "perspective" and math.abs(math.deg(scene.cameras[1].yfov) - 60) < 1e-9)
local hud = scene.nodes[4]
assert(hud.extras.godot.type == "Node2D" and hud.extras.godot.properties.position.args[1] == 10)
local tscn = assert(F.saveString(scene, { format = "godot" })) -- Godot 4 text, format 3
assert(string.find(tscn, '[node name="Box" type="MeshInstance3D" parent="."]', 1, true))
```
The writer makes Node3D / MeshInstance3D / Camera3D / light nodes, ArrayMesh sub-resources (uncompressed format 2
surfaces with positions, normals, tangents, colours, two UV sets) and StandardMaterial3D, plus a `Texture2D`
`ext_resource` per texture with a `uri`. Skins, animations and morph targets are not written. Godot resources
(`.tres`) are data: see [data.md](data.md).
## Formats
| Format | Read | Write | Notes |
@@ -769,6 +803,7 @@ assert(#again.meshes[again.nodes[2].mesh].primitives == 2)
| STEP | via the CAD plug-in | | see [cad.md](cad.md) |
| MD2, MD3, MDL, MD5, BSP | yes | - | game models and maps: see "Game models" above |
| SMD | yes | yes | Valve studiomdl data |
| Godot `.tscn` / `.escn` | yes | yes (`.tscn`) | Node3D trees, ArrayMesh and primitive meshes, StandardMaterial3D, cameras, lights, instances; 2D nodes in extras |
| ASE (3ds Max ASCII) | yes | yes | Multi/Sub-Object materials, texture channels, vertex colours, normals, helpers, cameras, lights; no animation; Z up |
| Roblox `.mesh` (robloxmesh) | yes | yes | see [roblox.md](roblox.md) |
| Roblox `.rbxm` / `.rbxl`, `.rbxmx` / `.rbxlx` | as an `RbxTree` | yes | instance trees with typed properties: see [roblox.md](roblox.md) |