Why does this work (project from Godot Asset Library)

Godot Version

Godot stable v4.3

Question

I’ve downloaded a project called 2D in 3D Viewport Demo from the Godot Asset Library. It renders a pong game in a SubViewport, and sets its texture to a MeshInstance3D:

func _ready():
	# Clear the viewport.
	var viewport = $SubViewport
	$SubViewport.set_clear_mode(SubViewport.CLEAR_MODE_ONCE)

	# Retrieve the texture and set it to the viewport quad.
	$ViewportQuad.material_override.albedo_texture = viewport.get_texture()

The Godot Documentation has no mention to the property albedo_texture of Material type property material_override. When I try to reproduce this in my code, I get an error:

Invalid Assignment of property or key 'albedo_texture' with value of type 'ViewportTexture' on a base object of type 'null instance'.

My actual code:

func _ready() -> void:
	$Player/MeshInstance3D.material_override.albedo_texture = $SubViewport.get_texture()

What’s happening? Why does it only work in the demo project?

I think it’s saying your mesh instance or material override is null.

My guess is that you didn’t create a new material for the override

That means your path to get the node (the $Player/MeshInstance3D) is wrong and returning null, can you show a print of your scene organization?

@matheusmdx
That means your path to get the node (the $Player/MeshInstance3D) is wrong and returning null

The path to the node is correct. I can show a print of my scene organization, but when I print($Player/MeshInstance3D.mesh), it did log: <PlaneMesh#-9223372011907054312>.
What’s returning null is the $Player/MeshInstance3D.material_override, because when I try to print it I get null in console.

@pennyloafers
My guess is that you didn’t create a new material for the override

The Player node is an instance from another scene. Inside the Player node (a CharacterBody3D) there’s a MeshInstance3D, and inside of it in the Inspector panel, I did my best to replicate what’s in the Godot Asset: I made a “New PlaneMesh” in Mesh, a “New StandardMaterial3D” in the Surface Material Override, and changed its Metallic → Specular to 0. Did I overlook anything else?

Ok, I found where I did a mistake. I was supposed to create a New StandardMaterial3D inside material_override in Geometry group, not in the Surface Material Override. Calling $Player/MeshInstance3D.get_active_material(0) solved the problem so I investigated it better. Thanks for helping.

1 Like