Skip to content

Commit ad46d61

Browse files
Switch from os.path to pathlib in meshcat_visualizer.py
1 parent 67fa1c5 commit ad46d61

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

bindings/python/pinocchio/visualize/meshcat_visualizer.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
import warnings
32
from pathlib import Path
43
from typing import ClassVar, List
@@ -128,29 +127,32 @@ def __init__(self, dae_path: str, cache: Optional[Set[str]] = None) -> None:
128127
)
129128
if img_lib_element:
130129
img_resource_paths = [
131-
e.text for e in img_lib_element.iter() if e.tag.count("init_from")
130+
Path(e.text)
131+
for e in img_lib_element.iter()
132+
if e.tag.count("init_from")
132133
]
133134

134135
# Convert textures to data URL for Three.js ColladaLoader to load them
135136
self.img_resources = {}
136137
for img_path in img_resource_paths:
138+
img_key = str(img_path)
137139
# Return empty string if already in cache
138140
if cache is not None:
139141
if img_path in cache:
140-
self.img_resources[img_path] = ""
142+
self.img_resources[img_key] = ""
141143
continue
142144
cache.add(img_path)
143145

144146
# Encode texture in base64
145-
img_path_abs = img_path
146-
if not Path(img_path).is_absolute():
147-
img_path_abs = os.path.normpath(dae_dir / img_path_abs)
148-
if not Path(img_path_abs).is_file():
147+
img_path_abs: Path = img_path
148+
if not img_path.is_absolute():
149+
img_path_abs = (dae_dir / img_path_abs).resolve()
150+
if not img_path_abs.is_file():
149151
raise UserWarning(f"Texture '{img_path}' not found.")
150-
with Path(img_path_abs).open("rb") as img_file:
152+
with img_path_abs.open("rb") as img_file:
151153
img_data = base64.b64encode(img_file.read())
152154
img_uri = f"data:image/png;base64,{img_data.decode('utf-8')}"
153-
self.img_resources[img_path] = img_uri
155+
self.img_resources[img_key] = img_uri
154156

155157
def lower(self) -> Dict[str, Any]:
156158
"""Pack data into a dictionary of the format that must be passed to

0 commit comments

Comments
 (0)