You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Autodesk APS Viewer加载CameraRotation扩展失败求助

解决CameraRotation扩展加载时的TypeError问题

错误原因

viewer.loadDocumentNode是异步操作,你在调用它之后立刻执行viewer.loadExtension('CameraRotation')时,模型还未完全加载完成。此时扩展内部尝试访问viewer.model.getInstanceTree(),但viewer.model仍处于未初始化状态,因此抛出Cannot read properties of undefined错误。

解决方案

等待模型完全加载后再加载扩展,有两种可靠实现方式:

方式1:利用loadDocumentNode返回的Promise

function loadModel(viewer, urn) {
    function onDocumentLoadSuccess(doc) {
        // 等待模型加载完成Promise resolve后再加载扩展
        viewer.loadDocumentNode(doc, doc.getRoot().getDefaultGeometry())
            .then(() => {
                viewer.loadExtension('CameraRotation');
            })
            .catch(err => {
                console.error('模型加载失败:', err);
            });
    }
    function onDocumentLoadFailure(viewerErrorCode, message) {
        console.error('onDocumentLoadFailure() - errorCode:' + viewerErrorCode);
        console.error(message);
    }
    window.location.hash = urn;
    Autodesk.Viewing.Document.load('urn:' + urn, onDocumentLoadSuccess, onDocumentLoadFailure);
}

方式2:监听模型几何加载完成事件

function loadModel(viewer, urn) {
    function onDocumentLoadSuccess(doc) {
        viewer.loadDocumentNode(doc, doc.getRoot().getDefaultGeometry());
        
        // 监听模型几何加载完成事件,确保模型初始化完成
        const onGeometryLoaded = () => {
            viewer.loadExtension('CameraRotation');
            // 移除监听避免重复触发
            viewer.removeEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT, onGeometryLoaded);
        };
        viewer.addEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT, onGeometryLoaded);
    }
    function onDocumentLoadFailure(viewerErrorCode, message) {
        console.error('onDocumentLoadFailure() - errorCode:' + viewerErrorCode);
        console.error(message);
    }
    window.location.hash = urn;
    Autodesk.Viewing.Document.load('urn:' + urn, onDocumentLoadSuccess, onDocumentLoadFailure);
}

以上两种方式都能确保在模型实例树等核心资源初始化完成后再加载扩展,彻底规避undefined错误。

内容的提问来源于stack exchange,提问作者kaushik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 06:24:59