Skip to content

Commit b18836b

Browse files
committed
rename normal parameters of save_obj
1 parent 653e498 commit b18836b

File tree

2 files changed

+33
-32
lines changed

2 files changed

+33
-32
lines changed

pytorch3d/io/obj_io.py

+25-24
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,8 @@ def save_obj(
684684
decimal_places: Optional[int] = None,
685685
path_manager: Optional[PathManager] = None,
686686
*,
687-
verts_normals: Optional[torch.Tensor] = None,
688-
faces_normals: Optional[torch.Tensor] = None,
687+
normals: Optional[torch.Tensor] = None,
688+
faces_normals_idx: Optional[torch.Tensor] = None,
689689
verts_uvs: Optional[torch.Tensor] = None,
690690
faces_uvs: Optional[torch.Tensor] = None,
691691
texture_map: Optional[torch.Tensor] = None,
@@ -700,9 +700,9 @@ def save_obj(
700700
decimal_places: Number of decimal places for saving.
701701
path_manager: Optional PathManager for interpreting f if
702702
it is a str.
703-
verts_normals: FloatTensor of shape (V, 3) giving the normal per vertex.
704-
faces_normals: LongTensor of shape (F, 3) giving the index into verts_normals
705-
for each vertex in the face.
703+
normals: FloatTensor of shape (V, 3) giving the normal per vertex.
704+
faces_normals_idx: LongTensor of shape (F, 3) giving the index into
705+
normals for each vertex in the face.
706706
verts_uvs: FloatTensor of shape (V, 2) giving the uv coordinate per vertex.
707707
faces_uvs: LongTensor of shape (F, 3) giving the index into verts_uvs for
708708
each vertex in the face.
@@ -718,12 +718,13 @@ def save_obj(
718718
message = "'faces' should either be empty or of shape (num_faces, 3)."
719719
raise ValueError(message)
720720

721-
if faces_normals is not None and (faces_normals.dim() != 2 or faces_normals.size(1) != 3):
722-
message = "'faces_normals' should either be empty or of shape (num_faces, 3)."
721+
if faces_normals_idx is not None and \
722+
(faces_normals_idx.dim() != 2 or faces_normals_idx.size(1) != 3):
723+
message = "'faces_normals_idx' should either be empty or of shape (num_faces, 3)."
723724
raise ValueError(message)
724725

725-
if verts_normals is not None and (verts_normals.dim() != 2 or verts_normals.size(1) != 3):
726-
message = "'verts_normals' should either be empty or of shape (num_verts, 3)."
726+
if normals is not None and (normals.dim() != 2 or normals.size(1) != 3):
727+
message = "'normals' should either be empty or of shape (num_verts, 3)."
727728
raise ValueError(message)
728729

729730
if faces_uvs is not None and (faces_uvs.dim() != 2 or faces_uvs.size(1) != 3):
@@ -741,7 +742,7 @@ def save_obj(
741742
if path_manager is None:
742743
path_manager = PathManager()
743744

744-
save_normals = all([n is not None for n in [verts_normals, faces_normals]])
745+
save_normals = all([n is not None for n in [normals, faces_normals_idx]])
745746
save_texture = all([t is not None for t in [faces_uvs, verts_uvs, texture_map]])
746747
output_path = Path(f)
747748

@@ -756,8 +757,8 @@ def save_obj(
756757
verts,
757758
faces,
758759
decimal_places,
759-
verts_normals=verts_normals,
760-
faces_normals=faces_normals,
760+
normals=normals,
761+
faces_normals_idx=faces_normals_idx,
761762
verts_uvs=verts_uvs,
762763
faces_uvs=faces_uvs,
763764
save_texture=save_texture,
@@ -794,8 +795,8 @@ def _save(
794795
faces,
795796
decimal_places: Optional[int] = None,
796797
*,
797-
verts_normals: Optional[torch.Tensor] = None,
798-
faces_normals: Optional[torch.Tensor] = None,
798+
normals: Optional[torch.Tensor] = None,
799+
faces_normals_idx: Optional[torch.Tensor] = None,
799800
verts_uvs: Optional[torch.Tensor] = None,
800801
faces_uvs: Optional[torch.Tensor] = None,
801802
save_texture: bool = False,
@@ -830,22 +831,22 @@ def _save(
830831
lines += "v %s\n" % " ".join(vert)
831832

832833
if save_normals:
833-
if faces_normals is not None and (faces_normals.dim() != 2 or faces_normals.size(1) != 3):
834-
message = "'faces_normals' should either be empty or of shape (num_faces, 3)."
834+
if faces_normals_idx is not None and (faces_normals_idx.dim() != 2 or faces_normals_idx.size(1) != 3):
835+
message = "'faces_normals_idx' should either be empty or of shape (num_faces, 3)."
835836
raise ValueError(message)
836837

837-
if verts_normals is not None and (verts_normals.dim() != 2 or verts_normals.size(1) != 3):
838-
message = "'verts_normals' should either be empty or of shape (num_verts, 3)."
838+
if normals is not None and (normals.dim() != 2 or normals.size(1) != 3):
839+
message = "'normals' should either be empty or of shape (num_verts, 3)."
839840
raise ValueError(message)
840841

841842
# pyre-fixme[16] # undefined attribute cpu
842-
verts_normals, faces_normals = verts_normals.cpu(), faces_normals.cpu()
843+
normals, faces_normals_idx = normals.cpu(), faces_normals_idx.cpu()
843844

844845
# Save verts normals after verts
845-
if len(verts_normals):
846-
V, D = verts_normals.shape
846+
if len(normals):
847+
V, D = normals.shape
847848
for i in range(V):
848-
normal = [float_str % verts_normals[i, j] for j in range(D)]
849+
normal = [float_str % normals[i, j] for j in range(D)]
849850
lines += "vn %s\n" % " ".join(normal)
850851

851852
if save_texture:
@@ -879,14 +880,14 @@ def _save(
879880
"%d/%d/%d" % (
880881
faces[i, j] + 1,
881882
faces_uvs[i, j] + 1,
882-
faces_normals[i, j] + 1,
883+
faces_normals_idx[i, j] + 1,
883884
)
884885
for j in range(P)
885886
]
886887
elif save_normals:
887888
# Format faces as {verts_idx}//{verts_normals_idx}
888889
face = [
889-
"%d//%d" % (faces[i, j] + 1, faces_normals[i, j] + 1) for j in range(P)
890+
"%d//%d" % (faces[i, j] + 1, faces_normals_idx[i, j] + 1) for j in range(P)
890891
]
891892
elif save_texture:
892893
# Format faces as {verts_idx}/{verts_uvs_idx}

tests/test_io_obj.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -903,12 +903,12 @@ def test_save_obj_with_normal(self):
903903
faces = torch.tensor(
904904
[[0, 2, 1], [0, 1, 2], [3, 2, 1], [3, 1, 0]], dtype=torch.int64
905905
)
906-
verts_normals = torch.tensor(
906+
normals = torch.tensor(
907907
[[0.02, 0.5, 0.73], [0.3, 0.03, 0.361], [0.32, 0.12, 0.47], [0.36, 0.17, 0.9],
908908
[0.40, 0.7, 0.19], [1.0, 0.00, 0.000], [0.00, 1.00, 0.00], [0.00, 0.00, 1.0]],
909909
dtype=torch.float32,
910910
)
911-
faces_normals = torch.tensor(
911+
faces_normals_idx = torch.tensor(
912912
[[0, 1, 2], [2, 3, 4], [4, 5, 6], [6, 7, 0]], dtype=torch.int64
913913
)
914914

@@ -919,8 +919,8 @@ def test_save_obj_with_normal(self):
919919
verts,
920920
faces,
921921
decimal_places=2,
922-
verts_normals=verts_normals,
923-
faces_normals=faces_normals,
922+
normals=normals,
923+
faces_normals_idx=faces_normals_idx,
924924
)
925925

926926
expected_obj_file = "\n".join(
@@ -1023,11 +1023,11 @@ def test_save_obj_with_normal_and_texture(self):
10231023
faces = torch.tensor(
10241024
[[0, 2, 1], [0, 1, 2], [3, 2, 1], [3, 1, 0]], dtype=torch.int64
10251025
)
1026-
verts_normals = torch.tensor(
1026+
normals = torch.tensor(
10271027
[[0.02, 0.5, 0.73], [0.3, 0.03, 0.361], [0.32, 0.12, 0.47], [0.36, 0.17, 0.9]],
10281028
dtype=torch.float32,
10291029
)
1030-
faces_normals = faces
1030+
faces_normals_idx = faces
10311031
verts_uvs = torch.tensor(
10321032
[[0.02, 0.5], [0.3, 0.03], [0.32, 0.12], [0.36, 0.17]],
10331033
dtype=torch.float32,
@@ -1042,8 +1042,8 @@ def test_save_obj_with_normal_and_texture(self):
10421042
verts,
10431043
faces,
10441044
decimal_places=2,
1045-
verts_normals=verts_normals,
1046-
faces_normals=faces_normals,
1045+
normals=normals,
1046+
faces_normals_idx=faces_normals_idx,
10471047
verts_uvs=verts_uvs,
10481048
faces_uvs=faces_uvs,
10491049
texture_map=texture_map,

0 commit comments

Comments
 (0)