[SOLVED] Can not edit object from FBX loader outside of loader

Dear reader,

I have a question concerning the editing of objects outside of the loader.load function from THREE.FBXLoader.
I have the following function to load in an FBX model:

    function FBXLoader(sourceFile, shotName, pos){
    var obj = new THREE.Object3D();
    var loader = new THREE.FBXLoader();
		loader.load( sourceFile, function( object ) {
            obj = object;
            scene.add(obj);
            obj.name = shotName;
            obj.position.x = -(pos.x - compensationPos.x);
            obj.position.y = pos.y - compensationPos.y;
            obj.position.z = pos.z - compensationPos.z;
            obj.scale.set(100,100,100);
            console.log('visible icon of type ', typeof(obj), ' placed at ', obj.position);
        });
        return obj;
    }, 

which works as expected. it loads the model, and changes its name/position to shotname and pos, and increases the scale.

However when i try to edit the object outside of the Loader.load function nothing happens, despite passing the loaded object to a new Object3D “obj”, like this:

function FBXLoader(sourceFile, shotName, pos){
var obj = new THREE.Object3D();
var loader = new THREE.FBXLoader();
		loader.load( sourceFile, function( object ) {
        obj = object;
    });
        //editing obj from here does not do anything, it won't get added to the scene either
        scene.add(obj);
        obj.name = shotName;
        obj.position.x = -(pos.x - compensationPos.x);
        obj.position.y = pos.y - compensationPos.y;
        obj.position.z = pos.z - compensationPos.z;
        obj.scale.set(100,100,100);
        console.log('visible icon of type ', typeof(obj), ' placed at ', obj.position);
    return obj;
},

Is there something i am doing wrong/overseeing here?

Thanks for your time,
~Remy

Your initial obj variable is overwritten by the onLoad() callback of FBXLoader.load(). The error becomes more obvious if you just declare obj. So instead of

var obj = new THREE.Object3D();

do

var obj;

Be aware that the onLoad() callback is asynchronous. You have to respect this in your code.

2 Likes

Oke, thanks for your swift reply!