Need help with Subviewport

Godot Version

4.2.1

Question

Guys, for the life of me, I cant get the subviewport to render its view to a texture programatically. I have a quad mesh (my_plane) which I want to display a subviewport’s view. The subviewport is all set up properly with a child camera, layers, cull masks, you name it.
It all works well when I set it up in the editor but when I try and replicate the setup in gdscript, i get the empty (pink) texture on my_plane.

My code is:

var texture = ViewportTexture.new()
texture.viewport_path = my_subviewport_node
my_plane.get_active_material(0).albedo_texture = texture

What am I missing? The subviewport exists in the scene and it’s all set up properly. It works fine when I assign it as viewport path manually in the inspector, but the above doesn’t work…

texture.viewport_path = my_subviewport_node.get_path()

Try getting the path of the node

that’s how I’m getting the path, otherwise the game wouldn’t run. I does get the subviewport but not the texture.

I have the same problem! I found this:
viewport.GetTexture()
which apparently can get the texture directly?
using it like so (with a custom shader)
thismat.SetShaderParameter("shader_parameter/albedo", viewport.GetTexture());

I managed to get it to work once, but then I changed my code again for something else and then impossible to make it work again. (I wonder if I had gotten it working actually >.>)

When I try to manually set the viewport texture during gameplay, with the remote hierarchy, I get a “Viewport Texture must be set to use it.” error?

I found it, at least for custom shaders!

thismat.Set(“shader_parameter/viewTexture”, viewport.GetTexture());