tobj icon indicating copy to clipboard operation
tobj copied to clipboard

Objects and Groups behave differently

Open johannesvollmer opened this issue 6 years ago • 3 comments

Hi!

As assumed in line 655, https://github.com/Twinklebear/tobj/blob/023076f013036fda28a3399b2fdd0a86be4a3030/src/lib.rs#L655 Objects and groups are not the same and should, in theory, be handled differently.

Probably, only few people will need it. Nevertheless, I'll describe the difference here. You will have to decide on your own if that distinction is important enough to be implemented.

Objects

The code currently treats objects as expected. For objects, each name starts a new object and closes the old one. Two objects with the same name will result in two different objects that happen to have the same name.

Groups

In the code, groups are handled the same way as objects. Instead, groups should be treated as collections, accumulating data from anywhere in the OBJ file, as many times as desired. As a result, a group name appearing twice in a file should not result in two distinct groups, but instead the result should be a single group containing the geometry of both parts.

Summarizing: Duplicate objects stay separated, duplicate groups should be merged.

Cheers!

johannesvollmer avatar Sep 16 '19 11:09 johannesvollmer

Thanks @johannesvollmer , this is interesting! I didn't know about this difference between the two, I haven't seen a file that uses this duplicate group merging functionality. I'll leave this issue open in case someone else finds that this causes a loading bug for them, but it seems like a relatively rare use case so for now I'll leave the code as is.

Twinklebear avatar Sep 18 '19 17:09 Twinklebear

To be honest, I too have never seen such a file in my life 😁

johannesvollmer avatar Sep 19 '19 07:09 johannesvollmer

Just ran into this while loading OBJ files. An interesting consequence of this is that it can become impossible to get the name of the object if there are no faces before the first group.

Example file: https://github.com/ProtoArt/spritec/blob/28bf3b8b84a0c86e646d3b2d598be983a2e7c167/samples/bigboi/obj/bigboi_000001.obj

In this example file, we have something like this:

# Blender v2.81 (sub 16) OBJ File: 'bigboi.blend'
# www.blender.org
mtllib bigboi_000001.mtl
o Icosphere_bigboi
v 1.701308 0.850645 0.432616
v 1.051475 0.525738 -1.132616
...
g Icosphere_bigboi_torso
usemtl torso
s 1
f 220/1/1 231/2/2 14/3/3
f 232/4/4 9/5/5 16/6/6
...

All the vertices are defined at the object level, but none of the faces show up until the first group. The code below means that name will be overwritten with the name of the group and it won't be possible to get the object name.

https://github.com/Twinklebear/tobj/blob/e3d93b4f1e3039b98fb3ff333b1270f64e1d6ad0/src/lib.rs#L655-L673

This isn't a huge deal for what I'm working on right now, but it definitely would have been nice to be able to get a tobj::Mesh that exposes its own name and a groups: Vec<tobj::Mesh> field.

sunjay avatar Jan 07 '20 22:01 sunjay