Skip to content

Commit 3797724

Browse files
committed
Update to latest data widgets for fixes
1 parent 2c7a2ae commit 3797724

File tree

6 files changed

+98
-51
lines changed

6 files changed

+98
-51
lines changed

examples/superellipsoid.ipynb

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
},
1919
{
2020
"cell_type": "code",
21-
"execution_count": 2,
22-
"metadata": {
23-
"collapsed": false
24-
},
21+
"execution_count": null,
22+
"metadata": {},
2523
"outputs": [],
2624
"source": [
2725
"import numpy as np\n",
@@ -52,10 +50,8 @@
5250
},
5351
{
5452
"cell_type": "code",
55-
"execution_count": 3,
56-
"metadata": {
57-
"collapsed": true
58-
},
53+
"execution_count": null,
54+
"metadata": {},
5955
"outputs": [],
6056
"source": [
6157
"def superellipsoid(rx, ry, rz, m1, m2):\n",
@@ -82,10 +78,8 @@
8278
},
8379
{
8480
"cell_type": "code",
85-
"execution_count": 4,
86-
"metadata": {
87-
"collapsed": false
88-
},
81+
"execution_count": null,
82+
"metadata": {},
8983
"outputs": [],
9084
"source": [
9185
"# superellipsoid parameters\n",
@@ -104,10 +98,8 @@
10498
},
10599
{
106100
"cell_type": "code",
107-
"execution_count": 5,
108-
"metadata": {
109-
"collapsed": false
110-
},
101+
"execution_count": null,
102+
"metadata": {},
111103
"outputs": [],
112104
"source": [
113105
"import scipy.spatial as spatial\n",
@@ -117,32 +109,35 @@
117109
},
118110
{
119111
"cell_type": "code",
120-
"execution_count": 6,
121-
"metadata": {
122-
"collapsed": false
123-
},
112+
"execution_count": null,
113+
"metadata": {},
124114
"outputs": [],
125115
"source": [
126116
"from pythreejs import *\n",
117+
"from ipydatawidgets import ConstrainedNDArrayWidget\n",
127118
"from IPython.display import display\n",
128119
"\n",
129-
"surf_g = PlainGeometry(vertices=coords.tolist(), faces=cvx.simplices.tolist())\n",
130-
"surf = Mesh(geometry=surf_g, material=BasicMaterial(color='green', wireframe=True))\n",
120+
"coordinate_widget = ConstrainedNDArrayWidget(dtype='float32')(array=coords)\n",
121+
"\n",
122+
"surf_g = PlainBufferGeometry(attributes=dict(\n",
123+
" position=BufferAttribute(coordinate_widget),\n",
124+
" index=BufferAttribute(cvx.simplices.ravel().astype(np.uint16)),\n",
125+
"))\n",
126+
"surf = Mesh(geometry=surf_g, material=MeshBasicMaterial(color='green', wireframe=True))\n",
131127
"scene = Scene(children=[surf, AmbientLight(color='#777777')])\n",
132128
"c = PerspectiveCamera(position=[2, 2, 3], up=[0, 0, 1],\n",
133129
" children=[DirectionalLight(color='white',\n",
134130
" position=[3, 5, 1],\n",
135131
" intensity=0.6)])\n",
136-
"renderer = Renderer(camera=c, scene=scene, controls=[OrbitControls(controlling=c)])\n",
132+
"renderer = Renderer(\n",
133+
" camera=c, scene=scene, controls=[OrbitControls(controlling=c)], width=600, height=400)\n",
137134
"display(renderer)"
138135
]
139136
},
140137
{
141138
"cell_type": "code",
142-
"execution_count": 7,
143-
"metadata": {
144-
"collapsed": true
145-
},
139+
"execution_count": null,
140+
"metadata": {},
146141
"outputs": [],
147142
"source": [
148143
"from ipywidgets import FloatSlider, HBox, VBox\n",
@@ -155,10 +150,8 @@
155150
},
156151
{
157152
"cell_type": "code",
158-
"execution_count": 8,
159-
"metadata": {
160-
"collapsed": true
161-
},
153+
"execution_count": null,
154+
"metadata": {},
162155
"outputs": [],
163156
"source": [
164157
"rx_slider, ry_slider, rz_slider = (FloatSlider(description='rx', min=0.01, max=10.0, step=0.01, value=rx, \n",
@@ -171,16 +164,14 @@
171164
},
172165
{
173166
"cell_type": "code",
174-
"execution_count": 9,
175-
"metadata": {
176-
"collapsed": false
177-
},
167+
"execution_count": null,
168+
"metadata": {},
178169
"outputs": [],
179170
"source": [
180171
"def update(change):\n",
181172
" superellipsoid(rx_slider.value, ry_slider.value, rz_slider.value, \n",
182173
" m1_slider.value, m2_slider.value)\n",
183-
" surf_g.vertices = coords.tolist()\n",
174+
" coordinate_widget.array = coords\n",
184175
" \n",
185176
"m1_slider.observe(update, names=['value'])\n",
186177
"m2_slider.observe(update, names=['value'])\n",
@@ -191,10 +182,8 @@
191182
},
192183
{
193184
"cell_type": "code",
194-
"execution_count": 10,
195-
"metadata": {
196-
"collapsed": false
197-
},
185+
"execution_count": null,
186+
"metadata": {},
198187
"outputs": [],
199188
"source": [
200189
"VBox([HBox([renderer, m1_slider, m2_slider]), rx_slider, ry_slider, rz_slider])"
@@ -203,19 +192,17 @@
203192
{
204193
"cell_type": "code",
205194
"execution_count": null,
206-
"metadata": {
207-
"collapsed": true
208-
},
195+
"metadata": {},
209196
"outputs": [],
210197
"source": []
211198
}
212199
],
213200
"metadata": {
214201
"anaconda-cloud": {},
215202
"kernelspec": {
216-
"display_name": "Python [conda root]",
203+
"display_name": "Python 3",
217204
"language": "python",
218-
"name": "conda-root-py"
205+
"name": "python3"
219206
},
220207
"language_info": {
221208
"codemirror_mode": {
@@ -227,7 +214,7 @@
227214
"name": "python",
228215
"nbconvert_exporter": "python",
229216
"pygments_lexer": "ipython3",
230-
"version": "3.5.2"
217+
"version": "3.5.3"
231218
},
232219
"widgets": {
233220
"state": {

js/scripts/prop-types.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ _.extend(ArrayBufferType.prototype, BaseType.prototype, {
290290
getPropertyConverterFn: function() {
291291
return 'convertArrayBuffer';
292292
},
293+
getPropArrayName: function() {
294+
return 'datawidget_properties';
295+
},
293296
});
294297

295298
function DictType(defaultValue={}, nullable) {

js/src/_base/Three.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ var ThreeModel = widgets.WidgetModel.extend({
7272
// initialize properties arrays
7373
this.three_properties = [];
7474
this.three_array_properties = [];
75+
this.datawidget_properties = [];
7576

7677
// TODO: not currently integrated
7778
this.three_dict_properties = [];
@@ -173,6 +174,33 @@ var ThreeModel = widgets.WidgetModel.extend({
173174

174175
}, this);
175176

177+
// Handle changes in data widgets/union properties
178+
this.datawidget_properties.forEach(function(propName) {
179+
180+
var curr = this.get(propName) || [];
181+
if (curr instanceof widgets.WidgetModel) {
182+
// listen to changes in current model
183+
this.listenTo(curr, 'change', this.onChildChanged);
184+
this.listenTo(curr, 'childchange', this.onChildChanged);
185+
}
186+
187+
// make sure to (un)hook listeners when property changes
188+
this.on('change:' + propName, function(model, value, options) {
189+
var prev = this.previous(propName) || [];
190+
var curr = value || [];
191+
192+
if (prev instanceof widgets.WidgetModel) {
193+
this.stopListening(prev);
194+
}
195+
if (curr instanceof widgets.WidgetModel) {
196+
// listen to changes in current model
197+
this.listenTo(curr, 'change', this.onChildChanged);
198+
this.listenTo(curr, 'childchange', this.onChildChanged);
199+
}
200+
}, this);
201+
202+
}, this);
203+
176204
this.on('change', this.onChange, this);
177205
this.on('msg:custom', this.onCustomMessage, this);
178206

@@ -738,10 +766,14 @@ var ThreeModel = widgets.WidgetModel.extend({
738766

739767
// ArrayBuffer
740768
convertArrayBufferModelToThree: function(arr, propName) {
769+
if (arr instanceof widgets.WidgetModel) {
770+
return arr.get('array').data
771+
}
741772
return arr.data;
742773
},
743774

744775
convertArrayBufferThreeToModel: function(arrBuffer, propName) {
776+
// Never back-convert to a new widget
745777
return ndarray(arrBuffer);
746778
},
747779

js/src/core/BufferAttribute.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var BufferAttributeModel = BufferAttributeAutogen.extend({
1414
},
1515

1616
decodeData() {
17-
var rawData = this.get('array');
17+
var rawData = datawidgets.getArrayFromUnion(this.get('array'));
1818
var itemSize = rawData.dimension === 1 ? 1 : rawData.shape[rawData.dimension - 1];
1919

2020
var data = this.convertArrayBufferModelToThree(rawData, 'array');
@@ -44,15 +44,36 @@ var BufferAttributeModel = BufferAttributeAutogen.extend({
4444
},
4545

4646
mapBufferAttributeArrayThreeToModel: function() {
47+
/*
48+
* There are a few different cases to take into account here:
49+
* 1. We are during initial setup of a normal creation, with a widget ref
50+
* 2. We are during initial setup of a normal creation, with an array
51+
* 3. We are during initial setup from an existing three obj (nothing in model yet)
52+
* 4. We are syncing something back (e.g. after change event), possibly this data.
53+
* The model should already have something defined then (possibly null).
54+
*/
4755
var attributeData = this.obj.array;
4856
var modelNDArray = this.get('array');
4957
if (modelNDArray) {
50-
modelNDArray.data.set(attributeData);
51-
} else {
58+
// 1. / 2.
59+
if (modelNDArray instanceof datawidgets.NDArrayModel) {
60+
modelNDArray.get('array').data.set(attributeData);
61+
} else {
62+
modelNDArray.data.set(attributeData);
63+
}
64+
} else if (modelNDArray) {
65+
// 3. / 4.
5266
this.set('array', ndarray(attributeData, [this.obj.count, this.obj.itemSize]));
5367
}
5468
},
5569

70+
onChildChanged: function(model, options) {
71+
if (model === this.get('array')) {
72+
// We need to update data
73+
this.mapBufferAttributeArrayModelToThree();
74+
}
75+
},
76+
5677
});
5778

5879
module.exports = {

js/src/objects/Blackbox.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@ var BlackboxModel = BlackboxAutogen.extend({
3838

3939
}
4040

41-
});
41+
});
42+
43+
module.exports = {
44+
BlackboxModel: BlackboxModel,
45+
};

js/src/textures/DataTexture.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var DataTextureModel = DataTextureBase.extend({
1414
},
1515

1616
decodeData() {
17-
var rawData = this.get('data');
17+
var rawData = datawidgets.getArrayFromUnion(this.get('data'));
1818
if (rawData.dimension < 2 || rawData.dimension > 3) {
1919
throw Error('DataTexture data dimensions need to be 2 or 3, got:', rawData.dimension)
2020
}

0 commit comments

Comments
 (0)