Vizard 8 » Tutorials & Examples » Example scripts » Viewpoint » Viewpoint camera control
8.0

Viewpoint Camera Control

\examples\viewpointCameraControl\tutorial_cameraHandler.py

This script demonstrates the view as controlled by custom camera handler class.

"""
The view is controlled by custom camera handler class.
Controls:
--Left / Right mouse buttons: move up/down.
--Mouse wheel: pan left / right.
--W / S keys: forward / back
"""
import viz
import vizact

viz.setMultiSample(4)
viz.fov(60)
viz.go()

import vizinfo
vizinfo.InfoPanel()

#Initialize world
viz.clearcolor( viz.SLATE )
ground = viz.addChild( 'ground.osgb' )

#Create custom camera handler
class MyCameraHandler( viz.CameraHandler ):

    def _camMouseDown( self, e ):
        if e.button == viz.MOUSEBUTTON_LEFT:
            #move view down
            e.view.move( [0, -1, 0] )
        elif e.button == viz.MOUSEBUTTON_RIGHT:
            #move view up
            e.view.move( [0, 1, 0] )

    def _camMouseWheel( self, e ):
        if e.dir > 0:
            #wheel rolled forward
            e.view.move( [1, 0, 0] )
        else:
            #wheel rolled backwards
            e.view.move( [-1, 0, 0] )

    def _camUpdate( self, e ):
        #Check keyboard movement
        if viz.key.isDown ('w' ):
            #move forward the amout of time in seconds sence the last call to _camUpdate
            e.view.move( [0, 0, e.elapsed * 10] )
        elif viz.key.isDown ('s' ):
            #move backward the amout of time in seconds sence the last call to _camUpdate
            e.view.move( [0, 0, -e.elapsed * 10] )

#Set camera handler
viz.cam.setHandler( MyCameraHandler() )

#Remove camera handler on spacebar (will revert to built-in camera handler)
vizact.onkeydown( ' ', viz.cam.setHandler, None )