import bpy




MeshConter = 0
for me in bpy.data.scenes[0].objects:
if(me.type == "MESH"):
MeshConter = MeshConter + 1




ff = "C:\\Users\\Maxim\\Desktop\\Blender_Game_Models\\Blender_Game_Models\\" + bpy.path.basename(bpy.context.blend_data.filepath) + ".txt"
out = open(ff, 'w')
out.write(bpy.path.basename(bpy.context.blend_data.filepath))
out.write(str(" Nombre de Mesh: ") + str(MeshConter))



for me in bpy.data.scenes[0].objects:
if(me.type == "MESH" and me.name[:2] != "BB"):

out.write(str("\n_____________________________________________________________________\n"))
conter = 0
for f in me.data.polygons:
conter = conter + 1
out.write(str(conter))
conter2 = 0
for obj in bpy.data.scenes[0].objects:
if(obj.type == "ARMATURE"):
armature = obj
conter2 = 0
for pose_bone in armature.pose.bones:
conter2 = conter2 + 1
out.write(" Triangles and " + str(conter2) + " Bones and Mesh name is: " + me.name + "\n________________________________________________________________________\n")

out.write(str(me.location.x))
out.write(str(" "))
out.write(str(me.location.y))
out.write(str(" "))
out.write(str(me.location.z))
out.write(str(" : Object Location\n______________________________________________________________________\n"))


me.rotation_mode = 'QUATERNION'
out.write(str(me.rotation_quaternion.w))
out.write(str(" "))
out.write(str(me.rotation_quaternion.x))
out.write(str(" "))
out.write(str(me.rotation_quaternion.y))
out.write(str(" "))
out.write(str(me.rotation_quaternion.z))
out.write(str(" : Object Quaternion(w,x,y,z)\n_____________________________________________________________________\n"))

out.write(str(me.scale.x))
out.write(str(" "))
out.write(str(me.scale.y))
out.write(str(" "))
out.write(str(me.scale.z))
out.write(str(" : Object Scaling\n______________________________________________________________________________\n"))

out.write(str("++++++++++++++++++++++++++++++++++++++++++++++ Vertex Coordinates ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"))

for f in me.data.polygons:
for i in f.loop_indices:
l = me.data.loops[i]
v = me.data.vertices[l.vertex_index]
out.write(str(v.co.x))
out.write(str(" "))
out.write(str(v.co.y))
out.write(str(" "))
out.write(str(v.co.z))
out.write(str(" \n"))

out.write(str("++++++++++++++++++++++++++++++++++++++++++++++ UV Coordinates ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"))

for f in me.data.polygons:
for i in f.loop_indices:
l = me.data.loops[i]
v = me.data.vertices[l.vertex_index]
for j,ul in enumerate(me.data.uv_layers):
checkUV = True
out.write(str(ul.data[l.index].uv.x))
out.write(str(" "))
out.write(str(ul.data[l.index].uv.y))
out.write(str("\n"))

out.write(str("++++++++++++++++++++++++++++++++++++++++++++++ Normals Coordinates ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"))

for f in me.data.polygons:
if(f.use_smooth):
for i in f.loop_indices:
l = me.data.loops[i]
v = me.data.vertices[l.vertex_index]
out.write(str(v.normal.x))
out.write(str(" "))
out.write(str(v.normal.y))
out.write(str(" "))
out.write(str(v.normal.z))
out.write(str("\n"))
else:
out.write(str(f.normal.x) + " ")
out.write(str(f.normal.y) + " ")
out.write(str(f.normal.z) + "\n")

out.write(str(f.normal.x) + " ")
out.write(str(f.normal.y) + " ")
out.write(str(f.normal.z) + "\n")

out.write(str(f.normal.x) + " ")
out.write(str(f.normal.y) + " ")
out.write(str(f.normal.z) + "\n")

out.write(str("++++++++++++++++++++++++++++++++++++++++++++++ Material Index ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"))

for f in me.data.polygons:
out.write(str(f.material_index) + "\n")


out.write(str("++++++++++++++++++++++++++++++++++++++++++++++ Material Index ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"))


for f in me.data.materials:
out.write(str(f.node_tree.nodes[2].image.name) + " ")



out.write(str("\n---------------------------------------------- End of File --------------------------------------------------------\n"))

out.close()