Skip to content

Commit

Permalink
load_dict shenanigans
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedh committed Jan 16, 2025
1 parent c3c858d commit fab9580
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion tests/test_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_scene(self):
# then make sure json can serialize it
e = g.json.dumps(s.export(file_type=export_format))
# reconstitute the dict into a scene
r = g.trimesh.load(g.json.loads(e))
r = g.trimesh.load(g.json.loads(e), file_type="dict")

# make sure the extents are similar before and after
assert g.np.allclose(g.np.prod(s.extents), g.np.prod(r.extents))
Expand Down
4 changes: 2 additions & 2 deletions trimesh/exchange/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ def load_scene(
# call the dummy function to raise the import error
# this prevents the exception from being super opaque
load_path()
elif isinstance(arg.file_obj, dict):
loaded = _load_kwargs(arg.file_obj)
elif isinstance(file_obj, dict):
loaded = _load_kwargs(file_obj)
elif arg.file_type in mesh_loaders:
# mesh loaders use mesh loader

Expand Down
37 changes: 19 additions & 18 deletions trimesh/exchange/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,25 @@ def load_dict(file_obj, **kwargs):

# now go through file_obj structure and if anything is encoded as base64
# pull it back into numpy arrays
if isinstance(file_obj, dict):
loaded = {}
file_obj = util.decode_keys(file_obj, "utf-8")
for key, shape in mesh_file_obj.items():
if key in file_obj:
loaded[key] = util.encoded_to_array(file_obj[key])
if not util.is_shape(loaded[key], shape):
raise ValueError(
"Shape of %s is %s, not %s!",
key,
str(loaded[key].shape),
str(shape),
)
if len(key) == 0:
raise ValueError("Unable to extract any mesh file_obj!")
return loaded
else:
raise ValueError("%s object passed to dict loader!", file_obj.__class__.__name__)
if not isinstance(file_obj, dict):
raise ValueError(f"`{type(file_obj)}` object passed to dict loader!")

loaded = {}
file_obj = util.decode_keys(file_obj, "utf-8")
for key, shape in mesh_file_obj.items():
if key in file_obj:
loaded[key] = util.encoded_to_array(file_obj[key])
if not util.is_shape(loaded[key], shape):
raise ValueError(
"Shape of %s is %s, not %s!",
key,
str(loaded[key].shape),
str(shape),
)
if len(loaded) == 0:
raise ValueError("Unable to extract a mesh from the dict!")

return loaded


def load_meshio(file_obj, file_type=None, **kwargs):
Expand Down
1 change: 0 additions & 1 deletion trimesh/parent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import abc
import os
from copy import deepcopy
from dataclasses import dataclass

import numpy as np
Expand Down

0 comments on commit fab9580

Please sign in to comment.