How can I write the vertices of a 2d polygon to a text file? I'm trying to figure out how to save the vertices of a 2D polygon to a text file.


e.g. i have drawn two flat squares on the X-Y plane, which i have named "Square 1" and "Square 2", locations of their vertices are:
Square 1: (1,1) (1,0) (0,0) (0,1) 
Square 2: (4.2,4.2) (4.2,3.2) (3.2,3.2) (3.2,4.2) 

I would like to save these vertices to a simple text file in this format:
Square 1: 1 1, 1 0, 0 0, 0 1 
Square 2: 4.2 4.2, 4.2 3.2, 3.2 3.2, 3.2 4.2 


Any help is welcome, Thanks!

after some more googling i'm finally getting somewhere!

import bpy  

current_obj = bpy.context.active_object  


    print("="*40) # printing marker  
    for face in current_obj.data.polygons:  
        verts_in_face = face.vertices[:]  
        for vert in verts_in_face:
            local_point = current_obj.data.vertices[vert].co
            world_point = current_obj.matrix_world * local_point
            print('%.2f %.2f, ' % (world_point.x, world_point.y), end='', flush=True )
    print("")


this outputs to the console in the format i want, however printing to a file in the for loop doesn't work.
To keep it similar to what you have already scripted.

import bpy

current_obj = bpy.context.active_object
f = open("d:\\test.txt", 'w')  # use 'a' to append
for face in current_obj.data.polygons:
    for vert in face.vertices[:]:
        local_point = current_obj.data.vertices[vert].co
        world_point = current_obj.matrix_world * local_point
        f.write('x: %.2f\t y: %.2f\n' % (world_point.x, world_point.y))
f.close()