points pos attribute error?

31 views
Skip to first unread message

junior song

unread,
Jan 13, 2025, 9:33:51 PM1/13/25
to Glowscript Users
hello, everyone
When I try to access the pos attribute of the points object, the code throws an exception. What could be causing this issue, and how should I properly access and modify the pos attribute of the points object?

from vpython import *

# first set of points
a=points(pos=[vector(-1, 0, 0),
            vector(1, 0, 0),
            vector(0, 1, 0),
            vector(0, -1, 0)],
       color=vector(1, 0, 0),
       radius=10)
print(a.pos)

while True:
    rate(100)

here is the exception when i run the code
Traceback (most recent call last):
  File "F:\pythonProject\wave_test.py", line 10, in <module>
    print(a.pos)
  File "C:\Program Files (x86)\anaconda\lib\site-packages\vpython\vpython.py", line 2047, in pos
    raise AttributeError('object does not have a "pos" attribute')
AttributeError: object does not have a "pos" attribute



Also, when I run the following code, the program encounters an error as well
points_list = []
field = [[[0 for _ in range(3)] for _ in range(Xsize + 1)] for _ in range(Xsize + 1)]
for i in range(Xsize + 1):
    for j in range(Xsize + 1):
        points_list.append(vector(i*dx, j*dx, field[i][j][0]))

the exception is

Traceback (most recent call last):
  File "F:\pythonProject\wave.py", line 122, in <module>
    wave_points = points(pos=points_list, size=2, color=color.blue)
  File "C:\Program Files (x86)\anaconda\lib\site-packages\vpython\vpython.py", line 2093, in __init__
    super(curveMethods, self).setup(args)
  File "C:\Program Files (x86)\anaconda\lib\site-packages\vpython\vpython.py", line 723, in setup
    setattr(self, key, value)
  File "C:\Program Files (x86)\anaconda\lib\site-packages\vpython\vpython.py", line 817, in size
    if value.x == 0:
AttributeError: 'int' object has no attribute 'x'

Max DeMarr

unread,
Jan 13, 2025, 10:21:00 PM1/13/25
to Glowscript Users
For your first problem, you need to use built-in methods when dealing with reading and writing data for these curve objects. Here is a link to the documentation for the tools you can use when modifying curves like these (as points objects are treated similarly to the curve object), https://www.glowscript.org/docs/VPythonDocs/curve.html?highlight=points. A work around for what you are trying to do is simply iterating through a slice of your points like so:
print('[', end='')
for point in a.slice(0,a.npoints):
    print(point.pos, end=', ')
print(']')

For your second problem, it seems like there is more code to the problem than just the snippet you've shared, as I see something called wave_points. I also see that you are setting 'size=2' but 'size' isn't the correct attribute for the points. You need, 
  • size_units (string) – Units for size of points. Default is ‘pixels’. An alternative is ‘world’.

which is from the documentation.

Best of luck,
- Maximillian
Reply all
Reply to author
Forward
0 new messages