How can I set an object's userData using a script in the editor?

Hello all. I’m trying to create a lit scene in the THREE.js Editor, but I was having trouble exporting shadow casting / receiving to GLTF. I decided to add a script to the scene that iterates through all children, and if it has shadow casting enabled, it would add that to the userData of the child (like below). Unfortunately, it looks like the userData change doesn’t reflect to the actual editor, and is overwritten immediately. Is there a way to get around this, or am I just doing something wrong?

function start( event ) {
	this.traverse(child => {
		if (child.castShadow) {
			child.userData.castShadow = true;
			console.log(child); // reflects userData change, but after the start() function completes, the changes are lost
		}
	});
}

Edit: I’ve also noticed that my script can’t properly read the userData either. Regardless of what’s actually stored in there (set using the text field), it always shows as an empty JSON object (but not undefined).

Hate to answer my own question so soon, but for anybody in the future having this problem, I did find a solution.

It looks like the this object passed to the script is only a copy of the true Scene object, making it effectively read-only; any writes to the object won’t be reflected back to the original.

I was able to work around this by writing the following:

function start(event) {
    let scene = window.editor.scene;
		if (child.castShadow) {
			child.userData.castShadow = true;
			console.log(child);
		}
}

This accesses the original Scene object, not just the copy, and thus allows changes.

(I could be wrong on the cause of this bug, but the above code does work as intended)

1 Like