I’m using Forge Viewer to load multiple models dynamically. After initializing the viewer with Autodesk.Viewing.Initializer, I iterate over my models and load them as follows:
models.forEach(model => {const documentId = 'urn:' + model.urn;
const applyRefPoint = model.applyRefPoint || false;
Autodesk.Viewing.Document.load(
documentId,
(viewerDocument) => {
const defaultModel = viewerDocument.getRoot().getDefaultGeometry();
const transformMatrix = viewerLoadInfo.forgeData.transform;
const globalOffset = model.globalOffset || { x: 0, y: 0, z: 0 };
let loadModelOptions = {
keepCurrentModels: true,
applyRefPoint,
applyScaling: 'm',
};
viewer.loadDocumentNode(viewerDocument, defaultModel, loadModelOptions).then(onLoad);
},
(error) => {
console.error(error);
}
});
The code generally works, but sometimes, one or more models fail to load with the following error message:
Viewer3DImpl.js:5586 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'isEmpty')
at te.hasModels (Viewer3DImpl.js:5586:30)
at ee.loadModel (Viewer3D.js:1539:24)
at D.loadModel (GuiViewer3D.js:324:48)
In Viewer3DImpl.js, the failing line of code is:
/**
* Whether any models have been loaded already.
* A model is considered loaded as soon as the Model instance has
* been added to RenderScene.
*/
this.hasModels = function () {
return !_renderScene.isEmpty();
};
It seems like _renderScene is somehow undefined or uninitialized at the time this line runs. This error is intermittent, and sometimes the models load without any issue.
Has anyone encountered a similar issue with random load failures in Forge Viewer?
The render scene is setup as part of the Viewer3D#initialize
method which is itself called within Viewer3D#start
. Make sure you only start loading the models after viewer.start()
.