# Ciclo 83 (i20): para cada res://v/<variante>/modelo.fbx importado, dice si cada malla trae textura de color (albedo).
# Uso: godot --headless --path proyecto --script res://medir_texturas.gd
extends SceneTree

func buscar(n: Node, out: Array) -> void:
	if n is MeshInstance3D and n.mesh != null:
		var info := {"nodo": str(n.name), "superficies": []}
		for s in range(n.mesh.get_surface_count()):
			var m = n.get_active_material(s)
			var d := {"material": "", "clase": "", "textura_albedo": false, "ruta_textura": ""}
			if m != null:
				d["material"] = str(m.resource_name)
				d["clase"] = m.get_class()
				if m is BaseMaterial3D and m.albedo_texture != null:
					d["textura_albedo"] = true
					d["ruta_textura"] = str(m.albedo_texture.resource_path)
					d["tam_textura"] = [m.albedo_texture.get_width(), m.albedo_texture.get_height()]
			info["superficies"].append(d)
		out.append(info)
	for h in n.get_children():
		buscar(h, out)

func _initialize() -> void:
	var res := {"godot": Engine.get_version_info()["string"], "variantes": {}}
	var dir := DirAccess.open("res://v")
	var vs := Array(dir.get_directories())
	vs.sort()
	for v in vs:
		var ruta: String = "res://v/" + str(v) + "/modelo.fbx"
		var ps = load(ruta)
		if ps == null:
			res["variantes"][v] = {"error": "no cargó"}
			continue
		var inst: Node = ps.instantiate()
		var out := []
		buscar(inst, out)
		res["variantes"][v] = out
		inst.free()
	var f := FileAccess.open("res://medicion_texturas.json", FileAccess.WRITE)
	f.store_string(JSON.stringify(res, " "))
	f.close()
	print("MEDIDO_TEXTURAS ", res["godot"], " ", vs.size())
	quit()
