How do I Export an Object's Vertex Positions?

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!

print (bpy.context.object.data.attributes[‘My_Attribute’].data,print())

<bpy_collection[4], FloatAttribute.data> None

print (bpy.context.object.data.attributes[‘My_newpos’].data,print())

<bpy_collection[4], FloatVectorAttribute.data> None

print (bpy.context.object.data.attributes[‘Attribute’].data,print())

<bpy_collection[4], FloatColorAttribute.data> None

Did this work for you? Because even after I cleared out the smartquotes it errored, and we have two working solutions above – I’m looking to mark this solved.

it returns this, not the actual data.

<bpy_collection[8], FloatVectorAttribute.data> None

bpy.data.meshes[‘Cube.003’].attributes.values()
will tell me what attributes are on the object

[bpy.data.meshes[‘Cube.003’].attributes[‘fa’], bpy.data.meshes[‘Cube.003’].attributes[‘va’]]

but how do I get to the values within??

1 Like

Next time, do print(dir(method)) to output all of the methods, classes, functions, etc of an API element. help() & type() are also there for accessibly outputs.

Here’s how to get values of all attributes.

import bpy

o = bpy.data.objects["Suzanne"]

for a in o.data.attributes:
    print(a.data)

Or:

for a in o.data.attributes:
    for d in a.data:
        print(d)

This will list per-vertex in this case, because it’s geometry… and that can slow Blender down though.

1 Like

Thanks, but Im after these values…

thats just showing me what I know already, how do I get to access the values shown in the circle?

Sorry my python is not great…

1 Like
import bpy
o = bpy.data.objects["Suzanne"]
for a in o.data.attributes:
    for d in a.data:
        print(d.value)

Don’t take this the wrong way, but why aren’t you checking these things yourself using the methods I described? It’s easy enough to check on your own, I think.

Ah, it outputting to the system console, I didn’t have it open, Thankyou.

and no, its not easy, not when you dont have a clue.

Thankyou…

Quick edit, ok, so its returning floats and ints but no vec3 or vec4.

1 Like

This is because you’ve chosen vertex as your requested data type… if you want a different type you need to state that by changing your data type when you create your mesh attribute. Note: You can also click on the arrow icon below the plus & minus attribute icons to convert existing attributes to other data types.

If you choose a different data type, type Vector for instance you’ll need to change d.value to d.vector.

2 Likes