Skip to content

Commit 7c4e908

Browse files
committed
Adjust [Buffer]Geometry.from_geometry
New default is to clear the reference after using it to copy. This prevents conflicts when restoring saved state. It can still be kept around if asked explicitly for it.
1 parent 2cfd05c commit 7c4e908

File tree

5 files changed

+46
-8
lines changed

5 files changed

+46
-8
lines changed

js/scripts/three-class-config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ module.exports = {
365365
skinWeights: new Types.VectorArray(),
366366
skinIndices: new Types.VectorArray(),
367367
_ref_geometry: new Types.ThreeType('BaseGeometry'),
368+
_store_ref: new Types.Bool(false),
368369
},
369370
},
370371
BufferGeometry: {
@@ -379,6 +380,7 @@ module.exports = {
379380
//groups: new Types.GeometryGroup(),
380381
//drawRange: new Types.DrawRange(),
381382
_ref_geometry: new Types.ThreeType(['BaseGeometry', 'BaseBufferGeometry']),
383+
_store_ref: new Types.Bool(false),
382384
},
383385
},
384386
InstancedBufferAttribute: {

js/src/core/BufferGeometry.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var BufferGeometryModel = AutogenBufferGeometryModel.extend({
1818
this.property_assigners['attributes'] = 'assignAttributesMap';
1919
},
2020

21-
constructFromRef: function(ref) {
21+
constructFromRef: function(ref, keep_ref) {
2222
var result = new THREE.BufferGeometry();
2323

2424
var chain = ref.initPromise.bind(this);
@@ -98,6 +98,10 @@ var BufferGeometryModel = AutogenBufferGeometryModel.extend({
9898
}).then(function() {
9999
// Add other fields that needs to be synced out:
100100
toSet.name = result.name;
101+
// Discard ref unless told to keep it:
102+
if (!keep_ref) {
103+
toSet._ref_geometry = null;
104+
}
101105

102106
// Perform actual sync to kernel side:
103107
this.set(toSet, 'pushFromThree');
@@ -109,8 +113,9 @@ var BufferGeometryModel = AutogenBufferGeometryModel.extend({
109113

110114
constructThreeObject: function() {
111115
var ref = this.get('_ref_geometry');
116+
var keep_ref = this.get('_store_ref');
112117
if (ref) {
113-
return this.constructFromRef(ref);
118+
return this.constructFromRef(ref, keep_ref);
114119
}
115120

116121
var result = new THREE.BufferGeometry();

js/src/core/Geometry.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var GeometryModel = AutogenGeometryModel.extend({
1010
var result = new THREE.Geometry();
1111

1212
var ref = this.get('_ref_geometry');
13+
var keep_ref = this.get('_store_ref');
1314
if (ref) {
1415
return ref.initPromise.bind(this).then(function() {
1516
result.copy(ref.obj);
@@ -27,6 +28,10 @@ var GeometryModel = AutogenGeometryModel.extend({
2728
}, this);
2829
this.syncToModel();
2930
this.props_created_by_three = old_three;
31+
if (!keep_ref) {
32+
this.set({_ref_geometry: null}, 'pushFromThree');
33+
this.save_changes();
34+
}
3035
return result;
3136
});
3237
}

pythreejs/core/BufferGeometry.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from traitlets import validate, TraitError, Undefined
44
from ipydatawidgets import NDArrayWidget, get_union_array
55

6+
from .Geometry import _make_key_filter
67
from .BufferGeometry_autogen import BufferGeometry as BufferGeometryBase
78

89

@@ -11,10 +12,13 @@
1112
class BufferGeometry(BufferGeometryBase):
1213

1314
@classmethod
14-
def from_geometry(cls, geometry):
15+
def from_geometry(cls, geometry, store_ref=False):
1516
"""Creates a PlainBufferGeometry of another geometry.
17+
18+
store_ref determines if the reference is stored after initalization.
19+
If it is, it will be used for future embedding.
1620
"""
17-
return cls(_ref_geometry=geometry)
21+
return cls(_ref_geometry=geometry, _store_ref=store_ref)
1822

1923
@validate('attributes')
2024
def validate(self, proposal):
@@ -48,6 +52,10 @@ def _gen_repr_from_keys(self, keys):
4852
signature_parts.append('%s=%s' % (name, _attr_dict_repr(getattr(self, name))))
4953
return '%s(%s)' % (class_name, ', '.join(signature_parts))
5054

55+
def _repr_keys(self):
56+
return filter(_make_key_filter(self._store_ref),
57+
super(BufferGeometry, self)._repr_keys())
58+
5159

5260
def _dict_is_default(ht, name):
5361
value = getattr(ht, name)

pythreejs/core/Geometry.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,37 @@
11
from ipywidgets import register
2-
from .Geometry_autogen import Geometry as GeometryBase
2+
from .Geometry_autogen import Geometry as AutogenGeometry
3+
from .._base.Three import ThreeWidget
34

45

56
@register
6-
class Geometry(GeometryBase):
7+
class Geometry(AutogenGeometry):
78

89
@classmethod
9-
def from_geometry(cls, geometry):
10+
def from_geometry(cls, geometry, store_ref=False):
1011
"""Creates a PlainGeometry of another geometry.
1112
13+
store_ref determines if the reference is stored after initalization.
14+
If it is, it will be used for future embedding.
15+
1216
NOTE:
1317
The PlainGeometry will copy the arrays from the source geometry.
1418
To avoid this, use PlainBufferGeometry.
1519
"""
16-
return cls(_ref_geometry=geometry)
20+
return cls(_ref_geometry=geometry, _store_ref=store_ref)
21+
22+
def _repr_keys(self):
23+
return filter(_make_key_filter(self._store_ref),
24+
super(Geometry, self)._repr_keys())
25+
1726

27+
_non_gen_keys = tuple(ThreeWidget.class_trait_names())
1828

29+
def _make_key_filter(use_ref):
30+
def key_filter(key):
31+
return (
32+
key in _non_gen_keys or
33+
(use_ref and key == '_ref_geometry') or
34+
(not use_ref and key != '_ref_geometry')
35+
)
1936

37+

0 commit comments

Comments
 (0)