How to get element number of picked object

21 views
Skip to the first unread message

Jamie Stotz

unread,
23 May 2016, 12:22:2923/05/2016
to Glowscript Users
In the rotating cubes example, the program uses 3 nested for loops to builds a list of boxes, each of which are appended to the boxes[] list.

It is then possible to set the color of a specific box with something like boxes[37].color=color.blue. If you know what box you want to set, you can refer to it by number.

Later, the program uses hit.color = color.white to set the color of the box that was clicked on. It must know the number of the box to do this.

I have a similar task, but I have the desired color in another list called colors[] that corresponds 1:1 with the boxes[] list. How do I find out the number of the box that was clicked on so I can look up the color in the other list and apply it to the box?

I want to do something like:
hit = scene.mouse.pick
myindex
= hit.index
mycolor
= colors[myindex]
hit
.color = mycolor



I know "index" is not the right property (I tried it) but I couldn't find a list of all the properties and methods of scene.mouse.pick. I saw this info, but is is confusing the way it is written.

pick Execute var obj = scene.mouse.pick to obtain the object pointed to by the mouse. If you have a box named B, you can determine whether the picked object is that box by asking if (B == obj). If there is no object pointed to by the mouse, obj is None. Also, obj will be None if the object has the pickable attribute set to False. For example, the curves, spheres, and arrows created by make_trail, attach_trail, and attach_arrow are not pickable, and you may wish to specify that some of your own objects are not pickable. At present label, helix, and extrusion cannot be picked. For curve objects, obj.segment is the number of the picked segment along the curve, starting with 1 (representing the segment from point number 0 to point number 1). You can test for a curve object with if instanceof(obj, curve):. See the GlowScript example MousePicking.

Specifically, I don't know if "var" and "obj" are actual python terms of if they are placeholders for my own variable names. That description seems to imply that I would have to test all 1000 of my boxes one at a time until I found the one that was clicked on. That can't be right.

Thanks

Bruce Sherwood

unread,
23 May 2016, 13:20:2423/05/2016
to Glowscript Users
In that program it's not that "hit" has a number in it. Rather, the mouse picking machinery sets "hit" to the computer memory address of the object that was clicked on.

A simple way to do what you want is to include in the box object your own attribute, something like this:

box(.... id=id, ....)
id += id

Then when you pick a box whose reference is "hit" you can say

hit.color = colors[hit.id]

The word "var" in the GlowScript VPython documentation is a mistake and has been deleted (it referred to a program that is written in JavaScript). So "obj" was meant to refer to your own variable, the name "hit" was used in the demo program.

Jamie Stotz

unread,
23 May 2016, 21:13:2323/05/2016
to Glowscript Users
Great explanation. I didn't realize I could add my own attributes. And I actually just stored the color info as the attribute instead of using a separate list and storing the index. But that may also be useful some day.

Thanks for your help.
Reply all
Reply to author
Forward
0 new messages