【问题标题】:TraitError: The 'faces' trait of a PlainGeometry instance must be a list, but a value of class 'numpy.ndarray'TraitError:PlainGeometry 实例的 'faces' 特征必须是一个列表,但是类 'numpy.ndarray' 的值
【发布时间】:2017-05-19 07:49:08
【问题描述】:

我在 chrome 中的 pythreejs 上的 jupyter notebook 中有这些代码:

github地址:https://github.com/jovyan/pythreejs/blob/master/examples/Examples.ipynb

消息来源说:索引几何

PlainGeometry 可让您指定曲面的顶点和面。

from pythreejs import *
import numpy as np
from IPython.display import display

vertices = np.asarray([
[0, 0, 0],
[0, 0, 1],
[0, 1, 0],
[0, 1, 1],
[1, 0, 0],
[1, 0, 1],
[1, 1, 0],
[1, 1, 1]
], dtype='float32')

faces = np.asarray([
        [0, 1, 3],
        [0, 2, 3],
        [0, 2, 4],
        [2, 4, 6],
        [0, 1, 4],
        [1, 4, 5],
        [2, 3, 6],
        [3, 6, 7],
        [1, 3, 5],
        [3, 5, 7],
        [4, 5, 6],
        [5, 6, 7]
    ])

vertexcolors = np.asarray([(0,0,0), (0,0,1), (0,1,0), (1,0,0), (0,1,1), (1,0,1), (1,1,0), (1,1,1)])
facecolors = [[vertexcolors[i] for i in f] for f in faces]
cubeGeometry = PlainGeometry(vertices=vertices, faces=faces, faceColors = facecolors)

myobjectCube = Mesh(geometry=cubeGeometry, material = LambertMaterial(vertexColors = 'VertexColors'))
cCube = PerspectiveCamera(position=[3, 3, 3], fov=20,
                      children=[DirectionalLight(color='#ffffff', position=[-3, 5, 1], intensity=0.5)])
sceneCube = Scene(children=[myobjectCube, AmbientLight(color='#dddddd')])

rendererCube = Renderer(camera=cCube, background='black', background_opacity=1,
                        scene = sceneCube, controls=[OrbitControls(controlling=cCube)])

display(rendererCube)

但运行时出现此错误:

TraitError                                Traceback (most recent call last)
<ipython-input-10-753dd6c3579b> in <module>()
     31 vertexcolors = np.asarray([(0,0,0), (0,0,1), (0,1,0), (1,0,0), (0,1,1), (1,0,1), (1,1,0), (1,1,1)])
     32 facecolors = [[vertexcolors[i] for i in f] for f in faces]
---> 33 cubeGeometry = PlainGeometry(vertices=vertices, faces=faces, faceColors = facecolors)
     34 
     35 myobjectCube = Mesh(geometry=cubeGeometry, material = LambertMaterial(vertexColors = 'VertexColors'))

c:\python34\lib\site-packages\ipywidgets\widgets\widget.py in __init__(self, **kwargs)
    198         """Public constructor"""
    199         self._model_id = kwargs.pop('model_id', None)
--> 200         super(Widget, self).__init__(**kwargs)
    201 
    202         Widget._call_widget_constructed(self)

c:\python34\lib\site-packages\traitlets\config\configurable.py in __init__(self, **kwargs)
     71 
     72         # load kwarg traits, other than config
---> 73         super(Configurable, self).__init__(**kwargs)
     74 
     75         # load config

c:\python34\lib\site-packages\traitlets\traitlets.py in __init__(self, *args, **kwargs)
    995             for key, value in kwargs.items():
    996                 if self.has_trait(key):
--> 997                     setattr(self, key, value)
    998                 else:
    999                     # passthrough args that don't set traits to super

c:\python34\lib\site-packages\traitlets\traitlets.py in __set__(self, obj, value)
    583             raise TraitError('The "%s" trait is read-only.' % self.name)
    584         else:
--> 585             self.set(obj, value)
    586 
    587     def _validate(self, obj, value):

c:\python34\lib\site-packages\traitlets\traitlets.py in set(self, obj, value)
    557 
    558     def set(self, obj, value):
--> 559         new_value = self._validate(obj, value)
    560         try:
    561             old_value = obj._trait_values[self.name]

c:\python34\lib\site-packages\traitlets\traitlets.py in _validate(self, obj, value)
    589             return value
    590         if hasattr(self, 'validate'):
--> 591             value = self.validate(obj, value)
    592         if obj._cross_validation_lock is False:
    593             value = self._cross_validate(obj, value)

c:\python34\lib\site-packages\traitlets\traitlets.py in validate(self, obj, value)
   2322 
   2323     def validate(self, obj, value):
-> 2324         value = super(List, self).validate(obj, value)
   2325         value = self.validate_elements(obj, value)
   2326         return value

c:\python34\lib\site-packages\traitlets\traitlets.py in validate(self, obj, value)
   2236         if isinstance(value, self._cast_types):
   2237             value = self.klass(value)
-> 2238         value = super(Container, self).validate(obj, value)
   2239         if value is None:
   2240             return value

c:\python34\lib\site-packages\traitlets\traitlets.py in validate(self, obj, value)
   1675             return value
   1676         else:
-> 1677             self.error(obj, value)
   1678 
   1679     def info(self):

c:\python34\lib\site-packages\traitlets\traitlets.py in error(self, obj, value)
   1522                 % (self.name, self.info(), msg)
   1523 
-> 1524         raise TraitError(e)
   1525 
   1526 

TraitError: The 'faces' trait of a PlainGeometry instance must be a list, but a value of class 'numpy.ndarray' (i.e. array([[0, 1, 3],
       [0, 2, 3],
       [0, 2, 4],
       [2, 4, 6],
       [0, 1, 4],
       [1, 4, 5],
       [2, 3, 6],
       [3, 6, 7],
       [1, 3, 5],
       [3, 5, 7],
       [4, 5, 6],
       [5, 6, 7]])) was specified.

那我该怎么办?最好的问候

【问题讨论】:

    标签: python node.js python-3.x github jupyter-notebook


    【解决方案1】:

    您可能已从 github 下载了该示例的最新版本,但您当前已从 pypi 安装了较早版本的 pythreejs。使用 numpy 数组作为参数的变化似乎是最近才出现的。

    我建议使用来自 github 的旧版本示例 - 这应该可以。

    https://github.com/jovyan/pythreejs/blob/fec50b7035608279594b17c55f2bcda26229b3c0/examples/Examples.ipynb

    或者,从 git 下载最新版本的 pythreejs 并手动安装。

    【讨论】:

      猜你喜欢
      • 2018-09-18
      • 1970-01-01
      • 2018-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-22
      相关资源
      最近更新 更多