Vizard 7 » Reference » Tools » Grabber
7.7

Grabber

A tool for grabbing objects in the scene. The following command creates a Grabber object:

Command

Description

grabber.Grabber(usingPhysics=True,

                       usingSprings=True,

                       highlightMode=highlighter.MODE_OUTLINE)                 

usingPhysics: This should be set to True if used in an environment with physics enabled. When set to True, an invisible collide sphere at the grabber location is used for detecting intersections. Otherwise, a distance test is used for detecting intersections.

usingSprings: Specifies whether or not springs are used for grabbing.

highlightMode: Can be highlighter.MODE_ARROW, highlighter.MODE_OUTLINE, highlighter.MODE_BOX

The following methods are available on the Grabber object:

Method

Description

<Grabber>.grab()

Activates the grabber and grabs the nearest intersecting object.

<Grabber>.grabAndHold() Activates the grabber and grabs the nearest intersecting object. The command should be called on subsequent frames to hold the object.

<Grabber>.getIntersection()

Returns the nearest intersecting object.

<Grabber>.release()

Releases the grabbed object.

<Grabber>.addItems(items)

Adds objects to the grab list.

items: a list of Vizard nodes.

<Grabber>.setItems(items)

Sets which objects in the scene the grabber is allowed to grab.

items: a list of Vizard nodes.

<Grabber>.removeItems(items)

Removes objects from the grab list.

items: a list of Vizard nodes.

<Grabber>.remove()

Releases any object grabbed and removes the grabber.

<Grabber>.setUpdateFunction(updateFunction)

Sets the update function that gets called every frame. The update function calls methods on the tool based on the state of input devices.

The following events will get triggered when a grab, release, or intersection occurs:

Event

Description

grabber.GRAB_EVENT

This event is generated when an object is grabbed. It provides a single event structure e with the following attributes:

e.object: The grabber object

e.grabbed: The object grabbed

grabber.RELEASE_EVENT

This event is generated when an object is released. It provides a single event structure e with the following attributes:

e.object: The grabber object

e.released: The object released

grabber.UPDATE_INTERSECTION_EVENT

This event is generated when the object intersecting the grabber changes. It provides a single event structure e with the following attributes:

e.object: The grabber object

e.new: The object currently intersecting the grabber. Returns None if there is no object.

e.old: The object  that previously intersected with the grabber. Returns None if there was no object.

Example

""" 
Mouse movements move the arrow left,right,up,down. 
Scroll wheel moves the arrow forward,back. 
Left mouse button grabs,releases. 
""" 

import viz
import vizshape
import vizact
import vizinfo
vizinfo.InfoPanel(align=viz.ALIGN_LEFT_BOTTOM)

viz.setOption('viz.display.stencil',1)

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

environment = viz.addChild('sky_day.osgb')
soccerball = viz.addChild('soccerball.osgb',pos=[-0.5,1.8,1.5])
basketball = viz.addChild('basketball.osgb',pos=[0,1.8,1.5])
volleyball = viz.addChild('volleyball.osgb',pos=[0.5,1.8,1.5])

#Initialize the Grabber and items that can be grabbed
#Change hightlight mode from default outline to box
usingPhysics=False
from tools import grabber
from tools import highlighter
tool = grabber.Grabber(usingPhysics=usingPhysics, usingSprings=usingPhysics, highlightMode=highlighter.MODE_BOX)
tool.setItems([soccerball,basketball,volleyball])

# update code for grabber
def updateGrabber(tool):
    state = viz.mouse.getState()
    if state & viz. MOUSEBUTTON_LEFT:
        tool.grabAndHold()
tool.setUpdateFunction(updateGrabber)

#Link the grabber to an arrow in order to
#visualize it's position
from vizconnect.util import virtual_trackers
mouseTracker = virtual_trackers.ScrollWheel(followMouse = True)
mouseTracker.distance = 1.4
arrow = vizshape.addArrow(length=0.2,color=viz.BLUE)
arrowLink = viz.link(mouseTracker,arrow)
arrowLink.postMultLinkable(viz.MainView)
viz.link(arrowLink,tool)

spin = vizact.spin(0,1,0,30,2)

#Add a spin action to the ball when its released
def onRelease(e):
    e.released.runAction(spin)
    
viz.callback(grabber.RELEASE_EVENT,onRelease)

#Disable mouse navigation and hide the mouse curser
viz.mouse(viz.OFF)
viz.mouse.setVisible(viz.OFF)