In Blender 2.93 scripting, I want to create an Wavefront Obj file upon a Flask post request. Originally my workflow is like this:
create mesh in Blender -> Export .obj, save in directory -> read .obj as binary file
-> encode binary file -> return request
Then the guy accepting my encoded string can decode it back to an obj file in node js.
Below is my minimal working example for export obj file. I would first getting vertices and faces information from my custom function, then create it in Blender.
v, f = get_output(body_meas, sex=args.gender)
e = []
new_mesh = bpy.data.meshes.new("sample_mesh")
new_mesh.from_pydata(v, e, f)
new_mesh.update()
new_object = bpy.data.objects.new("sample_object", new_mesh)
view_layer = bpy.context.view_layer
view_layer.active_layer_collection.collection.objects.link(new_object)
blend_file_path = bpy.data.filepath
directory = os.path.dirname(blend_file_path)
target_file = os.path.join(directory, '3d-sample.obj')
bpy.ops.export_scene.obj(filepath=target_file)
However, since this requires saving file in the computer, I would like to simplify the workflow so it won't save anything in the computer. Is it possible to do something like this?
create mesh in Blender -> bpy module transform mesh into binary string -> encode binary string -> return request
In this way then I don't need to store things in the computer, and could possibly speed up the request process.
I have read this, but it still requires the obj file already in directory. My question is whether it is possible to create a mesh in Blender, then directly transform it into a binary string.
Why I want to do this: For sending images through Flask app, it is a common practice to encode image into a base64 string, then decode it back to an image. In that way it is really just string flying back-and-forth through the request, and we don't really need to store anything on the server. In Python, images are treated as a pillow or numpy array, so no need image structure as well. Thus, I'm wondering could we do the same for obj file?