Need advice on userData with Threejs Editor

I am a designer using the ThreeJS editor. I do not assign a script to an object very often because I have very limited coding knowledge. But the editor has been an amazing resource for me regardless of my lacking coding skills.

I have been very interested in learning more about what I can do with the “User data” window that is located in the threejs editor.

I have done a lot of searching about userdata and it seems you can add any data you want there… I’ve seen several people searching on how to make an object a clickable weblink with userData for example…

I wanted to ask the community

  1. Can I use the User data window to add functionality to an object without also creating a script? Or a script of some kind is always needed to interact with the user data created?

  2. Does anyone have suggestions of use User data snippets I can plug into the User data window with in the editor?

I really appreciate the suggestions as it will help me learn a lot more about how to use userData!!

Thank you!!

It’s just a simple javascript data structure - object / dictionary. Being javascript, anything can be a member of that object. Given some key, the value can be a number, string, Boolean (primitives), or other complex objects including functions.

I never worked with the editor, but you should be able to save a function there, and then evaluate it once you load the object. Of course you’d have to “script” the function yourself, or find one that sets up the js context so that it could address your object Appropriately.

Thank you that explains a lot! I appreciate it!

BTW take care when storing anything that can’t be serialized in userData if you plan to use Object3D.copy or Object3D.clone.

Functions won’t be copied or cloned, for example, and will be ignored. More complex object will cause a crash (I know this because I tried to store an AnimationMixer instance in there).

I’m not sure what the reason for serializing userData is, IMO it’s useful to be able to store simple functions in there.

2 Likes

As it’s exported in json, to store some state etc. For methods or other cases it makes sense to create a new class extending Mesh or Object3D.

1 Like

Thank you, that is helpful info! I am just getting started with things but I am sure I will need to use copy/clone at some point