Skip to content

Commit

Permalink
Adjust unit conversion for mesh (#315)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
iory authored Nov 8, 2023
1 parent a7023d7 commit 971aef4
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions skrobot/utils/urdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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


Expand Down

0 comments on commit 971aef4

Please sign in to comment.