微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我的目标是使用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制作过程中做错了什么?
>是否有以更好的方式执行此操作的模块或功能
>可以做我想做的事吗?

谢谢.

更多例子:

Python:

enter image description here

统一:

enter image description here

解决方法:

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

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

faces=faces +1

成功!

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