I am currently facing an issue with checking if an object is already present in a three.js scene and then replacing it. It seems to be more of a scope problem rather than a specific three.js issue.
My scenario involves a form that pops up upon clicking, allowing users to customize options related to a model. Upon submission, I need the application to verify if an object already exists at the designated location (determined by assigning a name to the object during creation). If something is already there, the goal is to replace it with the new selection.
While I have managed to implement the checking mechanism successfully, I am encountering difficulties with the actual replacement:
var places = [];
$('#submit').off().on("click", function(){
//create a variable that stores info from the selections
place = motifClass.toString() + placeValue.toString();
motifChecker(place);
if (places.indexOf(place) === -1 ){
places.push(place);
};
….
var loader = new STLLoader().load(‘/assets/object.stl’, function( geometry ){
…
mesh.name = place;
place = "";
scene.add(mesh);
};
});
function motifChecker(){
var existing = scene.getObjectByName(place);
if ( places.includes(place) ){
scene.remove(existing);
}
};
The current code successfully removes the object but does not appear to replace it. Even though my browser console shows that the new .stl file is loaded, it fails to display on the screen.