Skip to content

Commit

Permalink
fix for #2330
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedh committed Dec 15, 2024
1 parent 181dbcb commit a25082b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
3 changes: 3 additions & 0 deletions tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def test_JSHTML(self):
children = list(h.body.iterchildren())
assert len(children) >= 2

# make sure this is returning anything
assert js.scene_to_notebook(s) is not None

def test_inNB(self):
import trimesh.viewer.notebook as js

Expand Down
26 changes: 20 additions & 6 deletions trimesh/exchange/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ def load(
allow_remote=allow_remote,
**kwargs,
)
arg = _parse_file_args(
file_obj=file_obj,
file_type=file_type,
resolver=resolver,
allow_remote=allow_remote,
**kwargs,
)

# combine a scene into a single mesh
if force == "mesh":
Expand All @@ -122,7 +129,7 @@ def load(
# we are matching deprecated behavior here!
# matching old behavior you should probably use `load_scene`
if len(loaded.geometry) == 1:
kind = loaded.metadata["file_type"]
kind = arg.file_type
geom = next(iter(loaded.geometry.values()))
if (kind not in {"glb", "gltf"} and isinstance(geom, PointCloud)) or kind in {
"obj",
Expand Down Expand Up @@ -185,7 +192,12 @@ def load_scene(
try:
if arg.file_type in path_formats():
# path formats get loaded with path loader
loaded = load_path(file_obj=arg.file_obj, file_type=arg.file_type, **kwargs)
loaded = load_path(
file_obj=arg.file_obj,
file_type=arg.file_type,
metadata=arg.metadata,
**kwargs,
)
elif arg.file_type in ["svg", "dxf"]:
# call the dummy function to raise the import error
# this prevents the exception from being super opaque
Expand All @@ -200,6 +212,7 @@ def load_scene(
file_obj=arg.file_obj,
file_type=arg.file_type,
resolver=arg.resolver,
metadata=arg.metadata,
**kwargs,
)
)
Expand All @@ -226,9 +239,10 @@ def load_scene(
loaded = Scene(loaded)

# add the "file_path" information to the overall scene metadata
loaded.metadata.update(arg.metadata)
# if 'metadata' not in kwargs:
# loaded.metadata.update(arg.metadata)
# add the load path metadata to every geometry
[g.metadata.update(arg.metadata) for g in loaded.geometry.values()]
# [g.metadata.update(arg.metadata) for g in loaded.geometry.values()]

return loaded

Expand Down Expand Up @@ -637,8 +651,8 @@ def _parse_file_args(
file_type = file_type.lower()

# if user passed in a metadata dict add it
if "metadata" in kwargs and isinstance(kwargs["metadata"], dict):
metadata.update(kwargs["metadata"])
if len(kwargs.get("metadata", {})) > 0:
metadata = kwargs["metadata"]
else:
metadata["file_type"] = file_type
if file_path is not None:
Expand Down
5 changes: 4 additions & 1 deletion trimesh/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def _get(name: str, decode: bool, decode_json: bool, as_stream: bool):
resource : str, bytes, or decoded JSON
File data
"""

# key by name and decode
cache_key = (name, bool(decode), bool(decode_json), bool(as_stream))
cached = _cache.get(cache_key)
Expand All @@ -57,7 +58,9 @@ def _get(name: str, decode: bool, decode_json: bool, as_stream: bool):
return cached

# get the resource using relative names
with open(os.path.join(_pwd, name), "rb") as f:
# all templates are using POSIX relative paths
# so fix them to be platform-specific
with open(os.path.join(_pwd, *name.split("/")), "rb") as f:
resource = f.read()

# make sure we return it as a string if asked
Expand Down

0 comments on commit a25082b

Please sign in to comment.