将 3D numpy 数组另存为 .obj 文件

问题描述 投票:0回答:2

我有一个使用一些代码生成的 3D numpy 数组。我想将该数组另存为 .obj 文件,以便在 Blender 等其他软件中打开它。我如何实现这一目标?

python multidimensional-array blender
2个回答
1
投票

阅读wavefront obj 文件格式并输出您的数据以匹配。

或者,您可以调整脚本以直接在搅拌机中创建对象。您可以使用 Blenders bmesh module 来创建网格数据。您可能可以在 blender.stackexchange 找到一些示例像这样,您可以在其中寻求更具体的帮助来生成网格。如果您仍然想要 obj 文件,可以从搅拌机导出它。

根据用于制作网格的算法,您还可以使用搅拌机附带的额外对象插件中的数学函数对象生成器。这个插件也是示例脚本的另一个地方。


0
投票

要将 3D numpy 数组另存为 .obj 文件以便在 Blender 等软件中使用,您可以使用如下所示的自定义函数

或有关此主题的参考:在 python 中从 3d 数组创建 .obj 文件

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')

© www.soinside.com 2019 - 2024. All rights reserved.