2
$\begingroup$

Basically, I've been trying to implement the code from the following page but I'm having difficulty tying the subtleties together.

Get indices of vertices of triangulated faces in python

The block that is over a light gray rectangle is the array that will be passed to a VBO. That is what I'd like that output from Blender to look like.

For the example code I am using, I would like to change every instance of the following:

print(indices)
or
print(plain_verts)

into something that will print to a text file in a specified folder on my hardrive.

***To address a comment that was made:***My question has nothing really to do with C/C++, what I am looking for is the Blender Python equivalent so that I can end up with the same file which is the Vertex, normals and uvCoords. In a 1 dimensional floating point array. Basically I need a Python script that can emulate the following code. I want to replace all the fluff below with a script that is run from within Blender.

enter image description here So basically, how do I end up with text with a file that has something like the following in it? By using Python in Blender.

For example it would be called modelName.txt and would contain the following.

GLfloat modelName[] = 
{
    vertex, normal, uv,      #Vertex 1 
    vertex, normal, uv,      #Vertex 2
    vertex, normal, uv,      #Vertex 3 
};
$\endgroup$
2
  • $\begingroup$ Basically this is can be viewed as a tutorial or a script request however it is pretty fundamental for game devs and import/export writers so a working script would be very beneficial for the overall Blender knowledge base. $\endgroup$ Commented Jan 20, 2014 at 12:15
  • $\begingroup$ It could easily be a plugin which people can alter so they can always output their choice of VBO format for specific models. This offers great optimization opportunities for a game engine to take advantage of. $\endgroup$ Commented Jan 20, 2014 at 12:17

1 Answer 1

3
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ @ CoDEmanX I removed the previous answer it was too messy since the topic changed. Thanks for your input and edits there. $\endgroup$
    – stacker
    Commented Jan 21, 2014 at 7:58
  • $\begingroup$ @jasperge Thanks for cleaning up, but I get now: ValueError: unmatched '{' in format Error: Python script fail, look in the console for now... $\endgroup$
    – stacker
    Commented Feb 20, 2014 at 12:07
  • $\begingroup$ My bad, the line file.write("GLfloat {ob.name}[] =\n{\n".format(ob=ob)) should read: file.write("GLfloat {ob.name}[] =\n{{\n".format(ob=ob)) (notice the double '{'). I can't make the edit, because an edit needs to be at least 6 characters :( $\endgroup$
    – jasperge
    Commented Feb 20, 2014 at 12:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .