Wavefront OBJ Format -> Groups & usemtl command
Asked Answered
P

2

8

So I'm writing a parser for wavefront obj model files and there are a few irregularities that I'm not sure how to handle.

So based off of my reading, a mesh can be broken up into groups using the 'g' command and a material can be assigned to each group using the 'usemtl' command

So an ideal file would look like this:

g group1
usemtl material1
//vertices
//UV coords
//faces

g group2
usemtl material2
//vertices
//UV coords
//faces

etc....

However in some obj files I've downloaded (from places like Turbosquid), I've seen a single group contain multiple "usemtl" like this:

g group1
usemtl material1
//vertices, faces, etc 
usemtl material2 
//vertices, faces, etc

g group2 
usematerial material3
//vertices, faces, etc

So if there can be multiple materials per group then what is the point of a group?

Are these files considered "non-standard" or broken?

Should I instead be grouping faces based on shared material instead of shared group?

Having multiple materials per group would complicate a lot of my code (for instance - let's say I have to generate a set of N random samples on a group of triangles/faces with a certain material. If there's just one material per group I can just look up the triangles in that group and generate samples. But if that group contained some triangles with the correct material and some without, I'd have to do some weird material checking on top of the group checking to generate the right samples. This is just one example - there are others where this becomes an issue as well)

Pauperism answered 1/6, 2015 at 20:5 Comment(0)
I
3

Obj meshes can be grouped not only into group items ( g), but objects (o) and smooth zones (s) Too. The easiest way is making faces reference the current material specified by usemtl, or just group by usemtl

Ivey answered 17/8, 2017 at 8:22 Comment(2)
In my case, I group by usemtl. Each new "usemtl" keyword defined a new group with same name as the last group with a counter sufix. Works great!Outgrow
@Ivey What is the difference between an object and a group item?Solution
W
1

The point of a group is to allow for easy transformations. For example, if I have an avatar made up of different meshes for skin, shirt, eyeball, etc., it is very convenient to group all the meshes so that I can rigidly rotate and translate or scale the avatar as one group. Note that this allows different elements of the group to have different materials. The skin doesn't have to have the same material as the eyeball.

Scroll down to "Grouping" in this link: http://paulbourke.net/dataformats/obj/

The difference between a "group" and an "object" element in an OBJ file is not as clear to me, other than the fact that a mesh can be assigned to more than one group while the same is not apparently true for assigning it to an object. Paul Bourke has an example of nested groups for a cube in the above link.

Wiesbaden answered 20/9, 2022 at 20:45 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Fowlkes

© 2022 - 2024 — McMap. All rights reserved.