Created
November 4, 2018 02:08
-
-
Save mikedh/cf2025e8119e43bef3f5c02de98b6695 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import trimesh | |
class FileSystemResolver(object): | |
def __init__(self, source): | |
""" | |
Resolve files based on a source path. | |
""" | |
# remove everything other than absolute path | |
clean = os.path.expanduser(os.path.abspath(str(source))) | |
if os.path.isdir(clean): | |
self.parent = clean | |
# get the parent directory of the | |
elif os.path.isfile(clean): | |
split = os.path.split(clean) | |
self.parent = split[0] | |
else: | |
raise ValueError('path not a file or directory!') | |
def get(self, name): | |
with open(os.path.join(self.parent, name), 'rb') as f: | |
data = f.read() | |
return data | |
class ZipResolver(object): | |
""" | |
Resolve file from a ZIP archive | |
""" | |
def __init__(self, archive): | |
self.archive = archive | |
def get(self, name): | |
return self.archive[name].read() | |
if __name__ == '__main__': | |
# example of file system assets | |
file_name = 'models/fuze.obj' | |
f_res = FileSystemResolver(file_name) | |
f_tex = f_res.get('fuze.obj.mtl') | |
# example of zip resolver | |
with open('models/fuze.zip', 'rb') as f: | |
d = trimesh.util.decompress(f, 'zip') | |
z_res = ZipResolver(d) | |
z_tex = z_res.get('fuze.obj.mtl') | |
### Examples (not implemented) | |
# just works because resolver will be created in load | |
#m = trimesh.load('models/fuze.obj') | |
# needs resolver to be passed | |
#with open(file_name, 'rb') as f: | |
# m = trimesh.load(f, file_type='obj', | |
# resolver=FileSystemResolver(file_name)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment