How can I instance a PackedScene, containing Meshes, and set all meshes material to a new material?

Godot Version

4.2

Question

Hi,

How can I instance a PackedScene, containing Meshes, and set all meshes material to a new material ?

I want to make the PackedScene meshes have a blueprint effect

Something like this? I haven’t tested the code.

# Load your PackedScene

var instance = load("res://path_to_your_scene.tscn").instantiate()

# Create a new material

var new_material = StandardMaterial3D.new()

# Set the properties of the new material here

# For example, to make it blue:

new_material.albedo_color = Color.blue

# Get all the MeshInstance3D and MultiMeshInstance3D children of the instance

var mesh_children = instance.find_children("*", "MeshInstance3D") + instance.find_children("*", "MultiMeshInstance3D")

# Set the material of each mesh to the new material

for child in mesh_children:
    for surface_i in child.get_surface_count():
        child.set_surface_material(surface_i, new_material)