ObjExporter can't export userData

const mesh = new THREE.Mesh(geo, mat);
mesh.userData = {
    'test': 'testData'
};
const exporter = new OBJExporter();
exporter.parse(mesh);

Then i found the exported obj file lost userData.
How can i retain my data after exported?

OBJ file format is rather basic. As far as I know, it does not support custom user data. You can try some unconventional tricks, like storing user data as a string in the name property:

const mesh = new THREE.Mesh(geo, mat);
mesh.userData = {
    'test': 'testData'
};
const exporter = new OBJExporter();
mesh.name = JSON.stringify( mesh.userData ); // try this
exporter.parse(mesh);
1 Like

but it has comments, so the exporter could dump the data in the comments and the resulting file would be readable - the user then would be responsible for parsing the comments if they want to. I’d say, a PR like that has about 50% chance to be accepted.

If this is the case, the user should also be responsible for generating the comments.

Edit: what I mean is that the OBJ file is quire restricted. Why should the exporter push the user data into comments? What about all the other attributes that are not part of the OBJ specification? And why an exporter will export something that the importer cannot import?

1 Like

well they could edit the exporter and keep the copy in their code - the way examples folder was meant to be used, originally

2 Likes

Yes, this is also possible. The exporter is fairly simple, so adding such functionality is not hard.

you would not have exif in jpegs if they thought this way

There are many vendor-specific extensions of the OBJ format that can put various data in it. If it is needed to store data that is not a part of the original format, it is better to make own extension of OBJ instead of overloading existing OBJ elements with some extra semantics. However, nobody is stopping anyone from doing any of these. I do not want to go deeper into discussing OBJs. Let’s just assume that you are correct. Please, accept my apologies.

how is it better though, making an extension would break the loader that isnt smart enough to ignore the data it does not know. comments, on the other hand, are skipped universally

Okay,well,thank you for this black magic.

2 Likes

OBJ is a pretty braindead format. Imo if you want userData use threejs exported GLTF or the threejs json format. both text / human readable / easier to parse than obj… and supporting all the structural richness.

Obj 1 based indexing of vertices… along with the wackyness of faces with partial index definitions… is enough to make me kinda hate it as a format… Also a lot of OBJ consuming parsers are braindead as well and might choke on json comments.