Switching a Subviewport view to the main Viewport

Godot Version

Godot 4.3

Question

I have a scene where, within a main viewport, I have multiple subviewports rendered onto mesh planes, and I want to “possess” the camera within these Subviewports, so that they become the main view.

I have tried setting the vairous cameras to current, but as the cameras are all tied to whatever Viewport they are under this has no effect.

I basically want to switch what is considered the “main” viewport - any ideas would be much appreciated :folded_hands:

1 Like

Is your node hierarchy set up in a way where you could move subtrees around? Something like:

root
  main view (viewport)
    cam1 (node3d)
      [stuff]
  sub view 1 (viewport)
    cam2 (node3d)
      [stuff]
  sub view 2 (viewport)
    cam3 (node3d)
      [stuff]
  sub view 3 (viewport)
    cam4 (node3d)
      [stuff]
  [...]

The camN nodes would be Node3D or Node2D depending on your needs, and would contain all the nodes for whatever they were looking at; cameras, sprites, whatever. You could then (say) move cam4 to the main view by removing cam1 from main view, removing cam4 from sub view 3, adding cam4 to main view, and adding cam1 to sub view 3.

Basically, keeping the contents of the subviewport in a bag so it’s easy to remove_child() and add_child() whole chunks of the node hierarchy to swap things around.

1 Like

Thank you, this solution works well… for those wondering I encapsulated the basic logic into this function that takes a “view” (ie the camN nodes in the above answer):

# run in a script on the root node, so "self" = root
func switch_view(view: Node3D):
	self.remove_child(self.get_children()[0])
	if view.get_parent():
		view.get_parent().remove_child(view)
	self.add_child(view)

(This code is very similar to the Godot docs on add_child())
Many thanks!

1 Like