0

I'm trying to change the color of elements in 3D Viewer using the Autodesk-forge platform, and for this I'm using this API https://forge.autodesk.com/cloud_and_mobile/2015/12/change-color-of-elements-with-view-and-data-api.html by Daniel Du. But the problem is when running I got this The error Pict

And this the function :

Autodesk.Viewing.Viewer3D.prototype.setColorMaterial = function(objectIds, color) {
        var material = addMaterial(color);

        for (var i=0; i<objectIds.length; i++) {

            var dbid = objectIds[i];

            //from dbid to node, to fragid
            viewer.addEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT, function () {
                var it = viewer.model.getData().instanceTree;
                console.log(it);

                it.enumNodeFragments(dbid, function (fragId) {          
                    var renderProxy = viewer.impl.getRenderProxy(viewer.model, fragId);
                    console.log("r prox : " + renderProxy);

                    renderProxy.meshProxy = new THREE.Mesh(renderProxy.geometry, renderProxy.material);

                    renderProxy.meshProxy.matrix.copy(renderProxy.matrixWorld);
                    renderProxy.meshProxy.matrixWorldNeedsUpdate = true;
                    renderProxy.meshProxy.matrixAutoUpdate = false;
                    renderProxy.meshProxy.frustumCulled = false;

                    viewer.impl.addOverlay(overlayName, renderProxy.meshProxy);
                    viewer.impl.invalidate(true);

                }, false);

            });

        }

    }

Hopefully, anyone has the solution to this problem...

Maistrenko Vitalii
  • 994
  • 1
  • 8
  • 16

1 Answers1

0

Most likely you are running this code before the instance tree has been loaded, which provokes the error Cannot read property 'enumNodeFragments' of undefined on it variable. You would need to wait for the Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT before running that code.

Take also a look at previous question about modifying materials in the viewer.

Felipe
  • 4,325
  • 1
  • 14
  • 19