5
$\begingroup$

I'm writing a model exporter in Blender 2.77 and except from triangles and vertex positions, I also want their normals and tangents.

As an example, I'm using a simple cube centered at the Origin (0,0,0).

I found out that Blender can calculate normals and tangents using the following commands:

data=C.object.data
data.calc_normals();
data.calc_tangents("UVMap");

So after that for testing purposes I'm executing the following code

data.calc_tessface();
for f in data.tessfaces:  # indices
    polygon = data.polygons[f.index] #Load Polygon

    for vert in range(len(f.vertices)):
        norm = data.vertices[f.vertices[vert]].normal
        norm1 = data.loops[polygon.vertices[vert]].normal
        print(norm, norm1)

I know that the order of the output vertices won't be correct when iterating in the tessfaces and the polygon structs. My problem is that the output looks like this:

<Vector (-0.5773, 0.5773, -0.5773)> <Vector (-1.0000, 0.0000, 0.0000)>
<Vector (-0.5773, -0.5773, -0.5773)> <Vector (-1.0000, 0.0000, 0.0000)>
<Vector (-0.5773, -0.5773, 0.5773)> <Vector (-1.0000, 0.0000, 0.0000)>
<Vector (-0.5773, 0.5773, 0.5773)> <Vector (-1.0000, 0.0000, 0.0000)>
<Vector (-0.5773, 0.5773, 0.5773)> <Vector (-1.0000, 0.0000, 0.0000)>
<Vector (0.5773, 0.5773, 0.5773)> <Vector (0.0000, 1.0000, 0.0000)>
<Vector (0.5773, 0.5773, -0.5773)> <Vector (0.0000, 1.0000, 0.0000)>
<Vector (-0.5773, 0.5773, -0.5773)> <Vector (-1.0000, 0.0000, 0.0000)>
<Vector (0.5773, 0.5773, 0.5773)> <Vector (0.0000, 1.0000, 0.0000)>
<Vector (0.5773, -0.5773, 0.5773)> <Vector (0.0000, 1.0000, 0.0000)>
<Vector (0.5773, -0.5773, -0.5773)> <Vector (0.0000, 1.0000, 0.0000)>
<Vector (0.5773, 0.5773, -0.5773)> <Vector (0.0000, 1.0000, 0.0000)>
<Vector (-0.5773, -0.5773, -0.5773)> <Vector (0.0000, 1.0000, 0.0000)>
<Vector (0.5773, -0.5773, -0.5773)> <Vector (-1.0000, 0.0000, 0.0000)>
<Vector (0.5773, -0.5773, 0.5773)> <Vector (-1.0000, 0.0000, 0.0000)>
<Vector (-0.5773, -0.5773, 0.5773)> <Vector (0.0000, 1.0000, 0.0000)>
<Vector (-0.5773, -0.5773, -0.5773)> <Vector (-1.0000, 0.0000, 0.0000)>
<Vector (-0.5773, 0.5773, -0.5773)> <Vector (-1.0000, 0.0000, 0.0000)>
<Vector (0.5773, 0.5773, -0.5773)> <Vector (0.0000, 1.0000, 0.0000)>
<Vector (0.5773, -0.5773, -0.5773)> <Vector (0.0000, 1.0000, 0.0000)>
<Vector (0.5773, -0.5773, 0.5773)> <Vector (0.0000, 1.0000, 0.0000)>
<Vector (0.5773, 0.5773, 0.5773)> <Vector (0.0000, 1.0000, 0.0000)>
<Vector (-0.5773, 0.5773, 0.5773)> <Vector (-1.0000, 0.0000, 0.0000)>
<Vector (-0.5773, -0.5773, 0.5773)> <Vector (-1.0000, 0.0000, 0.0000)>

Am I missing some other data operator that will update the normals? Why are the loop normals not equal to the vertices normals?

$\endgroup$
0
$\begingroup$

I never used tessface logic, but I can tell that you are using the wrong index in data.loops[] : each polygon has a loop_begin and a loop_end variable, and each loop with index in [loop_begin; loop_end[ belongs to that face, and is associated with a vertex, which have loop.vertex_index as index. that means that there are WAY more loops than vertices.

also, it seems that you are using flat shading. in that case, loop normals are the same that their faces' normals, and (I am not sure about that last bit) vertex normals are interpolated between face normals, and will be used for smooth shading

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.