首页 文章

在python中从3d数组创建一个.obj文件

提问于
浏览
5

我的目标是使用python从一个漂亮的(.nii)格式获取.obj文件,目的是在Unity上打开它 . 我知道“scikit-image”软件包有一个名为“measure”的模块,它实现了Marching cube算法 . 我将行进立方体算法应用于我的数据,并获得我期望的结果:

verts, faces, normals, values = measure.marching_cubes_lewiner(nifty_data, 0)

然后我可以绘制数据:

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(verts[:, 0], verts[:,1], faces, verts[:, 2],
                linewidth=0.2, antialiased=True)
plt.show()

enter image description here

我已经找到了将数据(顶点,面法线,值)保存为.obj的函数,但我还没找到 . 因此,我决定自己建造它 .

thefile = open('test.obj', 'w')
for item in verts:
  thefile.write("v {0} {1} {2}\n".format(item[0],item[1],item[2]))

for item in normals:
  thefile.write("vn {0} {1} {2}\n".format(item[0],item[1],item[2]))

for item in faces:
  thefile.write("f {0}//{0} {1}//{1} {2}//{2}\n".format(item[0],item[1],item[2]))  

thefile.close()

但是当我将数据导入到unity时,我得到了以下结果:

enter image description here

enter image description here

所以我的问题如下:

  • 我在.obj制作过程中做错了什么?

  • 是否有更好的方式执行此操作的模块或功能?

  • 有可能做我想做的事吗?

谢谢 .

更多例子:

蟒蛇:

enter image description here

统一:

enter image description here

1 回答

  • 4

    解决方案:经过数小时的调试,解决方案非常简单!只需将1添加到通过应用行进立方体给出的面数据 . 问题是Python认为顶点从0开始,统一认为它们从1开始 . 这就是为什么它们不匹配!没关系 .

    verts,faces,normals,values = measure.marching_cubes_lewiner(nifty_data,0)faces = faces 1

    成功!

    enter image description here

相关问题