To modify every mesh item in the scene to make the material local to the scene, you can use the @tool script in Godot 4. Below is an example of a simple script that you can use:
gdscript
# This script should be attached to an empty node that is a child of the scene root.
# Make sure the script has the '@tool' decorator to make it a tool script.
@tool
extends Node
var materials_to_update = []
# Connect to the ready signal to be notified when the node is ready.
func _ready():
update_materials()
# This function will update all the materials to be local to the scene.
func update_materials():
get_children().each(lambda child: update_material(child))
# Recursively update the materials of the given child node.
func update_material(node):
if node.is_class('MeshInstance'):
var mesh_instance = node as MeshInstance
var material = mesh_instance.material
if material != null:
material.make_local_to_scene()
materials_to_update.append(material)
# Recursively update the materials of the child node's children.
node.get_children().each(update_material)
# Connect to the scene changed signal to update materials when the scene is changed.
onready var scene_changed_signal = Engine.get_scene_changed_signal()
onready scene_changed_signal.connect(self, '_scene_changed')
# This function will be called when the scene is changed.
func _scene_changed():
materials_to_update.clear()
update_materials()
# Clean up the materials when the node is destroyed.
func _on_node_removed():
materials_to_update.each(lambda material: material.make_global())
This script will find all the MeshInstance nodes in the scene and make their materials local to the scene. It also connects to the scene_changed signal to update the materials when the scene is reloaded. When the node that has this script attached to it is removed from the scene, it will make sure to clean up the materials by making them global again.
Please note that this script assumes that the materials you want to make local are already in the scene and are not being imported dynamically. If you are using dynamic loading of assets, you will need to adjust the script to handle those cases.