Skip to content

Commit 613c218

Browse files
committed
Define trait WebGLDataUnion
1 parent 26bc6e4 commit 613c218

File tree

6 files changed

+32
-35
lines changed

6 files changed

+32
-35
lines changed

js/scripts/prop-types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ _.extend(ArrayBufferType.prototype, BaseType.prototype, {
285285
args.push(`shape_constraint=${this.shapeConstraint}`);
286286
}
287287

288-
return `DataUnion(${args.join(', ')}).tag(sync=True)`;
288+
return `WebGLDataUnion(${args.join(', ')}).tag(sync=True)`;
289289
},
290290
getPropertyConverterFn: function() {
291291
return 'convertArrayBuffer';

js/scripts/templates/py_wrapper.mustache

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ from traitlets import (
66
Tuple, List, Dict, Float, CFloat, Bool, Union, Any,
77
)
88

9-
from ipydatawidgets import DataUnion
10-
119
from {{ py_base_relative_path }}enums import *
1210
from {{ py_base_relative_path }}traits import *
1311

pythreejs/core/BufferAttribute.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,10 @@ class BufferAttribute(BaseBufferAttribute):
1111

1212
def __init__(self, array=None, normalized=True, **kwargs):
1313
if array is not None:
14-
# If not supplied, leave as undefined.
14+
# Only include array in kwargs if supplied
1515
# This is needed for remote initialization
16+
# (Undefined behaves differently)
1617
kwargs['array'] = array
1718
kwargs['normalized'] = normalized
1819
# NOTE: skip init of direct parent class on purpose:
1920
super(BaseBufferAttribute, self).__init__(**kwargs)
20-
21-
@validate('array')
22-
def _valid_array(self, proposal):
23-
value = proposal['value']
24-
if isinstance(value, NDArrayWidget):
25-
value = value.array
26-
27-
if isinstance(proposal['value'], NDArrayWidget):
28-
if value is not Undefined and value.dtype == np.float64:
29-
raise TraitError('Cannot use a float64 data widget as a BufferAttribute source.')
30-
return proposal['value']
31-
if value is not Undefined and value.dtype == np.float64:
32-
# 64-bit not supported, coerce to 32-bit
33-
value = value.astype(np.float32)
34-
return value

pythreejs/textures/DataTexture.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,16 @@ class DataTexture(BaseDataTexture):
1212
@validate('data')
1313
def _valid_data(self, proposal):
1414
value = proposal['value']
15-
if isinstance(value, NDArrayWidget):
16-
value = value.array
15+
array = value.array if isinstance(value, NDArrayWidget) else value
1716

1817
# Validate shape
19-
if np.ndim(value) < 2 or np.ndim(value) > 3:
18+
if np.ndim(array) < 2 or np.ndim(array) > 3:
2019
raise TraitError('Data needs to have two or three dimensions. Given shape was: %r'
21-
% (np.shape(value),))
20+
% (np.shape(array),))
2221

2322
old = self._trait_values.get(proposal['trait'].name, None)
24-
if old is not None and np.ndim(old) > 0 and np.shape(old) != np.shape(value):
23+
if old is not None and np.ndim(old) > 0 and np.shape(old) != np.shape(array):
2524
raise TraitError('Cannot change shape of previously initialized DataTexture. %r vs %r'
26-
% (np.shape(old), np.shape(value)))
25+
% (np.shape(old), np.shape(array)))
2726

28-
if isinstance(proposal['value'], NDArrayWidget):
29-
if value is not Undefined and value.dtype == np.float64:
30-
raise TraitError('Cannot use a float64 data widget as a BufferAttribute source.')
31-
return proposal['value']
32-
if value is not Undefined and value.dtype == np.float64:
33-
# 64-bit not supported, coerce to 32-bit
34-
value = value.astype(np.float32)
3527
return value
36-

pythreejs/traits.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11

2-
from traitlets import Unicode, Int, CInt, Instance, Enum, List, Dict, Float, CFloat, Bool, Tuple
2+
from traitlets import (
3+
Unicode, Int, CInt, Instance, Enum, List, Dict, Float, CFloat,
4+
Bool, Tuple, Undefined, TraitError,
5+
)
6+
7+
from ipydatawidgets import DataUnion, NDArrayWidget
38

49
def Vector2(trait_type=CFloat, default=None, **kwargs):
510
if default is None:
@@ -42,3 +47,17 @@ def Euler(default=None, **kwargs):
4247
if default is None:
4348
default = [0, 0, 0, 'XYZ']
4449
return Tuple(CFloat(), CFloat(), CFloat(), Unicode(), default_value=default, **kwargs)
50+
51+
52+
class WebGLDataUnion(DataUnion):
53+
def validate(self, obj, value):
54+
value = super(WebGLDataUnion, self).validate(obj, value)
55+
array = value.array if isinstance(value, NDArrayWidget) else value
56+
57+
if array is not Undefined and str(array.dtype) == 'float64':
58+
if isinstance(value, NDArrayWidget):
59+
raise TraitError('Cannot use a float64 data widget as a BufferAttribute source.')
60+
else:
61+
# 64-bit not supported, coerce to 32-bit
62+
value = value.astype('float32')
63+
return value

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@
4848
'pythreejs/static/index.js.map',
4949
]),
5050
],
51-
'install_requires': ['ipywidgets>=7.0.0a9', 'ipydatawidgets'],
51+
'install_requires': [
52+
'ipywidgets>=7.0.0a9',
53+
'ipydatawidgets'
54+
],
5255
'packages': find_packages(),
5356
'zip_safe': False,
5457
'cmdclass': cmdclass,

0 commit comments

Comments
 (0)