Skip to content

Commit e0923e5

Browse files
committed
Extract utility functions
1 parent 9e55cbe commit e0923e5

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed

js/src/_base/utils.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,39 @@ function nestedDiff(newObj, oldObj) {
357357
return ret;
358358
}
359359

360+
/**
361+
* Get the scene object of an Object3D.
362+
*
363+
* @param {THREE.Object3D} object3d
364+
* @returns The scene object, or null if it cannot be found
365+
*/
366+
function getObjectScene(object3d) {
367+
while (object3d.parent) {
368+
object3d = object3d.parent;
369+
}
370+
if (object3d.type === 'Scene') {
371+
return object3d;
372+
}
373+
return null;
374+
}
375+
376+
/**
377+
* Get the scene model for a three Object3D model.
378+
*
379+
* @param {any} model
380+
* @returns The scene object's model, or null if it cannot be found
381+
*/
382+
function getModelScene(model) {
383+
let obj = model.obj;
384+
while (obj.parent) {
385+
obj = obj.parent;
386+
}
387+
if (obj.type === 'Scene') {
388+
return obj.ipymodel;
389+
}
390+
return null;
391+
}
392+
360393

361394
module.exports = {
362395
createModel: createModel,
@@ -369,4 +402,6 @@ module.exports = {
369402
arrayDiff: arrayDiff,
370403
dictDiff: dictDiff,
371404
nestedDiff: nestedDiff,
405+
getObjectScene: getObjectScene,
406+
getModelScene: getModelScene,
372407
}

js/src/animation/AnimationAction.js

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,12 @@
11
var _ = require('underscore');
22
var widgets = require("@jupyter-widgets/base");
3+
var utils = require("../_base/utils");
34
var AnimationActionAutogen = require('./AnimationAction.autogen').AnimationActionModel;
45

56
var THREE = require('three');
67

78
var pkgName = require('../../package.json').name;
89

9-
function getObjectScene(object3d) {
10-
while (object3d.parent) {
11-
object3d = object3d.parent;
12-
}
13-
if (object3d.type === 'Scene') {
14-
return object3d;
15-
}
16-
return null;
17-
}
18-
19-
function getModelScene(model) {
20-
let obj = model.obj;
21-
while (obj.parent) {
22-
obj = obj.parent;
23-
}
24-
if (obj.type === 'Scene') {
25-
return obj.ipymodel;
26-
}
27-
return null;
28-
}
29-
3010
var AnimationActionModel = AnimationActionAutogen.extend({
3111

3212
defaults: function() {
@@ -70,7 +50,7 @@ var AnimationActionModel = AnimationActionAutogen.extend({
7050
this.obj.paused = false;
7151

7252
var root = this.get('localRoot');
73-
var scene = getModelScene(root);
53+
var scene = utils.getModelScene(root);
7454
if (scene) {
7555
this.listenTo(scene, 'afterRender', this.animateFrame.bind(this));
7656
}
@@ -92,7 +72,7 @@ var AnimationActionModel = AnimationActionAutogen.extend({
9272
mixer.update(delta);
9373
// As long as root is in scene, this will trigger a re-render
9474
// The onAfterRender will then trigger a new frame
95-
var scene = getModelScene(this.get('localRoot'));
75+
var scene = utils.getModelScene(this.get('localRoot'));
9676
if (scene) {
9777
scene.trigger('rerender');
9878
}
@@ -118,7 +98,7 @@ var AnimationActionModel = AnimationActionAutogen.extend({
11898
},
11999

120100
resetRenderHook: function() {
121-
var scene = getModelScene(this.get('localRoot'));
101+
var scene = utils.getModelScene(this.get('localRoot'));
122102
if (scene) {
123103
this.stopListening(scene, 'afterRender');
124104
}
@@ -223,4 +203,4 @@ var AnimationActionView = widgets.DOMWidgetView.extend({
223203
module.exports = {
224204
AnimationActionModel: AnimationActionModel,
225205
AnimationActionView: AnimationActionView,
226-
};
206+
};

0 commit comments

Comments
 (0)