PDA

View Full Version : PickRect code snippet


Gladsomebeast
01-18-2012, 11:19 AM
Example of window.pickRect that I though could be handy for other folks out in vizland:

import viz
import vizact

viz.go()

model = viz.add("mini.osg")
viz.MainView.setPosition([0, 1.2, -5])

viz.mouse.setOverride(viz.ON)
vizact.onkeydown(' ', viz.mouse.setOverride, viz.TOGGLE)

print model.getNodeNames()
parts = []
for name in model.getNodeNames():
part = model.getChild(name)
part.nodeName = name
parts.append(part)


#Rectangle Pick
viz.startLayer(viz.LINE_LOOP)
viz.vertex([0, 0, 0])
viz.vertex([0, 0, 0])
viz.vertex([0, 0, 0])
viz.vertex([0, 0, 0])
box = viz.endLayer(parent=viz.SCREEN)
box.alpha(.8)

firstPoint = [0,0]
def startPickRect():
global firstPoint
firstPoint = viz.mouse.getPosition()
box.setVertex(0, firstPoint+[0])
vizact.onmousedown(viz.MOUSEBUTTON_LEFT, startPickRect)

def updateBoxVisual():
if viz.mouse.getState() & viz. MOUSEBUTTON_LEFT:
mouseScreenPos = viz.mouse.getPosition()
box.setVertex(1, [mouseScreenPos[0],firstPoint[1],0])
box.setVertex(2, mouseScreenPos+[0])
box.setVertex(3, [firstPoint[0],mouseScreenPos[1],0])
vizact.onupdate(viz.PRIORITY_DEFAULT, updateBoxVisual)

def endPickRect():
mouseScreenPos = viz.mouse.getPosition()
#update box visual
box.setVertex(1, [mouseScreenPos[0],firstPoint[1],0])
box.setVertex(2, mouseScreenPos+[0])
box.setVertex(3, [firstPoint[0],mouseScreenPos[1],0])

rect = firstPoint + mouseScreenPos
# Get all nodes within the rectangle
picked = viz.MainWindow.pickRect(rect)
for node in picked:
print node.nodeName
vizact.onmouseup(viz.MOUSEBUTTON_LEFT, endPickRect)