Here's a simple script that should do what you're asking for:
@tool
extends EditorScript
# This function will be called when the tool is run
func _run():
# Get the selected nodes
var selected_nodes = get_editor_interface().get_selection()
# Iterate through the selected nodes
for node in selected_nodes:
# Check if the node is a MeshInstance or a SpatialMeshInstance
if node is MeshInstance or node is SpatialMeshInstance:
# Get the mesh from the node
var mesh = node.get_mesh()
# Create a new mesh and copy the data from the old mesh
var new_mesh = Mesh.new()
new_mesh.surface_get_array(Mesh.ARRAY_VERTEX, 0, mesh.surface_get_vertex_count(), mesh)
new_mesh.surface_set_material(0, node.get_material(0))
# Set the new mesh as the node's mesh
node.set_mesh(new_mesh)
This script will iterate through all the selected nodes in the scene, and if the node is a MeshInstance or a SpatialMeshInstance, it will create a new mesh and copy the data from the old mesh. It will then set the material of the new mesh to the material of the old mesh, and set the new mesh as the node's mesh. This will make the material local to the scene.
Keep in mind that this script will not work if the material is already local to the scene, or if the material is shared between multiple nodes.
Also, this script is not perfect and it's not handling the case where the mesh instance have multiple materials. If you have this case, you should adapt the script accordingly.
I hope this helps! Let me know if you have any questions.
EDIT:
If you want to make this script more robust, you could add some error checking to make sure that the selected nodes are valid, and that they have a mesh and a material. Here's an example of how you could do that:
@tool
extends EditorScript
# This function will be called when the tool is run
func _run():
# Get the selected nodes
var selected_nodes = get_editor_interface().get_selection()
# Iterate through the selected nodes
for node in selected_nodes:
# Check if the node is a MeshInstance or a SpatialMeshInstance
if not (node is MeshInstance or node is SpatialMeshInstance):
continue
# Check if the node has a mesh
var mesh = node.get_mesh()
if not mesh:
continue
# Check if the node has a material
var material = node.get_material(0)
if not material:
continue
# Create a new mesh and copy the data from the old mesh
var new_mesh = Mesh.new()
new_mesh.surface_get_array(Mesh.ARRAY_VERTEX, 0, mesh.surface_get_vertex_count(), mesh)
new_mesh.surface_set_material(0, material)
# Set the new mesh as the node's mesh
node.set_mesh(new_mesh)
This way, the script will only modify nodes that have a mesh and a material, and it will skip over any nodes that don't. This will help prevent errors and make the script more robust.
I hope this helps! Let me know if you have any questions.