From 971aef44fe70d67ce21f0759ced4ed6b7809cdc5 Mon Sep 17 00:00:00 2001 From: Iori Yanokura Date: Wed, 8 Nov 2023 14:02:16 +0900 Subject: [PATCH] Adjust unit conversion for mesh (#315) * Adjust unit conversion * Add transparency check to mesh loader and fix fully transparent PBRMaterials - Implemented `get_transparency` function to retrieve the alpha value from mesh material's main_color. - Updated `_load_meshes` function to check for transparency in meshes and set alpha to 255 for fully transparent PBRMaterials to ensure visibility. - Imported `PBRMaterial` from `trimesh.visual.material` to support the transparency correction. --- skrobot/utils/urdf.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/skrobot/utils/urdf.py b/skrobot/utils/urdf.py index 10218e19..07aeccb5 100644 --- a/skrobot/utils/urdf.py +++ b/skrobot/utils/urdf.py @@ -26,6 +26,7 @@ import PIL import six import trimesh +from trimesh.visual.material import PBRMaterial from skrobot.coordinates import normalize_vector from skrobot.coordinates import rpy_angle @@ -59,6 +60,13 @@ def no_mesh_load_mode(): _CONFIGURABLE_VALUES["no_mesh_load_mode"] = False +def get_transparency(mesh): + if hasattr(mesh, 'visual') and hasattr(mesh.visual, 'material'): + material = mesh.visual.material + if hasattr(material, 'main_color'): + return material.main_color[3] + + def parse_origin(node): """Find the ``origin`` subelement of an XML node and convert it @@ -249,6 +257,8 @@ def _load_meshes(filename): meshes = meshes.scaled(0.001) else: meshes = trimesh.load(filename) + if meshes.units is not None and meshes.units != 'meter': + meshes = meshes.convert_units('meter') except Exception as e: logger.error("Failed to load meshes from {}. Error: {}" .format(filename, e)) @@ -271,6 +281,11 @@ def _load_meshes(filename): else: raise ValueError('Unable to load mesh from file') + for mesh in meshes: + transparency = get_transparency(mesh) + if transparency is not None and transparency == 0.0: + if isinstance(mesh.visual.material, PBRMaterial): + mesh.visual.material.baseColorFactor[3] = 255 return meshes