-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clone objects with userData that isn't JSON encodable? #23956
Comments
I don't vote to support this use case. It's over-engineering. I would rather suggest to clarify the documentation. https://threejs.org/docs/index.html#api/en/core/Object3D.userData The docs already mentioned a limitation about functions. It should be no deal to enhance this section about cyclic references. |
It's not so much about cyclic references, though cyclic references are fairly unique in the fact that the thrown error just completely prevents cloning... it's more about the constraint on JSON serialization. (Admittedly modern) standard types like I agree that the extra option is over-engineering, but I guess it would be a breaking change since functions would throw when cloning, rather than simply being ignored. But if three wants to try and deep-clone |
How would you stringify an object using it? |
You can't. That does seem like a separate bit of functionality to me though. Cloning and serialization are separate concerns, evidenced by the fact that Three.js internally has separate hand-written I'm not certain that JSON round-trip:
structuredClone:
|
Sorry, but this feature request is out of scope. |
@willstott101 Instead of traversing and removing the cyclical references, why not use Symbols as keys instead? Symbol keys are ignored by JSON.stringify |
If it was data I wanted excluded from clone I could just add my own "realUserData" field or something. This ticket is about extended what kind of data is supported in user data by three.js itself. I can store my own data elsewhere if I didn't want three.js' existing clone to work |
Is your feature request related to a problem? Please describe.
I cannot clone my scene (which I wanted to do for export purposes, see #23951) because my userData has got cyclic references in it. Now on the one hand I'd concede perhaps it shouldn't, but it does seem like an unnecessary limitation to me.
Describe the solution you'd like
A) An extra option to the clone/copy functions to toggle the copying of userData.
B) Use of
structuredClone
in environments that support it overJSON.parse(JSON.stringify(x))
I think I should have the time to implement one of these if they're likely to get merged.
Describe alternatives you've considered
Traversing and removing my userData before cloning, and then re-adding it.
The text was updated successfully, but these errors were encountered: