AttributeError: 'vector' object has no attribute 'add'

49 views
Skip to first unread message

Francisco Gracia

unread,
May 1, 2016, 6:04:39 PM5/1/16
to Glowscript Users
I am trying to use some of the demo programs kindly provided with *VPython*, some of which I know since many years ago, in *Jupyter Vpython* (over *Firefox*), tuning them as required. I am dealing now with *crossproduct.py* and have managed to make it work as expected in most aspects. But I have hit on a problem that I have not been able to solve until now. One of the main operations that the user can perform in *crossproduct* consists in modifying the characteristics of one of the implied vectors by way of pointing over it and dragging it. As soon as such a move is intended in my case, the following error springs up:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-ff02cdeb3911> in <module>()
   
297     if drag:
   
298         newobs = scene.mouse.project( normal = vector(1, 0, 0),
--> 299                                       d = 0
   
300                                      )
   
301         if newobs and (newobs != obs):

C
:\Users\Paco\Anaconda3\lib\site-packages\vpython\vpython.py in project(self, **args)
   
2341         ndr = dot(normal, self._ray)
   
2342         t = -ndc/ndr
-> 2343         return self._canvas.camera.pos.add(self._ray.multiply(t))
   
2344
   
2345 class Camera(object):

As this leads one to the deepest internals of *vpython*, I have tried to study the source code for the *scene.mouse.project()* function, but to no avail, probably because I do not understand in the first place what the instruction in line 298 is trying to perform.

Can any hint be provided?

Many thanks

Francisco


Bruce Sherwood

unread,
May 1, 2016, 8:38:06 PM5/1/16
to Glowscript Users
Thanks for reporting this problem. The first thing to say is that apparently we never tested the Jupyter VPython version of scene.mouse.project(). The last lines of the project function should read as follows:

        ndc = dot(normal, self._canvas.camera.pos) - dist
        ndr = dot(normal, self._ray)
        if ndr == 0: return None
        t = -ndc/ndr
        return self._canvas.camera.pos + t*self._ray

Second, here is an extract from the Help at glowscript.org that explains what scene.mouse.project() does, which is the same as in Classic VPython:

Projecting mouse position onto a given plane


Here is a way to get the mouse position relative to a particular plane in space:


temp = scene.mouse.project(
             normal=vec(0,1,0),
             point=vec(0,3,0) )
# None if no intersection with plane:
if temp != None) ball.pos = temp


This projects the mouse cursor onto a plane that is perpendicular to the specified normal. If the second parameter is not specified, the plane passes through the origin. It returns a 3D position, or None if the projection of the mouse misses the plane (scene.mouse.ray is parallel to the plane).


In the example shown above, the user of your program will be able to use the mouse to place balls in a plane parallel to the xy plane, a height of 3 above the xy plane, no matter how the user has rotated the point of view.

You can instead specify a perpendicular distance from the origin to the plane that is perpendicular to the specified normal. The example above is equivalent to


temp=scene.mouse.project(normal=vec(0,1,0), d=3 )


Third, there is a GlowScript VPython version of the cross product program that works:


Fourth, when I try to run this code in Jupyter VPython it sort of works but when dragging things often stop changing despite not having released the mouse button. I tried a variety of probes to debug the problem, but whenever I put in a probe (such as for example a label object that displays variables), the program works properly. Clearly something is wrong, but just as clearly it's not obvious what.

 
Reply all
Reply to author
Forward
0 new messages