I have a 3D numpy array that I generated using some code. I want to save that array as a .obj file to open it in another software like Blender. How do I achieve this?
-
What have you tried? Please provide a [mcve]. – xMutzelx Apr 04 '19 at 15:26
-
I don't have a code for this. I moved to a different project by them time I could figure it out. One idea I had was to use blender's python interface/api to maybe do that? I'm not sure if it can be done though – BBloggsbott Nov 12 '21 at 11:54
2 Answers
Have a read of the wavefront obj file format and output your data to match.
Alternatively, you can adjust your script to create the object directly in blender. You would use Blenders bmesh module to create the mesh data. You might be able to find some examples like this at blender.stackexchange where you can ask for more specific help generating the mesh. If you still want the obj file you can export it from blender.
Depending on the algorithm you use to make your mesh, you may also be able to make use of the math function object generators that are in the extra objects addon that is included with blender. This addon is also another place for example scripts.

- 6,917
- 1
- 16
- 23
To save a 3D numpy array as a .obj file for use in software like Blender, you can use a custom function like the one below
or ref on this topic: Create a .obj file from 3d array in python
import numpy as np
def write_obj(vertices, output_obj_path='output.obj'):
with open(output_obj_path, 'w') as obj_file:
for vertex in vertices:
obj_file.write(f'v {vertex[0]} {vertex[1]} {vertex[2]}\n')
print(f'OBJ file saved to {output_obj_path}')
# Example usage:
# Generate a sample 3D numpy array (replace this with your actual data)
vertices = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]])
# Save the array as an .obj file
write_obj(vertices, 'output.obj')

- 53
- 6