Multiple camera

Hello, I’m new to UPBGE (but not to Blender and python scripting).
In my project I need to start with an empty scene and populate it from scratch at runtime via python script. My scene will contain possibly multiple cameras, and I need to switch between them at runtime.

So the question is: is it possible to have multiple cameras in the scene and set which one isused for rendering?

Alternatively, is it possible to have more cameras “activated” and modify the rendering viewport of each one to have views from multiple cameras at the same time on screen?

I will be deploying the result as a standalone executable.

EDIT: i’m using UPBGE v0.3.0 since I need EEVEE support

Tested on legacy 2.7x but it should be similar on the new API.

from bge.logic import getCurrentController
own = getCurrentController().owner

scene = own.scene
scene.active_camera = scene.objects["Camera.001"]

You may want too keep a list of object references to different cameras to avoid having to look them up by name. Managing such a list is eh, bit of another topic.


Woah, that scene.active_camera would be usefull for me, thank you

About multiple viewports, it has been restored recently.

You’d need such a script:

import bge

scene = bge.logic.getCurrentScene()

cam1 = scene.objects["cam1"]
cam2 = scene.objects["cam2"]

cam1.useViewport = True
cam2.useViewport = True

width = bge.render.getWindowWidth()
height = bge.render.getWindowHeight()

# Try to do a vertical split of the view (setViewport(left, bottom, right, top))
cam1.setViewport(0, 0, int(width / 2), height)
cam2.setViewport(int(width / 2), 0, width, height)

I test and edit with a .blend

EDIT: viewports.blend (835.2 KB)

1 Like

Tested on UPBGE 0.30.0 and I confirm it works as expected. Thanks!

Thanks, I tested this as well and works as expected. However after testing both this and the scene.active_camera flag I think i will proceed with the other method

Hmmm, this could be really useful for arcade/console like multiplayer games.

I will download UPBGE 0.30.0 to test it : D