Vizard 8 » Tutorials & Examples » Picking with the mouse » Tutorial: Picked object details
8.0

Tutorial: Picked object details

Now let's place the arrow above one of the balls when they are clicked.

 

All we need to do is get the location of the ball that is clicked and place the arrow above that location. The following command will get the position of an object:

pos = object.getPosition()

Replace the print command with this statement. Next we need to move the position up a little so that the arrow appears above the ball.

 

Add the following command after you get the object's position.

pos[1] += .2

This simply increments the y value of the position by 0.2 meters. Now we set the arrows position to this new position by issuing the following command:

arrow.setPosition(pos)

Don't forget to make the arrow visible since it was initially invisible:

arrow.visible(viz.ON)

Your whole block of code should like the following:

def pickBall():
    object = viz.pick()
    if object.valid():
        pos = object.getPosition()
        pos[1] += .2
        arrow.setPosition(pos)
        arrow.visible(viz.ON)

Now run your script.

 

When you click on one of the balls the arrow will appear above it. You will notice that there is a small side effect. Try clicking on the actual arrow. It will move up 0.2 meters every time you click on it. This is because we never check if the object we picked is one of the three balls. To get around this you can do one of two things.

 

In addition to checking that the object is valid also make sure the object is not the arrow by doing the following:

if object.valid() and object != arrow:

An even better way is to disable picking on the arrow. By default all objects have picking enabled on them, meaning they can be chosen by the viz.pick() command. If you disable picking on an object then it will be ignored by the viz.pick() command.

 

After you create the arrow, perform the following command to disable picking:

arrow.disable(viz.PICKING)

Now run the script again.

 

Clicking on the arrow won't change anything, since picking is disabled on it.

 

Picking is very useful for user interaction, especially when you need the user to select an object.

Picking objects

Accessing pick information

Picked object details