# 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()

• face.vertices[:] creates a full copy of the list of vertices. Why is that necessary? Also, you can use f.write('x: %.2f y: %.2f\n' % (world_point.x, world_point.y)) instead of the manual rounding and string conversion. – dr. Sybren Dec 6 '17 at 11:01
• @dr.Sybren Thank you Dr. Sybren. updated & removed some trailing white spaces. – Ratt Dec 6 '17 at 11:50
• Awesome, cheers! I'll see how that goes tomorrow! Thanks – CullodenSpectre Dec 7 '17 at 9:27
• Thanks to your script i figured out where i was going wrong when writing to a file, i would use "d:\test.txt" not a double backslash. Now i just have a permission error :-) but that's ok. – CullodenSpectre Dec 8 '17 at 20:52
• The double backslash is necessary because a single backslash is a special escape character. See the Python documentation for more info. – dr. Sybren Dec 9 '17 at 9:27

You can export your planes as FBX. In the export dialog, tick Selected Objects and switch the version to FBX 6.1 ASCII.

Now you have a simple text file, although with much overhead. In there you will find a line similiar to this:

Vertices: -1.000000,-1.000000,0.000000,2.000000,-2.000000,0.000000,-1.000000,1.000000,0.000000,1.000000,1.000000,0.000000

I got this result from exporting a simple standard plane where I moved one edge. The vertices are all in the order x,y,z. With search and replace in your text editor, you could delete all 0.000000, which are the z-values. They are all 0 and thus easily to distinguish from x- and y-values, since all your planes are on the x-y-plane.

• Thanks for your suggestion, while this does get the data i need into a text file, the overhead and the inclusion of the Z values would wreak havoc on my subsequent programs that require the polygons, and adding another script to clean it up the FBX seems like a doubling of work for no gain. I would much rather have a single python script that outputs the vertices into a text file. – CullodenSpectre Sep 28 '17 at 10:58