需要做些什么来解决这个问题?
在https://www.glowscript.org/#/user/murray.garth/folder/Public/program/Eyeballs,我找到了一个GlowScript示例,我试图将其用作python3脚本。

我将标题修改为
#https://www.glowscript.org/#/user/murray.garth/folder/Public/program/Eyeballs
#GlowScript 2.1 VPython
from vpython import *并将真假引用改为大写。
正在运行
python3 eyeballs.py 启动静态图像

然后给出错误消息:
compound event return
compound event return
compound event return
Traceback (most recent call last):
File "eyeballs.py", line 39, in <module>
world_pos = Head.compound_to_world( vRightEye.pos )
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/vpython/vpython.py", line 1553, in compound_to_world
v = v-self._origin
AttributeError: 'compound' object has no attribute '_origin'根据https://www.glowscript.org/docs/VPythonDocs/compound.html,compound_to_world的语法是:
world_pos = c.compound_to_world(v) 似乎没问题。对我来说。
环境是macports python3。
python3 --version
Python 3.7.4我安装了vpython
pip install vpython
pip --version
pip 18.1 from /opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pip (python 3.7)发布于 2019-07-30 06:12:31
这是一次艰难的考验。关于堆栈溢出的python,大约有120万个问题。如果你在寻找
[python]"object has no attribute" 你得到了大约12066的结果。所以这可能就是为什么这个问题没有得到足够的关注。
根据https://www.glowscript.org/docs/VPythonDocs/compound.html,我用一个较小的例子尝试了这个问题
from vpython import *
handle = cylinder( size=vec(1,.2,.2),color=vec(0.72,0.42,0) )
head = box( size=vec(.2,.6,.2), pos=vec(1.1,0,0),color=color.gray(.6) )
hammer = compound([handle, head])
hammer.axis = vec(1,1,0)
world_pos = hammer.compound_to_world(hammer.axis) 给出问题中提到的错误:
compound event return
Traceback (most recent call last):
File "hammer.py", line 10, in <module>
print (hammer.origin)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/vpython/vpython.py", line 1536, in origin
return self._origin
AttributeError: 'compound' object has no attribute '_origin'vpython.py的相关源代码行是:
@property
def origin(self):
return self._origin
@origin.setter
def origin(self,value): # compound origin cannot be reset
if not self._constructing:
raise AttributeError('The compound "origin" attribute is read-only; change "pos" instead.')
self._origin = value
def world_to_compound(self, v):
v = v-self._pos
x_axis = self._axis.hat
y_axis = self._up.hat
z_axis = x_axis.cross(y_axis)
ox = self._size0.x/self._size.x # _size0 is the original size
oy = self._size0.y/self._size.y
oz = self._size0.z/self._size.z
return self._origin + vector(v.dot(x_axis)*ox, v.dot(y_axis)*oy, v.dot(z_axis)*oz)
def compound_to_world(self, v):
v = v-self._origin
x_axis = self._axis.hat
y_axis = self._up.hat
z_axis = x_axis.cross(y_axis)
ox = self._size.x/self._size0.x # _size0 is the original size
oy = self._size.y/self._size0.y
oz = self._size.z/self._size0.z
return self._pos + v.x*ox*x_axis + v.y*oy*y_axis + v.z*oz*z_axis实际上,在构造函数后面的几行构造函数没有设置任何起源。因此,添加一个默认的原点:
class compound(standardAttributes):
compound_idx = 0 # same numbering scheme as in GlowScript
def __init__(self, objList, **args):
self._origin = vector(0,0,0)使语法错误消失。
from vpython import *
handle = cylinder( size=vec(1,.2,.2), color=vec(0.72,0.42,0) )
head = box( size=vec(.2,.6,.2), pos=vec(1.1,0,0), color=color.gray(.6) )
hammer = compound([handle, head])
hammer.axis = vec(1,1,0)
print (hammer.origin)
world_pos = hammer.compound_to_world(hammer.axis)
print (world_pos)然后给出结果:
compound event return
<0, 0, 0>
<0.6, 1.41421, 0>eyeballs.py代码的工作方式与预期的一样:

我不知道在那里报告这个错误,但我发布了发送给vpython-user组的消息。
发布于 2019-07-30 18:14:53
谢谢您报告这个bug,我将在vpython存储库中报告这个问题。正如您所说,解决办法是指定一个起源。
https://stackoverflow.com/questions/57175489
复制相似问题