Search code examples
texturesmeshgodot

How to set the texture of a spatial material on a mesh instance in gd script in godot


I have a mesh instance and I want to have a function that takes in a file path of a png file and adds it to spatial material and then adds the material to the mesh instance. I tried using the following:

var mat = SpatialMaterial.new() 
var tex = Texture.new() 
tex.resource_path = "res://icon.png" 
mat.albedo_texture = tex 
mesh.material_override = mat

but it returned this error on line 3: Invalid set index 'resource_path' (on base: 'Nil') with value of type 'String'.

Thanks in advance if you can help.


Solution

  • I ended up figuring it out. I just needed to use the load function. The following code works:

    var mat = SpatialMaterial.new() 
    var tex = load("res://icon.png")
    mat.albedo_texture = tex 
    mesh.material_override = mat