Skip to content

Commit ef21a6f

Browse files
Importing obj files without usemtl
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
1 parent 12f20d7 commit ef21a6f

File tree

7 files changed

+12062
-4
lines changed

7 files changed

+12062
-4
lines changed

pytorch3d/io/mtl_io.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,18 @@ def _load_texture_images(
454454
final_material_properties = {}
455455
texture_images = {}
456456

457+
used_material_names = list(material_names)
458+
if not used_material_names and material_properties:
459+
if len(material_properties) > 1:
460+
raise ValueError(
461+
"Multiple materials but no usemtl declarations in the obj file"
462+
)
463+
# No materials were specified in obj file and only one is in the
464+
# specified .mtl file, so we use it.
465+
used_material_names.append(next(iter(material_properties.keys())))
466+
457467
# Only keep the materials referenced in the obj.
458-
for material_name in material_names:
468+
for material_name in used_material_names:
459469
if material_name in texture_files:
460470
# Load the texture image.
461471
path = os.path.join(data_dir, texture_files[material_name])

pytorch3d/io/obj_io.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,9 +526,8 @@ def _load_materials(
526526
if not load_textures:
527527
return None, None
528528

529-
if not material_names or f is None:
530-
if material_names:
531-
warnings.warn("No mtl file provided")
529+
if f is None:
530+
warnings.warn("No mtl file provided")
532531
return None, None
533532

534533
if not path_manager.exists(f):
@@ -620,6 +619,13 @@ def _load_obj(
620619
device=device,
621620
)
622621

622+
if material_colors and not material_names:
623+
# usemtl was not present but single material was present in the .mtl file
624+
material_names.append(next(iter(material_colors.keys())))
625+
# replace all -1 by 0 material idx
626+
if torch.is_tensor(faces_materials_idx):
627+
faces_materials_idx.clamp_(min=0)
628+
623629
if create_texture_atlas:
624630
# Using the images and properties from the
625631
# material file make a per face texture map.

tests/data/missing_usemtl/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Acknowledgements
2+
3+
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.
4+
5+
Thank you to Keenen Crane for allowing the cow mesh model to be used freely in the public domain.
6+
7+
###### Source: http://www.cs.cmu.edu/~kmcrane/Projects/ModelRepository/

tests/data/missing_usemtl/cow.mtl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
newmtl material_1
2+
map_Kd cow_texture.png
3+
4+
# Test colors
5+
6+
Ka 1.000 1.000 1.000 # white
7+
Kd 1.000 1.000 1.000 # white
8+
Ks 0.000 0.000 0.000 # black
9+
Ns 10.0

0 commit comments

Comments
 (0)