From 87266724524b7503b8084ec2dfa677f170b3b0f7 Mon Sep 17 00:00:00 2001 From: Chris Barnes Date: Thu, 21 Dec 2023 12:39:04 +0000 Subject: [PATCH] Improve conditional trimesh use --- python/ncollpyde/main.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/python/ncollpyde/main.py b/python/ncollpyde/main.py index 1a1c620..915d7a0 100644 --- a/python/ncollpyde/main.py +++ b/python/ncollpyde/main.py @@ -7,11 +7,6 @@ import numpy as np from numpy.typing import ArrayLike, NDArray -try: - import trimesh -except ImportError: - trimesh = None - from ._ncollpyde import ( TriMeshWrapper, _index, @@ -136,7 +131,9 @@ def __init__( def _validate( self, vertices: np.ndarray, triangles: np.ndarray ) -> Tuple[NDArray[np.float64], NDArray[np.uint32]]: - if trimesh: + try: + import trimesh + tm = trimesh.Trimesh(vertices, triangles, validate=True) if not tm.is_volume: logger.info("Mesh not valid, attempting to fix") @@ -150,8 +147,7 @@ def _validate( ) return tm.vertices.astype(self.dtype), tm.faces.astype(np.uint32) - - else: + except ImportError: warnings.warn("trimesh not installed; full validation not possible") if vertices.shape[1:] != (3,):