How do I Export an Object's Vertex Positions?

How do I export or copy the Geonode Spreadsheet Vertex Data?

I’m trying to add a model’s vertex data to an add-on (details here). What I need is clearly visible in the Geonode Workspace Spreadsheet – how do I copy or export it to a .csv or even .txt format?

This example has 186 verts but may not be the one I go with – I’m looking for a general “how do I get this myself” rather than “somebody please extract this for me”.

MilkJug008b.blend (1.1 MB)

Edited to add: see posts below, this turns out to be a Python question. I’m trying to export an object’s vertex positions, with the intent of adding the data to an existing add-on that takes old bicubic patch data and outputs it as a mesh in a resolution specified by the user.

The object will be much like the example above, a reasonably simple mesh similiar to what the add-on already handles. Once I have the data I can use a spreadsheet program to make (pretty much) whatever changes are needed for what the add-on expects – hopefully the default order shown will be appropriate, but I don’t know till I try. Help?

1 Like

unfortunately you cant export the spreadsheet as txt or csv yet, but you can write out yr position as an attribute like this the access it for export maybe from python…

Hope that helps…

accessing attribute NEWPOS in shader editor.

@josephhansen I cant seem to find a python way to write the vertices to a csv, can you help?

1 Like

import bpy

outputFile = ‘d:\mesh.csv’

verts = [ bpy.context.object.matrix_world * v.co for v in bpy.context.object.data.vertices ]

csvLines = [ “;”.join([ str(v) for v in co ]) + “\n” for co in verts ]

f = open( outputFile, ‘w’ )
f.writelines( csvLines )
f.close()

This is all I could find and it errors…

Changed the filepath to C:\Users\Public\Documents\jugmesh000.csv, it wasn’t happy with the quote marks so I retyped them, then I got this:

SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape

I’m not very good with python, can’t tell what’s going on.

Oh boy I’m super rusty with python and CSV… I’ll be thinking about this though

2 Likes

print (object_eval.data.attributes[0])

<bpy_struct, FloatVectorAttribute(“NEWPOS”) at 0x000000747E7C81F8>

here they are, how do I get em?

classmethod bl_rna_get_subclass_py(id, default=None )

Parameters

id (string) – The RNA type identifier.

Returns

The class or default when not found.

Return type

type

from the manual, this is when the blender api hurts my head,.

1 Like

I’m still wrapping my head around the apparent fact that the devs labeled a gridded data display a “spreadsheet” for no reason other than it kinda-sorta looks like a spreadsheet to them. The data I need is right there, I can see it . . . but I can’t even copy’n’paste it into a text file, they haven’t even implemented that. ::facepalm:: It’s free software, I shouldn’t complain, it’s free software, I shouldn’t complain, it’s free software, I shouldn’t complain . . .

Sorry people, I thought the answer to this would be a lot simpler than it turned out, which is why I put it in Basics & Interface. Should this be moved? Maybe to (gods help us) Python Support?

1 Like

Yes, I tried as soon as nodes came out, no cut… what???

I think you should ask in another thread python maybe, I can see the attribute but its beyond my python skills…

Attributes are a relatively new afaik.

good luck… let me know if you succeed.

1 Like

Did you find it here?

import bpy

outputFile = ‘C:/tmp/mesh.csv’

verts = [ bpy.context.object.matrix_world * v.co for v in bpy.context.object.data.vertices ]

csvLines = [ “;”.join([ str(v) for v in co ]) + “\n” for co in verts ]

f = open( outputFile, ‘w’ )
f.writelines( csvLines )
f.close()

They say “It requires you to select and set the mesh you want to export as the active object” which I did, but I got:

Python: Traceback (most recent call last):
File “\Text”, line 5, in
File “\Text”, line 5, in
TypeError: Element-wise multiplication: not supported between ‘Matrix’ and ‘Vector’ types

Attempted with both my object and with a default cube in a factory default startup file.

Yes thats where I found that, but I got further with this…

<bpy_struct, FloatVectorAttribute(“NEWPOS”) at 0x000000747E7C81F8>

so its there, but the question is how to read it?

Plus that previous one refers to ‘vertex’ attributes, we are trying to read the object attributes.

1 Like

did you ask in python support?

He moved the thread to that category already, I believe.

2 Likes

@KickAir_8P I made a few scripts a while back which does what you’re looking for, I think.

2 Likes

Yep, this topic’s now in Python Support. I’d very much appreciate some – the code AlphaChannel and I found is from 2016, so I understand if Blender’s Python implementation’s changed, but with the right syntax this should still be feasible, yes? My apologies for flailing, when I first asked this I had good reason to believe it wasn’t gonna be a coding issue.

I tried this one:

import bpy

m = bpy.context.object.evaluated_get(bpy.context.evaluated_depsgraph_get()).to_mesh()

for v in range(0, len(m.vertices)):
print(str(v) + " | " + str(m.vertices[v].co))

Which didn’t error and says it ran, but I can’t find where the data went. I apologize from my noobish ignorance! :scream:

I’m not too familiar with the CSV format, but if it’s literally just a comma between each row and a newline for each column, you could have something like this.

import bpy
import os


def write_verts():
    obj = bpy.context.object

    if obj is None or obj.type != "MESH":
        return

    # Output geometry
    obj_eval = obj.evaluated_get(bpy.context.view_layer.depsgraph)
    filepath = "vertices.csv"
    
    with open(filepath, "w") as file:
        # Write the header, pos x | pos y | pos z
        file.write("pos x,pos y,pos z\n")

        for v in obj_eval.data.vertices:
            # ":.3f" means to use 3 fixed digits after the decimal point.
            file.write(f",".join(f"{c:.3f}" for c in v.co) + "\n")

    print(f"File was written to {os.path.join(os.getcwd(), filepath)}")

if __name__ == "__main__":
    write_verts()

Based on your blend file, this produces a vertices.csv file with this format:

pos x,pos y,pos z
0.762,-1.298,3.000
0.000,-1.500,3.000
1.298,-0.762,3.000
-1.298,-0.762,3.000
-1.500,0.000,3.000
-0.762,-1.298,3.000
1.016,-1.730,1.000
0.979,-1.666,1.630
0.000,-1.926,1.630
0.000,-2.000,1.000
0.885,-1.506,2.304
0.000,-1.741,2.304
2 Likes

The output should be displayed in your Blender Console.

1 Like

One of the questions was how does one read these attributes here?

image

Does anyone have a clue?

Far more helpful as geonodes can generate these and set them on an object.

Both of these worked – thanks oodles!