AttributeError: 'compound' object has no attribute '_origin' in vpython.py

AttributeError: 'compound' object has no attribute '_origin' in vpython.py

解决这个问题需要做什么?

https://www.glowscript.org/#/user/murray.garth/folder/Public/program/Eyeballs 我发现了一个 GlowScript 示例,我试图将其用作 python3 脚本。

我将header修改为

#https://www.glowscript.org/#/user/murray.garth/folder/Public/program/Eyeballs
#GlowScript 2.1 VPython
from vpython import *

并将 true/false 引用更改为大写。

运行

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) 

这似乎是 o.k。对我来说。

环境是macportspython3

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)

这是一场艰难的比赛。 在 Whosebug 上 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 代码按预期工作:

我不知道那里可以报告这个错误,但我发布了 a message to the vpython-users group

感谢您报告此错误,我将在 vpython 存储库中将其报告为一个问题。如您所说,解决方法是指定原点。