How to keep Surface Material Override didn't change

Godot Version

4.2.2

Question

I made a model and input it into the engine, then set all the Surface Material Override slots. But when I re-input the model, all of the Surface Material Override slots clear automatically. I have to set all the material again whenever I adjust the model. How to keep it didn’t change automatically?


There are six materials I have to set up whenever I re-input it.

the materials override are part of the MeshInstance3D node not the Mesh Resource. If you want to keep the materials between MeshInstance3D nodes you’ll need to set them in the Mesh Resource itself.

If the Mesh is part of an imported scene (glb, gltf, dae,… file) then you have two options:

@tool
extends EditorScenePostImport


func _post_import(scene):
	iterate(scene)
	return scene


func iterate(node):
	if node != null:
		if node is MeshInstance3D:
			# Override the materials of the mesh with a custom material
			var mesh:Mesh = node.mesh
			for i in mesh.get_surface_count():
				mesh.surface_set_material(i, preload("res://assets/kaykit dungeon/dungeon_material.tres"))

		# Keep iterating
		for child in node.get_children():
			iterate(child)
1 Like