Enabling mouse control of camera in new display region

I am closing the default window openend by DirectStart and opening my own with it’s own display region. I would like enable the standard trackball camera controls in this window, but am having difficulty.

I have attempted to set up a mouseWatcher for the window, but I can’t seem to control the camera. Is there a simple way to grab the trackball data and attach a costume camera to it?

The code I am using to open the window and set up the mouse watcher is here.


from direct.directbase.DirectStart import *
from pandac.PandaModules import *


base.closeWindow(base.winList[0])
wp = WindowProperties()
newWindow = base.openWindow(props = wp)
newCamera = hidden.attachNewNode('newCam')
camNode = Camera('cam')
lens = PerspectiveLens()
camNode.setLens(lens)
newCam = newCamera.attachNewNode(camNode)
newRender = NodePath('newRender')
DR = newWindow.getActiveDisplayRegion(0)
camNode.setScene(newRender)
DR.setCamera(newCam)
smiley = loader.loadModel('models/misc/smiley')
smiley.reparentTo(newRender)
newCam.setPos(5,5,5)
newCam.lookAt(smiley)

cardMaker = CardMaker('test')
cardMaker.setFrame(-1,1,-1,1)
card = NodePath(cardMaker.generate())
card.reparentTo(newRender)


mak = base.dataRoot.attachNewNode(MouseAndKeyboard(newWindow, 0, 'mak'))
mouseWatcherNode = MouseWatcher('mouseWatcher')
mouseWatcher = mak.attachNewNode(mouseWatcherNode)

mb = mouseWatcherNode.getModifierButtons()
mb.addButton(KeyboardButton.shift())
mb.addButton(KeyboardButton.control())
mb.addButton(KeyboardButton.alt())
mouseWatcherNode.setModifierButtons(mb)

# Now we have the main trackball & drive interfaces.
# useTrackball() and useDrive() switch these in and out; only
# one is in use at a given time.
trackball = base.dataUnused.attachNewNode(Trackball('trackball'))
drive = base.dataUnused.attachNewNode(DriveInterface('drive'))
mouse2cam = base.dataUnused.attachNewNode(Transform2SG('mouse2cam'))
mouse2cam.node().setNode(newCam.node())

# The default is trackball mode, which is more convenient for
# ad-hoc development in Python using ShowBase.  Applications
# can explicitly call base.useDrive() if they prefer a drive
# interface.
mouseInterface = trackball
#self.useTrackball()

# A ButtonThrower to generate events from the mouse and
# keyboard buttons as they are pressed.
buttonThrower = mouseWatcher.attachNewNode(ButtonThrower('buttons'))
buttonThrower.node().setPrefix('seatView-')

# Specialize the events based on whether the modifier keys are
# being held down.
mods = ModifierButtons()
mods.addButton(KeyboardButton.shift())
mods.addButton(KeyboardButton.control())
mods.addButton(KeyboardButton.alt())
buttonThrower.node().setModifierButtons(mods)
mouseWatcherNode.addRegion(PGMouseWatcherBackground())

Seems like you have copied a lot of code from ShowBase. You really don’t need all that code.

If you want to open a new window and set up the trackball, use:

win = base.openWindow()
base.setupMouse(win)

Incidentally, you don’t need to (and shouldn’t) make your own camera in this case, since base.openWindow() makes a camera by default.

There’s a lot of code in ShowBase that tries to be everything for everyone. If you don’t want all of that behavior, it is possible to avoid it completely, and make your own windows, cameras, and display regions using low-level calls directly. If this is your goal, I can show you the minimum commands you need–I wouldn’t advise starting by copying the code in ShowBase.

David