Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support loading meshes from file objects #362

Open
mancellin opened this issue Jun 21, 2023 · 2 comments
Open

Support loading meshes from file objects #362

mancellin opened this issue Jun 21, 2023 · 2 comments

Comments

@mancellin
Copy link
Collaborator

mancellin commented Jun 21, 2023

It would be nice to be able to load a mesh not only from a file name but also from an already opened file object.

Usage example:

import gzip
with gzip.open("meshes/viking_ship.stl.gz") as f:
    mesh = cpt.load_mesh(f, file_format="stl")

Python standard library include reader for various compressed file format (zip, gzip, etc.), it would be nice to be able to be able to compose them with Capytaine.

@mancellin
Copy link
Collaborator Author

mancellin commented Jun 21, 2023

Work-around using a temporary file:

import gzip, tempfile, shutil
with tempfile.NamedTemporaryFile('wb') as tmp_f:
    with gzip.open("meshes/viking_ship.stl.gz") as f:
        shutil.copyfileobj(f, tmp_f)
        mesh = cpt.load_mesh(tmp_f.name, file_format="stl", name=f.name)

@mancellin
Copy link
Collaborator Author

We could imagine that each load_XXX function supports either a filepath, a file object or raw data (text string or byte string).

Currently, some of them

  • work on the raw data by extracting it from the file in their first lines
  • call an external library (notably vtk) that may not support passing raw data and accept only a filename

We could define decorator that

  • when given a file or file object, extract the data and pass it to the function
  • when given raw data or a file object, write them in a temporary file and pass the filepath to the function.

The latter might be replaced by just raising an error as I'm not sure it would be so useful actually to implement that...
By applying each decorator to each kind of function, all function could support all the cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Low-priority todo
Development

No branches or pull requests

1 participant