From 96be9b22b8d538ef5fa445837482be5c6626ffb7 Mon Sep 17 00:00:00 2001 From: Aditya kharmale <134163659+AdityaKharmaleGoat7@users.noreply.github.com> Date: Fri, 9 Oct 2026 16:31:53 +0900 Subject: [PATCH] Make pymeshlab imports lazy in exporter utilities --- nerfstudio/exporter/exporter_utils.py | 4 +++- nerfstudio/exporter/tsdf_utils.py | 5 +++-- tests/test_exporter_imports.py | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 tests/test_exporter_imports.py diff --git a/nerfstudio/exporter/exporter_utils.py b/nerfstudio/exporter/exporter_utils.py index 3cd1fc2872..21ac101ad7 100644 --- a/nerfstudio/exporter/exporter_utils.py +++ b/nerfstudio/exporter/exporter_utils.py @@ -23,7 +23,6 @@ from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple import numpy as np -import pymeshlab import torch from jaxtyping import Float from rich.progress import BarColumn, Progress, TaskProgressColumn, TextColumn, TimeRemainingColumn @@ -41,6 +40,7 @@ # Importing open3d can take ~1 second, so only do it below if we actually # need it. import open3d as o3d + import pymeshlab @dataclass @@ -71,6 +71,8 @@ def get_mesh_from_pymeshlab_mesh(mesh: pymeshlab.Mesh) -> Mesh: # type: ignore def get_mesh_from_filename(filename: str, target_num_faces: Optional[int] = None) -> Mesh: """Get a Mesh from a filename.""" + import pymeshlab + ms = pymeshlab.MeshSet() # type: ignore ms.load_new_mesh(filename) if target_num_faces is not None: diff --git a/nerfstudio/exporter/tsdf_utils.py b/nerfstudio/exporter/tsdf_utils.py index f3f1e53571..427bea964c 100644 --- a/nerfstudio/exporter/tsdf_utils.py +++ b/nerfstudio/exporter/tsdf_utils.py @@ -23,7 +23,6 @@ from typing import List, Optional, Tuple, Union import numpy as np -import pymeshlab import torch import torch.nn.functional as F from jaxtyping import Bool, Float @@ -50,7 +49,7 @@ class TSDF: weights: Float[Tensor, "xdim ydim zdim"] """TSDF weights for each voxel.""" colors: Float[Tensor, "xdim ydim zdim 3"] - """TSDF colors for each voxel.""" + """TSDF colors of each voxel.""" voxel_size: Float[Tensor, "3"] """Size of each voxel in the TSDF. [x, y, z] size.""" origin: Float[Tensor, "3"] @@ -147,6 +146,8 @@ def export_mesh(cls, mesh: Mesh, filename: str) -> None: mesh: The mesh to export. filename: The filename to export the mesh to. """ + import pymeshlab + assert mesh.colors is not None vertex_matrix = mesh.vertices.cpu().numpy().astype("float64") face_matrix = mesh.faces.cpu().numpy().astype("int32") diff --git a/tests/test_exporter_imports.py b/tests/test_exporter_imports.py new file mode 100644 index 0000000000..9174f95cfc --- /dev/null +++ b/tests/test_exporter_imports.py @@ -0,0 +1,21 @@ +"""Tests for exporter dependency imports.""" + +import importlib +import sys + + +def test_exporter_modules_do_not_eagerly_import_pymeshlab(monkeypatch): + """Exporter modules should import without initializing pymeshlab.""" + monkeypatch.setitem(sys.modules, "pymeshlab", None) + + modules = [ + "nerfstudio.exporter.exporter_utils", + "nerfstudio.exporter.tsdf_utils", + ] + for module in modules: + sys.modules.pop(module, None) + + for module in modules: + importlib.import_module(module) + + assert sys.modules["pymeshlab"] is None