Friday, 19 May 2017

TraitError: The 'faces' trait of a PlainGeometry instance must be a list, but a value of class 'numpy.ndarray'

I have this codes in jupyter notebook on pythreejs in chrome:

the github address: https://github.com/jovyan/pythreejs/blob/master/examples/Examples.ipynb

the source said :Indexed Geometries

The PlainGeometry lets you specify vertices and faces for a surface.

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)

but when running I got this error:

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.

so what should I do? best regards



via hasanbaghal

No comments:

Post a Comment