Skip to content

Commit

Permalink
Importing obj files without usemtl
Browse files Browse the repository at this point in the history
Summary:
When there is no "usemtl" statement in the .obj file use material from .mtl if there is one.
#1068

Reviewed By: bottler

Differential Revision: D34141152

fbshipit-source-id: 7a5b5cc3f0bb287dc617f68de2cd085db8f7ad94
  • Loading branch information
seovchinnikovfb authored and facebook-github-bot committed Feb 10, 2022
1 parent 12f20d7 commit ef21a6f
Show file tree
Hide file tree
Showing 7 changed files with 12,062 additions and 4 deletions.
12 changes: 11 additions & 1 deletion pytorch3d/io/mtl_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,18 @@ def _load_texture_images(
final_material_properties = {}
texture_images = {}

used_material_names = list(material_names)
if not used_material_names and material_properties:
if len(material_properties) > 1:
raise ValueError(
"Multiple materials but no usemtl declarations in the obj file"
)
# No materials were specified in obj file and only one is in the
# specified .mtl file, so we use it.
used_material_names.append(next(iter(material_properties.keys())))

# Only keep the materials referenced in the obj.
for material_name in material_names:
for material_name in used_material_names:
if material_name in texture_files:
# Load the texture image.
path = os.path.join(data_dir, texture_files[material_name])
Expand Down
12 changes: 9 additions & 3 deletions pytorch3d/io/obj_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,8 @@ def _load_materials(
if not load_textures:
return None, None

if not material_names or f is None:
if material_names:
warnings.warn("No mtl file provided")
if f is None:
warnings.warn("No mtl file provided")
return None, None

if not path_manager.exists(f):
Expand Down Expand Up @@ -620,6 +619,13 @@ def _load_obj(
device=device,
)

if material_colors and not material_names:
# usemtl was not present but single material was present in the .mtl file
material_names.append(next(iter(material_colors.keys())))
# replace all -1 by 0 material idx
if torch.is_tensor(faces_materials_idx):
faces_materials_idx.clamp_(min=0)

if create_texture_atlas:
# Using the images and properties from the
# material file make a per face texture map.
Expand Down
7 changes: 7 additions & 0 deletions tests/data/missing_usemtl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Acknowledgements

This is copied version of docs/tutorials/data/cow_mesh with removed line 6159 (usemtl material_1) to test behavior without usemtl material_1 declaration.

Thank you to Keenen Crane for allowing the cow mesh model to be used freely in the public domain.

###### Source: http://www.cs.cmu.edu/~kmcrane/Projects/ModelRepository/
9 changes: 9 additions & 0 deletions tests/data/missing_usemtl/cow.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
newmtl material_1
map_Kd cow_texture.png

# Test colors

Ka 1.000 1.000 1.000 # white
Kd 1.000 1.000 1.000 # white
Ks 0.000 0.000 0.000 # black
Ns 10.0
Loading

0 comments on commit ef21a6f

Please sign in to comment.