Reputation: 388
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?
Upvotes: 3
Views: 5316
Reputation: 999
to convert a 3D npy file to .Obj use the following code. Make sure to pass correct file in load etc. Make sure to install all the necessary libraries as well. For installation you can use pip install [library name]. Ware using the Marching cube algorithm that is really best for the conversion.
import numpy as np
import mcubes
import os
def load_numpy_file(file_path):
"""Load a 3D numpy array from the given file path."""
return np.load(file_path)
def convert_to_obj(numpy_array, output_path):
if len(numpy_array.shape) == 4:
numpy_array = numpy_array[0] # Use the first 3D volume if the shape is (2, X, X, X)
print(f"Using numpy array with shape: {numpy_array.shape}")
# Use Marching Cubes to extract the surface (isosurface threshold at 0.5, or 0 for binary data)
vertices, triangles = mcubes.marching_cubes(numpy_array, 0.5)
# Export the 3D mesh as an .obj file
mcubes.export_obj(vertices, triangles, output_path)
print(f"3D object saved at: {output_path}")
if __name__ == '__main__':
# Path to your numpy file (replace with your actual numpy file path)
numpy_file_path = "spotscar.npy" # Replace with your numpy file path
# Load the numpy file containing your 3D shape data
numpy_array = load_numpy_file(numpy_file_path)
# Check and print numpy array shape
print(f"Numpy array shape: {numpy_array.shape}")
# Specify output path for .obj file
output_obj_path = "output/car_sport.obj" # Example: "output_shape.obj"
# Convert numpy array to .obj file
convert_to_obj(numpy_array, output_obj_path)
Upvotes: 0
Reputation: 53
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')
Upvotes: 0
Reputation: 7079
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.
Upvotes: 1