Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/diffusers/utils/export_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
41 changes: 41 additions & 0 deletions tests/others/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)