Files
2026-11-01 19:25:15 -06:00

81 lines
2.9 KiB
GDScript

# tools/fixtures/_godot_dump.gd: run by tools/fixtures/godot.py (godot --headless -s ... -- <scene.tscn>...): loads
# each scene with Godot's own loader and prints one JSON line per scene: every node (path, class, global transform as
# 12 numbers: basis rows then origin, visibility), every MeshInstance3D's surfaces (mesh.surface_get_arrays: vertex,
# normal, tangent, uv, uv2, color, index; the active material's albedo / metallic / roughness / emission / texture
# path / transparency / cull mode), cameras and lights.
extends SceneTree
func xf(t):
var b = t.basis
return [b.x.x, b.y.x, b.z.x, b.x.y, b.y.y, b.z.y, b.x.z, b.y.z, b.z.z, t.origin.x, t.origin.y, t.origin.z]
func flat(a):
var out = []
if a == null:
return out
for v in a:
if v is Vector3:
out.append_array([v.x, v.y, v.z])
elif v is Vector2:
out.append_array([v.x, v.y])
elif v is Color:
out.append_array([v.r, v.g, v.b, v.a])
else:
out.append(v)
return out
func mat(m):
if m == null:
return null
var d = {"class": m.get_class(), "name": m.resource_name}
if m is BaseMaterial3D:
d["albedo"] = [m.albedo_color.r, m.albedo_color.g, m.albedo_color.b, m.albedo_color.a]
d["metallic"] = m.metallic
d["roughness"] = m.roughness
d["emission"] = [m.emission.r, m.emission.g, m.emission.b] if m.emission_enabled else null
d["emission_energy"] = m.emission_energy_multiplier
d["texture"] = m.albedo_texture.resource_path if m.albedo_texture else null
d["transparency"] = m.transparency
d["cull"] = m.cull_mode
return d
func walk(n, root, out):
var e = {"path": str(root.get_path_to(n)), "class": n.get_class()}
if n is Node3D:
e["xf"] = xf(n.transform)
e["visible"] = n.visible
if n is MeshInstance3D and n.mesh:
var surfs = []
for s in n.mesh.get_surface_count():
var a = n.mesh.surface_get_arrays(s)
surfs.append({
"vertex": flat(a[Mesh.ARRAY_VERTEX]), "normal": flat(a[Mesh.ARRAY_NORMAL]), "tangent": flat(a[Mesh.ARRAY_TANGENT]),
"uv": flat(a[Mesh.ARRAY_TEX_UV]), "uv2": flat(a[Mesh.ARRAY_TEX_UV2]), "color": flat(a[Mesh.ARRAY_COLOR]),
"index": flat(a[Mesh.ARRAY_INDEX]), "material": mat(n.get_active_material(s)),
})
e["surfaces"] = surfs
e["mesh_class"] = n.mesh.get_class()
if n is Camera3D:
e["camera"] = {"fov": n.fov, "near": n.near, "far": n.far, "projection": n.projection, "size": n.size}
if n is Light3D:
var l = {"color": [n.light_color.r, n.light_color.g, n.light_color.b], "energy": n.light_energy}
if n is OmniLight3D:
l["range"] = n.omni_range
if n is SpotLight3D:
l["range"] = n.spot_range
l["angle"] = n.spot_angle
e["light"] = l
out.append(e)
for c in n.get_children():
walk(c, root, out)
func _init():
for path in OS.get_cmdline_user_args():
var ps = load(path)
var res = {"file": path, "ok": ps != null, "nodes": []}
if ps:
var root = ps.instantiate()
walk(root, root, res["nodes"])
print("JSON:" + JSON.stringify(res))
quit()