Python script to output location of vertices to text file

Hello, I have no idea how to use Python scripting. I was wondering if anyone has made a script for Python to output the location and number of the vertices of an object to a text file, for example it would look something like this:

(VertexName: [x, y, z])

Vert1 [3.56, 2.34, 4.56]
Vert2 [4.56, -1.25, 3.90]
Vert3 [7.89, 9.65, 4.90]
Vert3… etc…

I saw a video of a guy who did this with Maya here:

He did not provide the script for it. Maya provides the numbers for each vertices on screen. Can a script for Blender do the same? If so how do I run a script command? I see Python running on the top of blender but can’t use it. Will that be a hard script to create for someone who has no idea of Python? Is it a simple things to create for someone who knows Python scripting? Could someone here even create it?

Thanks in advance.

This writes the vertex coords of the active object (needs to be type MESH) in world space to a file (dump.txt):

import bpy

with open("dump.txt", "w") as file:
    ob = bpy.context.object
    mat = ob.matrix_world
    me = ob.data

    for v in me.vertices:
        file.write("Vert%i [%.2f, %.2f, %.2f]
" % ((v.index,) + (mat*v.co)[:]))

For Vertex Indices on screen, go to Python Console:

bpy.app.debug = True

Then open the N-toolshelf in the 3D View. Active object needs to be a mesh in edit mode. Locate the Mesh Display panel and turn on Indices option (at the bottom, Edge Info). Make sure mesh selection mode is vertex. You can achieve the same by running this in the Text Editor:

import bpy

bpy.app.debug = True

ob = bpy.context.object
if ob is None:
    raise Exception("No active object")

if ob.type != 'MESH':
    raise Exception("Not a mesh object")

if ob.mode != 'EDIT':
    bpy.ops.object.mode_set(mode='EDIT', toggle=False)
    
me = ob.data
me.show_extra_indices = True

bpy.context.tool_settings.mesh_select_mode = (True, False, False)

Very nice thanks! I will give all of these a try.

Edit:

It worked! is there a way to make the numbers bigger?

There were some errors when copy and pasting the top code, I tried a few times. Also can you tell me where Python outputs the dump file location?


Hi,

paste the code into a text block and run from there, rather than the console.

A blank line in the code in the console stuffs up the indentation.

Try a simple line like <space>x=0 in the console and you will get an indent error.

is there a way to make the numbers bigger?

Unfortunately no, unless you increase the DPI value (User Preferences > System) and scale the entire UI up.

Case matters in python, True is not the same as true.

And no, the second code snippet won’t export anything, it just enables the vertex indices in viewport.

Ok nice, thanks everyone for the help. I’ll post back if I have anymore questions