So, here's the code i'm trying to implement:

import bpy

#remember to bpy.ops.mesh.quads_convert_to_tris()

ob = bpy.data.objects

#create an output file
import os
file = open(os.path.splitext(bpy.data.filepath)[0] + ".txt", 'w')

for objects in ob:
    #get vertices, normals, and texCoords
    if(ob[objects.name].type == 'MESH'):
        for face in ob[objects.name].data.polygons:
            for vert, loop in zip(face.vertices, face.loop_indices):
                coord = ob[objects.name].data.vertices[vert].co
                normal = ob[objects.name].data.vertices[vert].normal
                layers = ob[objects.name].data.uv_layers
                for MeshUVLoopLayer in layers:
                    #the problem is here
                    uv = ob[objects.name].data.uv_layers[MeshUVLoopLayer.name].data[loop].uv
                    file.write("v {co.x} {co.y} {co.z}\nvn {n.x} {n.y} {n.z}\nvt {uv.x} {uv.y}\n".format(co=coord, n=normal, uv=uv))

this was working when i wasn't doing the for loop and it was '...uv_layers.active', but when i replaced it with what it is now i get this error.

i know the bpy_prop_collection isn't empty it should have two items in it and i checked it with the console using bpy.data.objects["Suzanne"].data.uv_layers.items() and it came out with UVMap and UVMap.001. Suzanne is the only object in the scene, plus it checks the objects first if they're meshes.

Any ideas, on what's causing the error? Or what i'm obviously doing wrong, but am completely oblivious to?

  • $\begingroup$ Your code works fine for me. It creates a text file containing all vertices, normals and texure coordinates, even when I have mutliple objects and non-mesh objects in the scene. $\endgroup$
    – maddin45
    Jan 24, 2015 at 10:14
  • $\begingroup$ why won't it work for me, then? :( that's exactly what it's supposed to do but i get an error. $\endgroup$
    – jape670
    Jan 24, 2015 at 10:16
  • 1
    $\begingroup$ I don't know, maybe you could upload your .blend file so we can check it. A good place to upload your blends is pasteall.org/blend $\endgroup$
    – maddin45
    Jan 24, 2015 at 10:17
  • $\begingroup$ not really sure how to use that site, but i think this is the link: monkey.blen $\endgroup$
    – jape670
    Jan 24, 2015 at 10:19
  • $\begingroup$ Yep, that's the correct link. Again, your script works fine for me, so I can not reporduce the error. I only had to comment out the last two lines where you were checking for an armature. This may sound like a noob suggestion, but have you tried to just reopen Blender or restart your PC? $\endgroup$
    – maddin45
    Jan 24, 2015 at 10:23

1 Answer 1


You need to run the script in Object Mode, make sure that you meet all requirements (material and unwrapping) read the bullet points here,

  • $\begingroup$ omg, that was it. thank you sorry i only really started learning bpy a couple of days ago. $\endgroup$
    – jape670
    Jan 24, 2015 at 10:58
  • $\begingroup$ Does this mean that it is not possible to create scripts that don't rely on excessive ui-interaction? I mean scripts without any clicking at all? $\endgroup$
    – TNT
    Apr 3, 2017 at 15:53
  • $\begingroup$ @TNT No, it doesn't mean that. $\endgroup$
    – Mentalist
    Oct 27, 2020 at 7:54

You must log in to answer this question.

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