From fab95801cd092bdda029f614a9812dcf6a9c8d31 Mon Sep 17 00:00:00 2001 From: Michael Dawson-Haggerty Date: Thu, 16 Jan 2025 01:33:18 -0500 Subject: [PATCH] load_dict shenanigans --- tests/test_scene.py | 2 +- trimesh/exchange/load.py | 4 ++-- trimesh/exchange/misc.py | 37 +++++++++++++++++++------------------ trimesh/parent.py | 1 - 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/tests/test_scene.py b/tests/test_scene.py index e9e783433..d483de04d 100644 --- a/tests/test_scene.py +++ b/tests/test_scene.py @@ -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)) diff --git a/trimesh/exchange/load.py b/trimesh/exchange/load.py index 1fd45a96f..abf5e49f8 100644 --- a/trimesh/exchange/load.py +++ b/trimesh/exchange/load.py @@ -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 diff --git a/trimesh/exchange/misc.py b/trimesh/exchange/misc.py index 152dccb31..a13f176a8 100644 --- a/trimesh/exchange/misc.py +++ b/trimesh/exchange/misc.py @@ -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): diff --git a/trimesh/parent.py b/trimesh/parent.py index db8527bc8..7e216ddc9 100644 --- a/trimesh/parent.py +++ b/trimesh/parent.py @@ -7,7 +7,6 @@ import abc import os -from copy import deepcopy from dataclasses import dataclass import numpy as np