Skip to content

fix parsing of state-setting OBJ attributes (ex: material_name) - #122

Merged
ffreyer merged 2 commits into
JuliaIO:masterfrom
jrbaxter0:fix-attribute-state-setting
Sep 16, 2026
Merged

ffreyer merged 2 commits into
JuliaIO:masterfrom
jrbaxter0:fix-attribute-state-setting

Conversation

@jrbaxter0

Copy link
Copy Markdown
Contributor

Group name (g), smoothing group (s), object name (o), and material (usemtl) statements in OBJ files are all state-setting, meaning they continue to take effect until explicitly changed.1 The current parser does not respect this, and this patch fixes it, enabling correct loading of complex textured MetaMeshes.

Simple Example

In this OBJ file snippet, both groups face0 and face should have material0 applied, but the current parser only applies it to the former:

usemtl material0
g face0
f 1/1/1 2/2/2 103/103/103
g face1
f 1/1/1 103/103/103 102/102/102

It should be parsed as:

my_meta_mesh.meta[:material_names]
2-element Vector{String}:
 "material0"
 "material0"

but it is instead parsed as:

my_meta_mesh.meta[:material_names]
2-element Vector{Union{String,Nothing}}:
 "material0"
 nothing

Plotting the latter with Makie via mesh(my_meta_mesh) will result in the error: KeyError: key nothing not found.

Realistic Example

The earlier example is small to make it understandable, but a more realistic test can be done with this file:
Drone.zip (OBJ export of https://github.com/SimonDanisch/RayDemo/blob/main/GLTF/Drone.glb)

using GLMakie, FileIO, MeshIO
drone = load("./Drone.obj")
mesh(drone)

With the patch, the drone will be correctly rendered with a black body, silver landing gear, and orange-striped propellors.
Without the patch, it will fail:

ERROR: KeyError: key nothing not found
Stacktrace:
  [1] getindex(h::Dict{String, Dict{String, Any}}, key::Nothing)
    @ Base ./dict.jl:477
  [2] plot!(p::Mesh{Tuple{GeometryBasics.MetaMesh{3, Float32, GeometryBasics.Mesh{…}}}})
    @ Makie ~/.julia/packages/Makie/p9K7f/src/basic_recipes/mesh.jl:17
  [3] connect_plot!(parent::Scene, plot::Mesh{Tuple{GeometryBasics.MetaMesh{3, Float32, GeometryBasics.Mesh{…}}}})
    @ Makie ~/.julia/packages/Makie/p9K7f/src/compute-plots.jl:855
  [4] plot!
    @ ~/.julia/packages/Makie/p9K7f/src/interfaces.jl:211 [inlined]
  [5] plot!(ax::LScene, plot::Mesh{Tuple{GeometryBasics.MetaMesh{3, Float32, GeometryBasics.Mesh{…}}}})
    @ Makie ~/.julia/packages/Makie/p9K7f/src/figureplotting.jl:437
  [6] plot!(fa::Makie.FigureAxis, plot::Mesh{Tuple{GeometryBasics.MetaMesh{3, Float32, GeometryBasics.Mesh{…}}}})
    @ Makie ~/.julia/packages/Makie/p9K7f/src/figureplotting.jl:433
  [7] _create_plot(F::Function, attributes::Dict{…}, args::GeometryBasics.MetaMesh{…})
    @ Makie ~/.julia/packages/Makie/p9K7f/src/figureplotting.jl:336
  [8] #mesh#59
    @ ~/.julia/packages/Makie/p9K7f/src/recipes.jl:530 [inlined]
  [9] mesh(args::GeometryBasics.MetaMesh{3, Float32, GeometryBasics.Mesh{…}})
    @ Makie ~/.julia/packages/Makie/p9K7f/src/recipes.jl:528
 [10] top-level scope
    @ REPL[3]:1
Some type information was truncated. Use `show(err)` to see complete types.

Footnotes

  1. https://paulbourke.net/dataformats/obj/ ↩

@codecov

codecov Bot commented Sep 14, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.73684% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 79.47%. Comparing base (fe63a6a) to head (1911937).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/io/obj.jl 94.73% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #122      +/-   ##
==========================================
- Coverage   79.49%   79.47%   -0.03%     
==========================================
  Files          11       11              
  Lines         839      843       +4     
==========================================
+ Hits          667      670       +3     
- Misses        172      173       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ffreyer

ffreyer commented Sep 15, 2026

Copy link
Copy Markdown
Collaborator

Would be good to have a test with a (small) obj file that fails to load correctly without these changes

@jrbaxter0

Copy link
Copy Markdown
Contributor Author

Would be good to have a test with a (small) obj file that fails to load correctly without these changes

Done! I added a small six-sided die which explicitly names each face (via vertex groups in Blender). It ends up being very similar to the "simple example" given in my initial message.

@jrbaxter0
jrbaxter0 force-pushed the fix-attribute-state-setting branch from 10cc990 to 1911937 Compare September 15, 2026 12:51
@ffreyer
ffreyer merged commit e1caab2 into JuliaIO:master Sep 16, 2026
4 checks passed
@ffreyer

ffreyer commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants