Hello! I am happy to be in this community. And i have this question. I use script to make my .fbx models textured .tscns. But when i use this code, materials are not applying. What did i miss?

extends Node


func _ready():
	import()


func textureAll(node:Node):
	print("Texturing parts...")
	if node is MeshInstance:
		node = texture(node)
	for part in node.get_children():
		textureAll(part)

func toon(mat:SpatialMaterial):
	var mat2 : SpatialMaterial = SpatialMaterial.new()
	mat2.albedo_color = Color.black
	mat2.flags_unshaded = true
	mat2.render_priority = -1
	mat2.params_grow = true
	mat2.params_grow_amount = 0.5
	mat.set_next_pass(mat2)
	return mat

func texture(node : MeshInstance):
	
	print ("Adding material to "+node.name+"...")
	var mat : SpatialMaterial = SpatialMaterial.new() 
	
	mat = toon(mat)
	
	print ("Adding textures to "+node.name+"...")
	var texPath = "res://models/textures/"
	var texFiles = get_filelist(texPath)
	for t in texFiles:
		var n = t
		print(n)
		if node.name+"_" in n && !("import" in n)&& !("meta" in n):
			var pic = load(n)
			print(n)
			if "Albedo" in n:
				mat.albedo_texture = pic
				
			elif "Height in n":
				mat.depth_enabled = true
				mat.depth_texture = t
				
			elif "Metallic" in n:
				mat.metallic = 0.5
				mat.metallic_texture = pic
				
			elif "Normal" in n:
				mat.normal_enabled = true
				mat.normal_texture = pic
				
			elif "Occlusion" in n:
				mat.ao_enabled = true
				mat.ao_light_affect = 0.5
				mat.ao_texture = pic
				
			elif "Specular" in n:
				mat.roughness = 0.5
				mat.roughness_texture = pic
				
			elif "Emission" in n:
				mat.emission_enabled = true
				mat.emission_texture = pic
		
	node.set_surface_material(0, mat)
	return node

func get_filelist(scan_dir : String) -> Array:
	var my_files : Array = []
	var dir := Directory.new()
	if dir.open(scan_dir) != OK:
		printerr("Warning: could not open directory: ", scan_dir)
		return []
	
	if dir.list_dir_begin(true, true) != OK:
		printerr("Warning: could not list contents of: ", scan_dir)
		return []
	
	var file_name := dir.get_next()
	while file_name != "":
		if dir.current_is_dir():
			my_files += get_filelist(dir.get_current_dir() + "/" + file_name)
		else:
			my_files.append(dir.get_current_dir() + "/" + file_name)
	
		file_name = dir.get_next()
	
	return my_files

func import():
	for i in get_parent().get_node("fbx").get_children():
		print("Importing "+i.name+"...")
		var my_scene : PackedScene = PackedScene.new()
		var result = my_scene.pack(i)
		
		print(result)
		if result == OK:
			print("Texturing scene or model...")
			
			
			print ("Saving object in resources...")
			var p = "res://models/tscn/"+i.name+".tscn"


			ResourceSaver.save(p, my_scene)
			var ready_scene = load(p).instance()
			textureAll(ready_scene)
			print("Save textures...")
			
			print("Adding to scene...")
			get_parent().get_node("tscn").add_child(ready_scene)

			my_scene.pack(ready_scene)
			p="res://models/tscn/textured_models/"+i.name+".tscn"
			printerr(ResourceSaver.save(p, my_scene))
19 days later

Sorry, I don't know how to use FBX and how to deal with in code, but, can't you use DAE (Collada)? Do you need to apply texture to them by code? Why not importing, dealing with it in the inspector, and only instantiate by code?

3 years later