bpy stuff not found

I want to do something like the following:


while k < len(bpy.data.objects[0]):
    for obj_data in bpy.data.meshes[bpy.data.objects[0].name]:



    k++

My error:

‘bpy_prop_collection[key]: key “Tree” not found’

What are you trying to accomplish? Your code is trying to do things that are not logically possible so we would need to know your intent in order to help you out.

For your information:

  • bpy.data.objects[0], will be an Object or None depending on your .blend file. This cannot be iterated. If you want to run though all the Objects in your .blend then you need to use “len(bpy.data.objects)” this would refer to the length of the list of Objects in the .blend as opposed to the length of a single object, which is undefined.
  • If you want to iterate the Objects, normally you would just use a ‘for’ loop:

for obj in bpy.data.objects:
  print(obj.name)

  • If you want to access the Mesh for a give Object then you do something like:

for obj in bpy.data.objects:
  
  if type(obj.data) != bpy.types.Mesh:
    continue

  # Do something on the mesh
  mesh = obj.data

Yeah I had a dumb moment. Currently how I am going through the objects is like this:


for obj_data in bpy.data.meshes:

But I’ve been having troubles getting the uv tex coords for each face for each object in the file. At first I was thinking “wait… is a mesh an object?” so I tried changing the way I was going through it to something that didn’t make much sense.


for obj_data in bpy.data.meshes:
    for face in obj_data.faces:
        #write out texture coordinates for this face
        #how do I do this???

I’ve tried things like



for i in face.faces:
    data = struct.pack('<ff',i.uv)
    out.write(data)


and many variations like that… but I can’t quite figure out how to get the uv for a face…

UV textures are on the same level as faces in the mesh data. So access to one would be along the lines of:

Mesh.data.uv_textures[textureslot].data[face.index].uv1

Also, see:
http://www.blender.org/documentation/blender_python_api_2_62_release/bpy.types.MeshTextureFace.html#bpy.types.MeshTextureFace

Ok. I think I was using the wrong API version for the version of python I’ve been using. That’s why it has been so difficult for me.

I’ve tried:


list(bpy.data.meshes[0].uv_textures)

and it says it’s empty. When my model has been uv mapped. Why is this?

Chances are that you really have more than 1 mesh in your .blend file. Try:


print(bpy.data.meshes[0].name)

That will tell you the name of the mesh that you are really accessing.

When I want to work with the Mesh on an Object I usually make the Object active and run some code like this:



# access the current active object
obj = bpy.context.active_object

# Ensure that this Object is attached to a Mesh.
if type(obj.data) != bpy.types.Mesh:
  pass

  # In reality, you would exit or return failure

mesh = obj.data


Lastly, check out the DataBlocks view in the “Outliner” panel, it is really helpful for seeing all the data that Python can access in your .blend file.

http://dl.dropbox.com/u/7150710/datablocks.jpg

Ok so it works 95% or so right now. However it seems for some reason the way I am exporting it inserts a random odd value. The values that I export with my script match 99% of the values that the obj format exports.


#write out number of textures THIS object uses
            data = struct.pack('<i',len(obj_data.uv_textures))
            out.write(data)
            
            #write out all the texture coordinates for all textures
            for tex in obj_data.uv_textures:
                #write out number of coordinates for each texture
                data = struct.pack('<i',(len(tex.data)*8))
                out.write(data)
                #write out the actual tex coordinates
                for uvs in tex.data:
                   data = struct.pack('<ffffffff',uvs.uv_raw[0],uvs.uv_raw[1],uvs.uv_raw[2],uvs.uv_raw[3],uvs.uv_raw[4],uvs.uv_raw[5],uvs.uv_raw[6],uvs.uv_raw[7])
                   out.write(data)

That is how I am exporting it. Where obj_data is an iterator for bpy.data.meshes.

Is there any reason why this script wouldn’t work?