@tool
extends EditorScenePostImport

var regular_outline_material : StandardMaterial3D
var character_outline_material : StandardMaterial3D

func _post_import(root : Node):
	regular_outline_material = ResourceLoader.load("res://assets/style/base_outline_material.tres") as StandardMaterial3D
	character_outline_material = ResourceLoader.load("res://assets/style/character_outline_material.tres") as StandardMaterial3D
	apply_outline_recursive(root)
	return root

func get_flag(node : Node, flag : String) -> bool:
	if node.name.contains(flag):
		node.name = node.name.erase(node.name.find(flag), flag.length())
		return true
	else:
		return false

func apply_outline_recursive(node : Node):
	if node != null:
		var outline : bool = not get_flag(node, "-nooutline")
		if outline and node is MeshInstance3D:
			var detail : bool = get_flag(node, "-detailoutline")
			var mesh : MeshInstance3D = (node as MeshInstance3D)
			if detail and character_outline_material:
				mesh.material_overlay = character_outline_material
			elif regular_outline_material:
				mesh.material_overlay = regular_outline_material
		for child in node.get_children():
			apply_outline_recursive(child)
