How to use viewports to render two different views of the same object

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By theOtherHolmes

I have a 2D scene with two viewports, each inside side-by-side viewport containers. The viewports are showing a 3D scene (“Prism-2”), which currently has just a simple prism mesh. Attached to the prism mesh are two camera, one that shows it from the front and one that shows it from the side. I want the viewport on the left to show the front camera and the viewport on the right to show the side camera. See screenshot:

![Side-by-Side Viewports][1]

I cannot figure out how to do this. I tried the following script attached to Viewport_Side but it didn’t work:

extends Viewport

onready var camera_front = $"Prism-2/Camera_Front";
onready var camera_side = $"Prism-2/Camera_Side";

func _ready():
	camera_front.current = false;
	camera_side.current = true;

Any help?

[1]: file:///Users/brian/Dropbox/Brian/Godot%20Projects/SVGs/ViewportScreenShot.png

Per @SIsilicon, my image link isn’t working, so try this:
Side-by-Side Viewports
https://drive.google.com/file/d/1YTt8f95UM_yE8OWK6Fixx_FCSvN2DlGW/view?usp=sharing

theOtherHolmes | 2018-09-16 23:03

You know, I just realized something. The code you are showing doesn’t have the keyword tool. Which would mean that the code was not running in the editor at all.

SIsilicon | 2018-09-17 00:27

True, although it doesn’t work when I build and run the game either. But thanks for the reminder that I can test it in the editor too. I added the tool keyword. Still the same result.

theOtherHolmes | 2018-09-17 01:23

:bust_in_silhouette: Reply From: SIsilicon

Well, the answer was actually simpler than it looked.

tool
extends Viewport

var camera_front
var camera_side

func _ready():
    camera_front = $"Prism-2/Camera-front"
    camera_side = $"Prism-2/Camera-side"
    
    camera_front.current = false
    camera_side.current = true

All I did was move the variable assignments inside the _ready function. Maybe onready variables get called after the main ready function. ¯_(ツ)_/¯

Thanks. As it turns out, your tool tip (ha ha) worked AFTER I quit Godot and restarted. So I didn’t need to move the variable assignments into _ready. My code is now this, and it works as advertised:

tool
extends Viewport

onready var camera_front = $"Prism-2/Camera_Front";
onready var camera_side = $"Prism-2/Camera_Side";

func _ready():
	camera_front.current = false;
	camera_side.current = true;

(Yes, the line-ending semicolons are unnecessary, but I come from a C# background and I still use it and I don’t want to get out of the habit.)

There’s no good reason I can think of that my previous code wasn’t working in the build-and-run version of the game. But one way or the other, it’s working now. Thanks!

theOtherHolmes | 2018-09-18 12:15