[英]AttributeError: 'compound' object has no attribute '_origin' in vpython.py
What needs to be done to fix this problem? 要解决此问题需要做什么?
At https://www.glowscript.org/#/user/murray.garth/folder/Public/program/Eyeballs I found a GlowScript example that i tried to use as a python3 script. 在https://www.glowscript.org/#/user/murray.garth/folder/Public/program/Eyeballs,我找到了一个GlowScript示例,尝试将其用作python3脚本。
I modified the header to 我将标题修改为
#https://www.glowscript.org/#/user/murray.garth/folder/Public/program/Eyeballs
#GlowScript 2.1 VPython
from vpython import *
and changed the true/false references to uppercase. 并将true / false引用更改为大写。
running 跑步
python3 eyeballs.py
and then gives the error message: 然后给出错误信息:
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'
According to https://www.glowscript.org/docs/VPythonDocs/compound.html the syntax for compound_to_world is: 根据https://www.glowscript.org/docs/VPythonDocs/compound.html,compound_to_world的语法为:
world_pos = c.compound_to_world(v)
Which seems to be ok to me. 这似乎对我没关系。
The environment is macports python3 环境是macports python3
python3 --version
Python 3.7.4
I had installed vpython with 我已经安装了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)
This was a tough one. 这是一个艰难的过程。 There are some 1.2 million questions on python on stackoverflow.
关于stackoverflow上的python大约有120万个问题。 If you search for
如果您搜寻
[python]"object has no attribute"
you get some 12066 results. 您会得到一些12066的结果。 So that's probably why this question didn'get much attention.
因此,这可能就是为什么这个问题没有引起足够重视的原因。
I tried out the issue with a smaller example according to https://www.glowscript.org/docs/VPythonDocs/compound.html 我根据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)
giving the error mentioned in the question: 给出问题中提到的错误:
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'
The relevant source code lines of vpython.py are: 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
and indeed a few lines further up the constructor does not set any origin. 实际上,构造函数再往后几行并没有设置任何原点。 So adding a default origin:
因此,添加默认来源:
class compound(standardAttributes):
compound_idx = 0 # same numbering scheme as in GlowScript
def __init__(self, objList, **args):
self._origin = vector(0,0,0)
makes the syntax error go away. 使语法错误消失。
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)
then gives the result: 然后给出结果:
compound event return
<0, 0, 0>
<0.6, 1.41421, 0>
and the eyeballs.py code works as expected: 并且eyeballs.py代码按预期工作:
I do not know there to report this bug but I posted a message to the vpython-users group 我不知道在那里报告此错误,但我向vpython-users组发布了一条消息
Thanks for reporting this bug, which I'll report as an issue in the vpython repository. 感谢您报告此错误,我将在vpython存储库中将其报告为问题。 As you say, a workaround is to specify an origin.
如您所说,一种解决方法是指定一个原点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.