Vizard 7 » Tutorials & Examples » Example scripts » Interaction » Picking
7.6

Picking

\examples\input\picking.py

This script demonstrates how to use picking. When the mouse is over a box it will trigger an animation. All the animations are done using built-in animation engines.

"""
This script demonstrates how to use picking.
When the mouse is over a box it will trigger an animation.
All the animations are done using built-in animation engines.
"""
import viz
import vizact

viz.setMultiSample(4)
viz.go()

import vizinfo
vizinfo.InfoPanel()

#Declare some constants
GRID_WIDTH      = 10
GRID_HEIGHT     = 10
SPACING         = 1.1
MAX_SCALE       = 5.0
BOX_COLOR       = viz.RED
BOX_FADE_COLOR  = viz.WHITE
FADE_TIME       = 2.0

#Create mouse over action which will animate node fading in and shrinking
MouseOverAction = vizact.parallel(  vizact.method.scale(1.0,1.0,MAX_SCALE)
                                    ,vizact.sizeTo([1,1,1],time=FADE_TIME)
                                    ,vizact.fadeTo(BOX_COLOR,begin=BOX_FADE_COLOR,time=FADE_TIME)
                                    ,vizact.fadeTo(1,begin=0,time=FADE_TIME) )

#Create the pivot point for all the boxes
pivot = viz.addGroup()
pivot.setCenter([5,5,0])
pivot.addAction(vizact.spin(0,1,0,20,viz.FOREVER))

#Create all the box objects
for x in range(0,GRID_WIDTH*GRID_HEIGHT):
    box = viz.addChild('box.wrl',cache=viz.CACHE_COPY)
    box.setParent(pivot)
    box.specular(viz.WHITE) #Add specular color to make box look shiny
    box.setPosition([int(x/GRID_HEIGHT)*SPACING,(x % GRID_HEIGHT)*SPACING,0])

#Initialize boxes to green
pivot.color(viz.GREEN)

#Create a light that points towards the boxes
light = viz.addLight()
light.position(6,7,-5)
light.direction(0,0,1)

#Turn off specular highlights on headlight
viz.MainView.getHeadLight().specular(viz.BLACK)

def checkMouse():
    #Check if the mouse is over one of the boxes
    item = viz.pick()
    if item.valid():
        #Add mouse over action
        item.runAction(MouseOverAction)

#Create and start a timer for checking if the mouse is over a box
vizact.ontimer(0, checkMouse)

#Move the viewer back and have it look at the center of the boxes
viz.MainView.setPosition([0,2,-20])
viz.MainView.lookAt([GRID_WIDTH/2.0,GRID_HEIGHT/2.0,0])

#Disable mouse navigation
viz.mouse(viz.OFF)

#Set the background color
viz.clearcolor(viz.SLATE)