Error selecting move mode in the inspector after loading a scene

We are getting an error that this.gizmoLayer.utilityLayerScene.activeCamera is undefined on line 14319 of babylon.js. We are getting this error when we load a scene from the web into our app, open the inspector and click the move cursor on a point light in the scene. I can also get the error on a light that I add after the scene is loaded. I have been unable to reproduce the error in playground ( Does anyone have any idea what could cause the activeCamera on the gizmoLayer.utilityLayerScene to become undefined when loading a scene?

Can you try using scene.onActiveCameraChanged to see if the activeCamera ever becomes falsy?

It does become null (I suspect when I dispose of the current scene), but has a non null value after loading the new scene.

Digging into the code this is the section in my babylon bundle that is causing the problem:

if (this.attachedMesh) {
          var e = this.attachedMesh._effectiveMesh || this.attachedMesh;

          if (this.updateGizmoPositionToMatchAttachedMesh && this._rootMesh.position.copyFrom(e.absolutePosition), this.updateGizmoRotationToMatchAttachedMesh ? e.getWorldMatrix().decompose(void 0, this._rootMesh.rotationQuaternion) : this._rootMesh.rotationQuaternion.set(0, 0, 0, 1), this._updateScale) {
            var t = this.gizmoLayer.utilityLayerScene.activeCamera,
                i = t.globalPosition;
            t.devicePosition && (i = t.devicePosition), this._rootMesh.position.subtractToRef(i, this._tempVector);
            var n = this._tempVector.length() * this.scaleRatio;
            this._rootMesh.scaling.set(n, n, n), e._getWorldMatrixDeterminant() < 0 && (this._rootMesh.scaling.y *= -1);

But when I look at 4.0.0 tag of the Babylon source, the Gizmo code looks completely different:

if (this.attachedMesh) {
            const effectiveMesh = this.attachedMesh._effectiveMesh || this.attachedMesh;

            // Position
            if (this.updateGizmoPositionToMatchAttachedMesh) {

            // Rotation
            if (this.updateGizmoRotationToMatchAttachedMesh) {
                effectiveMesh.getWorldMatrix().decompose(undefined, this._rootMesh.rotationQuaternion!);
            else {
                this._rootMesh.rotationQuaternion!.set(0, 0, 0, 1);

            // Scale
            if (this._updateScale) {
                const activeCamera = this.gizmoLayer.utilityLayerScene.activeCamera!;
                var cameraPosition = activeCamera.globalPosition;
                if ((<WebVRFreeCamera>activeCamera).devicePosition) {
                    cameraPosition = (<WebVRFreeCamera>activeCamera).devicePosition;
                this._rootMesh.position.subtractToRef(cameraPosition, this._tempVector);
                var dist = this._tempVector.length() * this.scaleRatio;
                this._rootMesh.scaling.set(dist, dist, dist);

                // Account for handedness, similar to Matrix.decompose
                if (effectiveMesh._getWorldMatrixDeterminant() < 0) {
                    this._rootMesh.scaling.y *= -1;

Since I’m only updating position, it shouldn’t even get to the line that is failing according to the 4.0.0 tagged code. I wonder if this issue exists only in whatever is bundled under NPM, which seems to lag the latest, even though it has a 4.0.3 version number.

can you switch to 4.1? It is as stable as the “stable” release :slight_smile:

Thanks. I did install the 4.1.0-alpha.20 babylonjs from NPM. Now I’m failing when the UtlityLayerRenderer tries to create the DefaultUtilityLayer. It appears that EngineStore.LastCreatedScene is undefined after I load the scene. I see LastCreateScene gets set in the Scene constructor. What I am seeing is that the Scene constructor doesn’t appear to get called when I load the scene. I don’t know if that is why LastCreatedScene isn’t being set or is being set to null. What I don’t understand is why this works in playground.

I set a breakpoint in Scene and found the constructor is called when loading a scene in playground. But not in my code. I tried removing the call to stopRenderLoop, but that did not change the result.

can you share a repro somewhere?

I have been unable to reproduce in playground so far. My setup is that I am including babylon via npm in a library which is then included in a react application. The react application needs to do things that the library doesn’t, such as adding lights to scenes, so it also includes babylon as an npm module. The LoadAsync happens in the library in response to a call made from the app. I tried importing babylon.max so I could step through in dev tools, but there seems to be a bug in max when doing LoadAssetContainer.

@Deltakosh could the problem be that I’m calling scene.showDebugLayer in the app on a scene that was created in and returned from the library? But I don’t have the issue when I create a new scene (which is also done in the library.), add a light, show debug layer and activate the position gizmo.

All that you describe should work. Maybe you can publish your app on some pages?

A little easier to debug using babylon.max. From what I can tell, when I try to activate the position gizmo, EngineStore.Instances is size 0 and _LastCreatedScene is null. My suspicion is that the app is somehow getting a different EngineStore module than the library, but then I would expect it to behave the same after a new scene.

I added console.log in the library and the app. What I found is that LastCreatedScene is always in library and app after loading the scene. Instances has the last engine in the library, but is empty in the app. I’ve worked around this issue by setting LastCreatedScene in the app if it isn’t set before showing the inspector and that seems to work, but I still don’t know why it never gets set in my code on load but does on create.

AS I told you on PM this is likely to be a webpack/module issue and we cannot help as this is related to your setup :frowning: