autodesk-forgeautodesk-viewerautodesk-model-derivative

Error to load CameraRotation extension in Autodesk APS viewer


I am trying to add an extension by using the reference [https://github.com/autodesk-platform-services/aps-extensions]

I was implementing CameraRotation in simple viewer. I followed all the basis instructions as per [https://aps.autodesk.com/blog/viewer-extensions-tips-and-tricks-or-treats]

function loadModel(viewer, urn) {
    function onDocumentLoadSuccess(doc) {
        viewer.loadDocumentNode(doc, doc.getRoot().getDefaultGeometry());
        viewer.loadExtension('CameraRotation');
    }
    function onDocumentLoadFailure(viewerErrorCode, message) {
        console.error('onDocumentLoadFailure() - errorCode:' + viewerErrorCode);
        console.error(message);
    }
    window.location.hash = urn;
    Autodesk.Viewing.Document.load('urn:' + urn, onDocumentLoadSuccess, onDocumentLoadFailure);
}

But I am getting an error

 TurnTableExtension.js:35 Uncaught TypeError: Cannot read properties of undefined (reading 'getInstanceTree')
 at TurnTableExtension.load (TurnTableExtension.js:35:31)
 at extension.<computed> [as load] (ExtensionEventsMixin.js:31:27)
 at GuiViewer3D.loadExtensionLocal (ExtensionManager.js:401:34)
 at GuiViewer3D.loadExtension (ExtensionManager.js:260:19)
 at onDocumentLoadSuccess (main.js:110:15)
 at Function.onSuccess (Document.js:197:26)
 at onSuccessWrapped (Xhr.js:761:32)
 at XMLHttpRequest.onLoad (Xhr.js:1003:24)

Solution

  • This looks like a data race issue, with the extension trying to access the instance tree of the model before it is available. If the code in your extension needs to access certain model data, make sure to wait until the data is available, for example, using the GEOMETRY_LOADED_EVENT:

    viewer.addEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT, async function () {
      // access model data here
    });