Easy way to access all materials from imported 3D scene with Script?

Hi all.
I’ve been playing around with importing fbx files from Synty. I’ve found that I’ve needed to set the material for each one, connecting them all to the same material. I’ve managed to make a nice script that runs when importing a model to make sure that in most cases, when there’s just a single mesh, props can share the same material, but some props are made up of multiple 3D items.

When you double click on a prop, if you click on the “materials” tab, you can see all the materials in a scene. Is there a way of doing this easily in script? Or is it what I suspect, that you need to go through the entire tree of the scene, check for nodes that are meshes and then check all of their materials?

Thanks all!

I’m guessing you are using a a EditorScenePostImport script.

You can override the material for each Mesh surface like:

@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)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.