Three.js: Uncaught TypeError: Cannot read property 'length' of undefined

Created on 5 Sep 2017  路  11Comments  路  Source: mrdoob/three.js

with threejs r87 i have a problem calling this : console.log(mesh.geometry.faces.length); that worked fine with threejs r74. what is modified? Anyone can help me?
this is the program:

// start
var loaderF = new THREE.FileLoader();
//load a text file a output the result to the console
loaderF.load(
    // resource URL
    'segnalibro.stl',
    // Function when resource is loaded
    function ( data ) {
        // output the text to the console
        //console.log( data );
        console.log("Caricamento modello stl finito");
    },
    // Function called when download progresses
    function ( xhr ) {
        console.log( (xhr.loaded / xhr.total * 100) + '% loaded' );
    },
    // Function called when download errors
    function ( xhr ) {
        console.error( 'An error happened' );
    }
);
var loader = new THREE.STLLoader();
STLMaterial = new THREE.MeshPhongMaterial({color: 0x7777ff});
loader.load( 'segnalibro.stl', function (geometry) {
STLMaterial = new THREE.MeshPhongMaterial( { color: 0x999999,specular: 0x111111,shininess: 20} );
mesh = new THREE.Mesh(geometry, STLMaterial);
 mesh.geometry.computeBoundingBox();
console.log( mesh);
//console.log(boundingBox);
mesh.rotation.set( - Math.PI / 2, 0,  -Math.PI / 2);
mesh.position.set(-125, 0, 125);
mesh.scale.set(1, 1, 1);
scene.add(mesh);
mesh.receiveShadow=true;
mesh.castShadow=true;   
console.log(mesh.geometry.faces.length);

This form is for three.js bug reports and feature requests only.
This is NOT a help site. Do not ask help questions here.
If you need help, please use stackoverflow.

Describe the bug or feature request in detail.
A code snippet, screenshot, and small-test help us understand.

You can edit for small-test.
http://jsfiddle.net/akmcv7Lh/ (current revision)
http://jsfiddle.net/hw9rcLL8/ (dev)

Three.js version
  • [ ] Dev
  • [x ] r87
  • [ ] ...
Browser
  • [x] All of them
  • [x ] Chrome
  • [ ] Firefox
  • [ ] Internet Explorer
OS
  • [x] All of them
  • [x ] Windows
  • [ ] macOS
  • [ ] Linux
  • [ ] Android
  • [ ] iOS
Hardware Requirements (graphics card, VR Device, ...)
Help (please use the forum)

Most helpful comment

now it work with:

    var geometry = new THREE.Geometry().fromBufferGeometry( mesh.geometry );
        console.log( geometry.faces.length );


        var geomesh = new THREE.Mesh(
                    geometry ,
                    new THREE.MeshBasicMaterial( { color: 0xffffff, opacity: 0.5, transparent: true } )
                );
        var volumeMesh = calculateVolume(geomesh)/1000;

All 11 comments

STLLoader now returns BufferGeometry.

i try to add
var geomesh = new THREE.Geometry().fromBufferGeometry(mesh.geometry) console.log(geomesh.geometry.faces.length);
but i have the same error

Try this:

var geometry = new THREE.Geometry().fromBufferGeometry( mesh.geometry );
console.log( geometry.faces.length );
var geometry = new THREE.Geometry().fromBufferGeometry( mesh.geometry );
console.log( geometry.faces.length );

ok it work fine now but if i call

var volumeMesh = calculateVolume(geometry)/1000;

or

var volumeMesh = calculateVolume(mesh)/1000;

i have the same problem

function volumeOfT(a, b, c){
    var p1 = a.x*b.y*c.z;
    var p2 = c.x*a.y*b.z;
    var p3 = b.x*c.y*a.z;
    var n1 = c.x*b.y*a.z;
    var n2 = b.x*a.y*c.z;
    var n3 = a.x*c.y*b.z;
    return (1.0/6.0)*(p1 + p2 + p3 - n1 - n2 - n3);
}
function calculateVolume(object){
    var volumes = 0.0;
    var negative_volumes = 0.0;
    for(var i = 0; i < object.geometry.faces.length; i++){
        var Pi = object.geometry.faces[i].a;
        var Qi = object.geometry.faces[i].b;
        var Ri = object.geometry.faces[i].c;
         //console.log('object.geometry.vertices',object.geometry.vertices[Pi]);
        var P = new THREE.Vector3(object.geometry.vertices[Pi].x, object.geometry.vertices[Pi].y, object.geometry.vertices[Pi].z);
        var Q = new THREE.Vector3(object.geometry.vertices[Qi].x, object.geometry.vertices[Qi].y, object.geometry.vertices[Qi].z);
        var R = new THREE.Vector3(object.geometry.vertices[Ri].x, object.geometry.vertices[Ri].y, object.geometry.vertices[Ri].z);
        var volume = volumeOfT(P, Q, R);
        volumes += volume;
    }
    return Math.abs(volumes + negative_volumes);
}

now it work with:

    var geometry = new THREE.Geometry().fromBufferGeometry( mesh.geometry );
        console.log( geometry.faces.length );


        var geomesh = new THREE.Mesh(
                    geometry ,
                    new THREE.MeshBasicMaterial( { color: 0xffffff, opacity: 0.5, transparent: true } )
                );
        var volumeMesh = calculateVolume(geomesh)/1000;

@Mugen87 Uhm, are we supporting help questions on this site now? If so, we should change the posted policy.

Sry, it was just so obvious...

@Mugen87 I understand.

Thank you so much for your help, where can I talk about threejs's problem? Is possible here?

@videoluce you can ask over on the forum at http://discourse.threejs.org, or at https://stackoverflow.com/questions/tagged/three.js.

thank you

Was this page helpful?
0 / 5 - 0 ratings

Related issues

scrubs picture scrubs  路  3Comments

konijn picture konijn  路  3Comments

filharvey picture filharvey  路  3Comments

seep picture seep  路  3Comments

donmccurdy picture donmccurdy  路  3Comments