diff --git a/src/diffusers/utils/export_utils.py b/src/diffusers/utils/export_utils.py index 5ceb3c5b3452..a5ce425998bd 100644 --- a/src/diffusers/utils/export_utils.py +++ b/src/diffusers/utils/export_utils.py @@ -101,7 +101,7 @@ def export_to_ply(mesh, output_ply_path: str = None): return output_ply_path -def export_to_obj(mesh, output_obj_path: str = None): +def export_to_obj(mesh, output_obj_path: str = None) -> str: if output_obj_path is None: output_obj_path = tempfile.NamedTemporaryFile(suffix=".obj").name @@ -120,6 +120,8 @@ def export_to_obj(mesh, output_obj_path: str = None): with open(output_obj_path, "w") as f: f.writelines("\n".join(combined_data)) + return output_obj_path + def _legacy_export_to_video( video_frames: list[np.ndarray] | list[PIL.Image.Image], output_video_path: str = None, fps: int = 10 diff --git a/tests/others/test_utils.py b/tests/others/test_utils.py index dd4ed7c6c1ae..bacf732bac34 100755 --- a/tests/others/test_utils.py +++ b/tests/others/test_utils.py @@ -503,3 +503,44 @@ def is_staging_test(test_case): return pytest.mark.skip("test is staging test")(test_case) else: return pytest.mark.is_staging_test()(test_case) + + +class TestExportToObj: + @staticmethod + def _dummy_mesh(): + import types + + return types.SimpleNamespace( + verts=torch.tensor([[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]), + faces=torch.tensor([[0, 1, 2]]), + vertex_channels={ + "R": torch.tensor([1.0, 0.0, 0.0]), + "G": torch.tensor([0.0, 1.0, 0.0]), + "B": torch.tensor([0.0, 0.0, 1.0]), + }, + ) + + def test_export_to_obj_returns_temp_path_when_none(self): + from diffusers.utils import export_to_obj + + out_path = export_to_obj(self._dummy_mesh()) + try: + assert isinstance(out_path, str) + assert out_path.endswith(".obj") + assert os.path.isfile(out_path) + with open(out_path) as f: + lines = f.read().splitlines() + assert [line.split(" ", 1)[0] for line in lines] == ["v", "v", "v", "f"] + assert lines[-1] == "f 1 2 3" + finally: + if out_path and os.path.exists(out_path): + os.remove(out_path) + + def test_export_to_obj_returns_explicit_path(self, tmp_path): + from diffusers.utils import export_to_obj + + target_path = str(tmp_path / "mesh.obj") + returned = export_to_obj(self._dummy_mesh(), output_obj_path=target_path) + + assert returned == target_path + assert os.path.isfile(target_path)