Vizard 8 » Tutorials & Examples » Picking with the mouse » Tutorial: Accessing pick information
8.0

Tutorial: Accessing pick information

Now we need to detect when the user clicks on a ball. The first thing you must do is add a callback to your script for mouse clicks.

Add the following code to the end of your script:

def pickBall():
    print('mouse clicked')

vizact.onmousedown(viz.MOUSEBUTTON_LEFT, pickBall)

This simply creates a function that Vizard will call every time the user clicks the left mouse button.  The first argument in vizact.onmousedown can be either viz.MOUSEBUTTON_LEFT, viz.MOUSEBUTTON_RIGHT, or viz.MOUSEBUTTON_MIDDLE.

 

If you run your script now and click a mouse button inside of it you will notice that the output window will print out 'mouse clicked'. You may also notice that when we click the mouse the viewpoint jumps around.

 

Since we don't plan on moving the viewpoint in this tutorial, let's disable mouse navigation. Add this line of code to your script:

viz.mouse(viz.OFF)

This will stop the mouse from moving the viewpoint.

 

Now let's detect when the user has clicked on a ball using the left mouse button. Replace the print statement in your pickBall function with the following code:

object = viz.pick()

Note: When replacing the print statement with above code, make sure that the indentation for the copy-pasted code is at the indentation level as the print statement.

Python uses indentation as a structural element of the language. Make sure that the indentation is consistently either tabs or spaces, not mixed. You can make the tabs / whitespace visible in Vizard with View >Whitespace.

The viz.pick() command detects which object the mouse is currently over and returns it.

 

Once you have the object you must make sure it is valid, since the mouse might not be over any object.

 

Add the following line of code after the viz.pick() command at the same level of indentation:

if object.valid():
    print('Clicked on an object')

Now run the script again. When you click on one of the balls the output window will print "Clicked on an object".

Picking objects

Accessing pick information

Picked object details