program crashes with 'list' object has no attribute 'visible' error

43 views
Skip to first unread message

Joe Heafner

unread,
May 16, 2024, 6:20:18 PMMay 16
to Users VPython
I'm running VPython 7.6.5 in a pristine Anaconda environment with Python 3.12.3 and Jupyter Notebook 7.2.0 on macOS Sonoma and Safari with all updates applied. The excerpt is from a larger program that still runs correctly in Web VPython. In Jupyter Notebook, the program crashes in the ToggleVis() function with the following error message:

AttributeError: 'list' object has no attribute 'visible'

with the apparent culprit being something.visble in the ToggleVis() function. I suspect the problem may be with Python 3.12.3 but I haven't tested against another Python version.

Here is the MWE (minimal working example):

from vpython import *

def MakeCoordAxesAt(ctr=vector(0,0,0),xnum=3,zcolor=color.white,ds=1e-15):
"""
This function returns a set of coordinate axes and labels.
ctr = coordinate center in multiples of ds
(defaults to vector(0,0,0))
xnum = half-length of each axis in multiples of ds
(defaults to 3)
zcolor = axis and label color
(defaults to color.white)
ds = order of magnitude in SI units
(defaults to 1e-15 m)
axeslist[] = returned list of axes and labels

Ex: refframe = MakeCoordAxesAt(xnum=5) for a macroscopic frame
Ex: refframe = MakeCoordAxesAt(xnum=1e-15) for a microscopic frame
"""
axeslist = []
xaxis = curve()
xaxis.append(pos=ds*(ctr-vector(xnum,0,0)),color=zcolor)
xaxis.append(pos=ds*(ctr+vector(xnum,0,0)),color=zcolor)
yaxis = curve()
yaxis.append(pos=ds*(ctr-vector(0,xnum,0)),color=zcolor)
yaxis.append(pos=ds*(ctr+vector(0,xnum,0)),color=zcolor)
zaxis = curve()
zaxis.append(pos=ds*(ctr-vector(0,0,xnum)),color=zcolor)
zaxis.append(pos=ds*(ctr+vector(0,0,xnum)),color=zcolor)
axeslist.append(xaxis)
axeslist.append(yaxis)
axeslist.append(zaxis)
axeslist.append(label(pos=ds*vector(xnum+ctr.x,0+ctr.y,0+ctr.z),
text='x',xoffset=10,yoffset=10,color=zcolor,linecolor=zcolor))
axeslist.append(label(pos=ds*vector(0+ctr.x,xnum+ctr.y,0+ctr.z),
text='y',xoffset=10,yoffset=10,color=zcolor,linecolor=zcolor))
axeslist.append(label(pos=ds*vector(0+ctr.x,0+ctr.y,xnum+ctr.z),
text='z',xoffset=10,yoffset=10,color=zcolor,linecolor=zcolor))
return axeslist

def ToggleVis(something):
"""
This function toggles the visibility of an object.
The object must support the 'visible' attribute.
something = either a list or another object

Note that None is returned.

Ex: ToggleVis(vr)
"""
if something.visible == undefined:
for c in something:
c.visible = not c.visible
else:
something.visible = not something.visible

scene.title = "MandIpy3 Demo"
#scene.waitfor('click')

ref = MakeCoordAxesAt(vector(0,0,0),4,color.white)
scene.waitfor('click')
ToggleVis(ref)



Joe Heafner
Sent from one of my Macs


Kevin Karplus

unread,
May 17, 2024, 12:06:40 AMMay 17
to vpytho...@googlegroups.com
Instead of
 if something.visible == undefined:
        for c in something:
            c.visible = not c.visible
    else:
        something.visible = not something.visible
I think you want something like
 if hasattr(something,"visible"):
          something.visible = not something.visible
    else:
          for c in something:
               ToggleVis(c)
even better
     try:
          something.visible = not something.visible
     except:
           for c in something:
                 ToggleVis(c)

Professor Emeritus, UC, Santa Cruz
Affiliations for identification only.




--
You received this message because you are subscribed to the Google Groups "VPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vpython-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vpython-users/20191182-D20E-4198-BAD6-EF43A1377574%40gmail.com.
Message has been deleted

