How to get the right mesh data for generating mesh in Godot?

Hi, I’m trying to generate mesh data for rebuild mesh in Godot.

According to Godot manual [link], I need to get data of indices, verts, uvs, and normals.

I try to generate code and easily paste it into Godot.

When the shape is simple, it works fine but when the shape becomes more complex, I start to get the wrong shape in Godot. How can I fix it?

Scene

import bpy
import os

obj = bpy.context.active_object
mesh = obj.data

normals = []
indices = []
verts = []
verts_temp = []
uvs = []

for vert in mesh.vertices:
    verts_temp.append(vert.co.xyz)

for face in mesh.polygons:
    indices.append(face.vertices[0])
    indices.append(face.vertices[1]) 
    indices.append(face.vertices[2])

    for i in range(len(face.vertices)):
        v = mesh.vertices[face.vertices[i]]
        normals.append([v.normal[0],v.normal[1],v.normal[2]])
        
for uv_layer in mesh.uv_layers:
    for x in range(len(uv_layer.data)):
        uvs.append(uv_layer.data[x].uv)

for i in indices:
    verts.append(verts_temp[i])


print("#___________________indices___________________")
for i in indices:
    print("    indices.push_back(" + str(i) + ")")
    
print("#___________________verts___________________")
for a in verts:
    print("    vertices.push_back(Vector3" + str(a).replace("<Vector ", "").replace(">", "") + ")")

print("#___________________uvs___________________")
for b in uvs:
    
    print("    uvs.push_back(Vector2" + str(b).replace("<Vector ", "").replace(">", "") + ")")
    
print("#___________________normals___________________")
for k in normals:
#    print("    normals.push_back(Vector3(" + str(k).replace("[", "").replace("]", "") + "))")
    print("    normals.push_back(Vector3.FORWARD)")

Just a quick look: You are adding only the first three vertices of every face to your new object which can consist of PrimitiveType:

  • PRIMITIVE_TRIANGLES
  • PRIMITIVE_TRIANGLE_STRIP
  • PRIMITIVE_TRIANGLE_FAN

but they could be quads or n-gons… so you have to triangle them first or reconsider the way you are adding them in a way according tothe choosen typ.

Hi, thanks for the reply! I follow your advice that converts all the meshes from quad to triangle but still the same.
I also try ico sphere and the shape is still not complete.

Hmm the tetrahedron in the (very small) first image gave me the idea. The new objects in the (bigger) image not only seems to lack some polgons the also seem to have another orientaton… too unfamilar with godot her :frowning: (wanna change this…)

I figure it out. To convert the mesh from Blender to Godot, you need to give the vertex a clockwise order or the shape will break.

in the code:

for face in mesh.polygons:
indices.append(face.vertices[0])
indices.append(face.vertices[1])
indices.append(face.vertices[2])

change it to:
for face in mesh.polygons:
indices.append(face.vertices[0])
indices.append(face.vertices[2])
indices.append(face.vertices[1])

It is working fine for me.

Captures

Sorry, :sweat_smile: that was what i meant with:

and:

but on the other side, this was my post with the most spelling errors i ever had so far here on BA… so i was in a hurry :blush: (double checked on this one :wink: )
So next time: just go on telling after one or two days so someone else could pick this up. (And i also wanted to have a deeper look into godot yet.)

Oh, I think I didn’t get what you mean. pardon my poor English and don’t be sorry I’m thankful for your help! :bowing_man:

And i wasn’t precise/clear enough because it didn’t took some more seconds to rethink/reread my written answer… (next time then).