So thats my code. The problem Im getting is that I would like to get “model” with the “object” inside it to be able to spin my 3d model around, but I get undefined when I do console.log(model). What Im doing wrong?
var loader = new OBJLoader();
var model
loader.load( 'models/cono.obj', function ( object ) {
object.name = "topota";
object.traverse(function(child) {
if (child instanceof THREE.Mesh){
child.material = material;
child.name = "obj";
model = child.clone()
}
});
scene.add( object );
});
console.log(model)
The lesson to take from it, is that you should indent your code properly and keep it clean. :’)
var model; // <- 1. This happens first, it's synchronous.
loader.load( 'models/cono.obj', function ( object ) { // <- 2. Loading START happens second, it's still synchronous.
object.name = "topota";
object.traverse(function(child) {
if (child instanceof THREE.Mesh){
child.material = material;
child.name = "obj";
model = child.clone() // <- 4. This happens last, it's asynchronous, since it's in a callback of `loader.load`, and is executed only AFTER the model is loaded.
}
});
scene.add( object );
});
console.log(model); // <- 3. This happens third, it's synchronous.
While console.log
can happen immediately, model = ...
will happen only after the model is loaded. So basically you try to log a model that is not loaded at that point, yet.
Got it. So I should be able to reference the object in the code to animate it then. I Will try.
About the code… I just pasted directly from vs code… I dont know how it looks that terrible in here!!
Thanks mjurczyk
If you move console.log(model)
a line below scene.add( object );
it should work fine.
1 Like
Yea its working now. I understand how it works now! awesome