I can’t access the glTF model properties after “adding” a clone of the model to the Object3D instance.
Steps to reproduce the behavior:
- Load the glTF model using the
GLTF()
loader. - Create an
Object3D
instance. - Add the loaded model to the
Object3D
instance. - Try to access the
Object3D
child[0] (added model) to change its color.
Code
// code goes here
class ModelCreator {
constructor(model_path, model_containers,
model_positions, model_rotation, model_color, model_names, scene) {
this.model_path = model_path;
this.model_containers = model_containers;
this.model_positions = model_positions;
this.model_rotation = model_rotation;
this.model_color = model_color;
this.model_names = model_names;
this.scene = scene;
this.loader = new GLTFLoader();
}
create = () => {
this.loadModel();
this.duplicateModel();
}
loadModel =()=> {
const onLoad = (result, model_containers) => {
const model = result.scene.children[0];
model.position.set(0.0, 0.0, 0.0);
model.scale.set(0.05, 0.05, 0.05);
model.material.color.set(this.model_color)
for (let i = 0; i < model_containers.length; i++) {
model_containers[i].add(model.clone());
}
};
this.loader.load(
this.model_path,
gltf => onLoad(gltf, this.model_containers),
), undefined, function (error) {
console.log('An error happened');
};
}
duplicateModel = () => {
for (let i = 0; i < this.model_containers.length; i++) {
this.model_containers[i].translateX(this.model_positions[i].x);
this.model_containers[i].translateY(this.model_positions[i].y);
this.model_containers[i].translateZ(this.model_positions[i].z);
if (this.model_rotation) {
this.model_containers[i].rotateY(Math.PI/2);
}
/* Supplementary Information */
this.model_containers[i].userData.color = this.model_color;
this.model_containers[i].name = this.model_names[i];
this.scene.add(this.model_containers[i]);
}
}
}
export function createLego(position, name, scene, size=2, rot=false) {
let lego = [new Object3D()];
let lego_color = new Color(0x4D4D4D);
let model_path = lego_2x2_path;
const legoCreator_ = new ModelCreator(model_path, lego, [position],
rot, lego_color, [name], scene);
legoCreator_.create();
}
...
/* This function has no access to the Object3D child model, it generates an error! */
function changeColorLego(scene, name, color) {
// This part works well and I can see my object in the console
console.log(scene.getObjectByName(name).children);
// children[0] is not accessible!
scene.getObjectByName(name).children[0].material.color.set(color);
}
ERROR Message:
TypeError: scene.getObjectByName(...).children[0] is undefined
I can access console.log(scene.getObjectByName(...).children)
and see that my model is inside the object which means that my model is already loaded, but I have no access to the children[0]
to change the properties, and console.log(scene.getObjectByName(...).children)
yields 0
, but my model is already loaded as I can get an output from console.log(scene.getObjectByName(...).children)
which is the object.
Expected behavior
To be able to access the Object3D
children and change its properties.
Platform:
- Device: [Desktop]
- OS: [Windows]
- Browser: [Firefox]
- Three.js version: [r1440]