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

2024-04-26 23:27:07 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

然后我可以绘制数据:

^{pr2}$

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


Tags: 模块数据in算法objformatforitem
1条回答
网友
1楼 · 发布于 2024-04-26 23:27:07

解决方案:经过数小时的调试,解决方案非常简单!只需将+1添加到通过应用marching cubes得到的面部数据。问题是Python认为顶点从0开始,unity认为它们从1开始。这就是为什么他们不匹配!欢迎。在

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

faces=faces +1

成功!在

enter image description here

相关问题 更多 >