Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

737
Visualizações
Create a .obj file from 3d array in python

My goal is to get a .obj file from a nifty (.nii) format using python, with the purpose of open it on Unity. I know that the "scikit-image" package has a module called "measure" which has the Marching cube algorithm implemented. I apply the marching cube algorithm to my data and I obtain the results I expect:

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

I can then plot the data:

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

I have looked for functions to save the data (verts,faces normals, values) as a .obj but I haven't found one. Thus, I decided to build it myself.

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

But when I import the data to unity I got the following result:

enter image description here

enter image description here

So my questions are the followings:

  • What I'm doing wrong in the .obj making process?
  • Is there a module or function that do this in a better way?
  • Is it possible at all to do what I want?

Thank you.

More examples:

Python:

enter image description here

Unity:

enter image description here

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Solution: After hours of debugging, the solution was very simple! Just add +1 to the faces data given by applying the marching cubes. The problem was that Python considered the verts as starting from 0 and unity consider them as starting from 1. That is why they didn't match! You welcome.

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

faces=faces +1

Success!

enter image description here

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda