In order to export the triangles of the default scene's cube the process would be as follows.
- Select the Cube, switch to Edit Mode
- Select all faces
- Load the Texture in UV-Image Editor
- UV-Unwrap U Smart UV-Project
Create a Material and Texture , assign the texture and UV-Map
Convert the quads to tris Ctrl-F Ctrl-T
Switch to Object Mode (Important)
Run the script:
import bpy
#bpy.ops.mesh.quads_convert_to_tris()
ob = bpy.context.object
with open("c:\\tmp\\{ob.name}.txt".format(ob=ob), "w") as file:
file.write("GLfloat {ob.name}[] =\n{\n".format(ob=ob))
for face in ob.data.polygons:
for vert, loop in zip(face.vertices, face.loop_indices):
coord = ob.data.vertices[vert].normal
normal = ob.data.vertices[vert].co
uv = ob.data.uv_layers.active.data[loop].uv
file.write("\t{co.x}, {co.y}, {co.z}, {n.x}, {n.y}, {n.z}, {uv.x}, {uv.y},\n".format(co=coord, n=normal, uv=uv))
file.write( "};\n")
Find the output file in the temp directory:
GLfloat Cube[] =
{
0.577349, 0.577349, -0.577349, 1.000000 1.000000 -1.000000, 0.666667, 0.333333,
0.577349, -0.577349, -0.577349, 1.000000 -1.000000 -1.000000, 1.000000, 0.333333,
-0.577349, -0.577349, -0.577349, -1.000000 -1.000000 -1.000000, 1.000000, 0.666667,
0.577349, 0.577349, 0.577349, 1.000000 0.999999 1.000000, 0.333333, 0.666667,
-0.577349, 0.577349, 0.577349, -1.000000 1.000000 1.000000, 0.333333, 0.333333,
0.577349, -0.577349, 0.577349, 0.999999 -1.000001 1.000000, 0.666667, 0.666667,
0.577349, 0.577349, -0.577349, 1.000000 1.000000 -1.000000, 0.333333, 0.333333,
0.577349, 0.577349, 0.577349, 1.000000 0.999999 1.000000, 0.000000, 0.333333,
0.577349, -0.577349, -0.577349, 1.000000 -1.000000 -1.000000, 0.333333, 0.000000,
0.577349, -0.577349, -0.577349, 1.000000 -1.000000 -1.000000, 0.333333, 0.333333,
0.577349, -0.577349, 0.577349, 0.999999 -1.000001 1.000000, 0.333333, 0.000000,
-0.577349, -0.577349, -0.577349, -1.000000 -1.000000 -1.000000, 0.666667, 0.333333,
-0.577349, -0.577349, -0.577349, -1.000000 -1.000000 -1.000000, 0.666667, 0.000000,
-0.577349, -0.577349, 0.577349, -1.000000 -1.000000 1.000000, 1.000000, 0.000000,
-0.577349, 0.577349, 0.577349, -1.000000 1.000000 1.000000, 1.000000, 0.333333,
0.577349, 0.577349, 0.577349, 1.000000 0.999999 1.000000, 0.000000, 0.333333,
0.577349, 0.577349, -0.577349, 1.000000 1.000000 -1.000000, 0.333333, 0.333333,
-0.577349, 0.577349, 0.577349, -1.000000 1.000000 1.000000, 0.000000, 0.666667,
-0.577349, 0.577349, -0.577349, -1.000000 1.000000 -1.000000, 0.666667, 0.666667,
0.577349, 0.577349, -0.577349, 1.000000 1.000000 -1.000000, 0.666667, 0.333333,
-0.577349, -0.577349, -0.577349, -1.000000 -1.000000 -1.000000, 1.000000, 0.666667,
0.577349, 0.577349, 0.577349, 1.000000 0.999999 1.000000, 0.000000, 0.333333,
0.577349, -0.577349, 0.577349, 0.999999 -1.000001 1.000000, 0.000000, 0.000000,
0.577349, -0.577349, -0.577349, 1.000000 -1.000000 -1.000000, 0.333333, 0.000000,
0.577349, 0.577349, -0.577349, 1.000000 1.000000 -1.000000, 0.333333, 0.333333,
-0.577349, 0.577349, -0.577349, -1.000000 1.000000 -1.000000, 0.333333, 0.666667,
-0.577349, 0.577349, 0.577349, -1.000000 1.000000 1.000000, 0.000000, 0.666667,
-0.577349, 0.577349, 0.577349, -1.000000 1.000000 1.000000, 0.333333, 0.333333,
-0.577349, -0.577349, 0.577349, -1.000000 -1.000000 1.000000, 0.666667, 0.333334,
0.577349, -0.577349, 0.577349, 0.999999 -1.000001 1.000000, 0.666667, 0.666667,
-0.577349, 0.577349, -0.577349, -1.000000 1.000000 -1.000000, 0.666667, 0.333333,
-0.577349, -0.577349, -0.577349, -1.000000 -1.000000 -1.000000, 0.666667, 0.000000,
-0.577349, 0.577349, 0.577349, -1.000000 1.000000 1.000000, 1.000000, 0.333333,
0.577349, -0.577349, 0.577349, 0.999999 -1.000001 1.000000, 0.333333, 0.000000,
-0.577349, -0.577349, 0.577349, -1.000000 -1.000000 1.000000, 0.666667, 0.000000,
-0.577349, -0.577349, -0.577349, -1.000000 -1.000000 -1.000000, 0.666667, 0.333333,
};
The code is based on ideasman42 answer here: Exporting UV coordinates
.blend file as demo.