PyMeshLab
PyMeshLab copied to clipboard
Save mesh to string
I would like to save a MeshSet object in a specific format (say, e.g. .obj) but keep the resulting string/blob in a variable instead of writing it to disk. Is there currently a possibility for this that I didn't see?
If not, I see two (three) possible routes.
- make a new function/method like
MeshSet.save_current_mesh(...)that does all the same, but returns the "file content" instead of writing it to some file. - change
MeshSet.save_current_mesh(...)such that it accepts a fileobject in addition to a file name. This would allow the user to define anio.StringIOfileobject to save the file content to. - change
MeshSet.save_current_mesh(...)such that it returns the "file content" if the filename is not set (orNone). This should work, but would be a bit ugly since the return type would change depending on the parameters.
Use cases would be (among others, of course)
- changing the mesh in a specific file format (maybe I want to insert some static comment into the file before saving)
- using the string/blob as input for other libraries/functions
- adding the "file" to an archive before saving
What do you think about this?
(For those who are interested, my current workaround is to create a virtual filesystem in RAM, saving the mesh file to this virtual disk and reading it again for further use.)
There is no an easy way to do this with the current codebase, since all the meshlab plugin are going to create files in a given path, and write into it. Each one of your proposed solutions require kind of a revolution in the meshlab framework (and also in the vcg library). I don't know if this suits for your case, but if you need just the mesh data, you can extract coords, face indices, etc into numpy arrays using the Mesh class. I'll label this issue as an enhancement, but I can't say if and when this feature request will be implemented.
Hi there,
I had a similiar issue few weeks back, when I wanted the string content of a PLY file without actually writing it to the filesystem. I worked around it by creating a trimesh object from meshlab vertices and faces similar to what @alemuntoni proposed. In your case with an obj, the solution would look something like this:
import pymeshlab
import trimesh
import os
file_path = os.path.join(os.getcwd(), 'models', '3Dbenchy.stl')
ms = pymeshlab.MeshSet()
ms.load_new_mesh(file_path)
# Do stuff with your mesh...
# ...
# Get vertices and faces from current meshlab mesh
vertices = ms.current_mesh().vertex_matrix()
faces = ms.current_mesh().face_matrix()
# Create a trimesh object
trimesh_mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
# This function returns the file content as a string without actually writing it
trimesh_obj_string = trimesh.exchange.obj.export_obj(
mesh=trimesh_mesh,
include_normals=True,
include_color=True,
include_texture=True,
return_texture=False,
write_texture=True,
resolver=None,
digits=8)
# This is how you would write a file in the end, if you wanted to
with open('benchy.obj', 'w') as f:
f.write(trimesh_obj_string)
Thank you very much, both of you! I understand that it would be a lot of work to develop this. Maybe you might want to keep it in the back of your mind though. There might be funding/time/Google Summer of Code/other opportunities to implement something like using a file object instead of a path in the future :smiley:
I will also have a look at trimesh! I didn't know about that.
@alemuntoni please close this issue or keep it open however you want. :)