Joe Heafner

unread,
May 17, 2024, 12:17:38 AMMay 17
to Users VPython
Okay the snippet works now; many thanks! All of the proposed solutions, including one I found, work. Now, my next question is why has this code always worked before and why does it work in Web VPython? Very strange.

Joe Heafner

unread,
May 17, 2024, 11:13:12 PMMay 17
to Users VPython
Today I confirmed that hasattr() works in Web VPython, and it's a more robust way to do what I need than what I was originally doing. I'm still puzzled over why the code suddenly stopped working though.


> On May 17, 2024, at 00:08, John <john...@shaw.ca> wrote:
>
> Try changing the line in def ToggleVis(something): function
>
> if something.visible == undefined:
> to call python function hasattr() instead
>
> if not hasattr(something, 'visible'):
>
> to work with the code block you have currently
>
> or change the code to the following
>
> if hasattr(something, 'visible'):
> something.visible = not something.visible
> else:
> for c in something:
> c.visible = not c.visible
>
> Then it should work.
>
> John
> --
> You received this message because you are subscribed to the Google Groups "VPython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vpython-user...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/vpython-users/218954cd-6bfc-4eb2-955c-1d8e7df66470n%40googlegroups.com.
Message has been deleted
Message has been deleted

John

unread,
May 21, 2024, 10:37:52 AMMay 21
to VPython-users
I get the same problem when running previous version of vpython (7.6.4) with python version 3.9 in a jupyter notebook. You can see for yourself by running it on the cloud on mybinder.org


I set up this  github repository to test it out with the following versions of the software specified in the binder/environment.yml file



So I don't think I understand your question, since your code still doesn't work with prior release of vpython in jupyter notebook.

John

John

unread,
May 21, 2024, 10:38:08 AMMay 21
to VPython-users
Which version of the code did it previously work for you? I tried your code with vpython 7.6.4 and python 3.9.x in a jupyter notebook and it didn't work there either. So I am not sure what you mean by "  I'm still puzzled over why the code suddenly stopped working though ".  You can try it for yourself in mybinder here.


I set up this github repository to test it in mybinder


with the following versions of software specified in the binder/environment.yml file


John


On Friday, May 17, 2024 at 8:13:12 PM UTC-7 heafnerj wrote:

Joe Heafner

unread,
May 23, 2024, 1:01:07 PMMay 23
to Users VPython


> On May 18, 2024, at 12:04, John <john...@shaw.ca> wrote:
>
> Which version of the code did it previously work for you? I tried your code with vpython 7.6.4 and python 3.9.x in a jupyter notebook and it didn't work there either. So I am not sure what you mean by " I'm still puzzled over why the code suddenly stopped working though ".

Well, the words are to be taken literally. It DID indeed work in a previous incarnation of Notebook or I would not have saved it as a .ipynb file. Why would I lie about that? I even used the notebook with the code in the classroom and in workshops to demonstrate portability. Like I said, I've used Web VPython, where the program still works perfectly, for the past several years and haven't followed development of other versions. While I don't remember specific version numbers, one thing I do remember is that at the time the same code could be used interchangeably between Web VPython and VPython for Jupyter Notebook. That's how I ported the project from the former to the latter in the first place. Something somewhere has changed, but it hasn't been on my end. Again, why would I imagine that or lie about it? C'mon...I'm better than that.

John

unread,
May 23, 2024, 2:19:29 PMMay 23
to VPython-users
Well it hasn't been a recent change to vpython. I tried it with vpython version 7.6.2 which was released in July 2021 and jupyterlab 3.1.0 which was the version at that time and still get the error.

AttributeError: 'list' object has no attribute 'visible'

You can try it here


 which has these versions of vpython and jupyter notebook from July 2021

    - jupyterlab==3.1.0
    - notebook==6.4.0
    - vpython==7.6.2
    - jupyterlab-vpython==3.1.1

Here is the release history of vpython


Since it's not something that has changed going back a few releases in vpython I don't think it is worth exploring further.

Reply all
Reply to author
Forward
0 new messages