1
$\begingroup$

I'm completely baffled by this - it makes no sense to me at all!

I'm trying to copy over vertex coordinates from an object in blender into a list, and at the end of the loop the entire list is filled with only the last vertex.

This prints out the list with only the last vertex in every part of the list:

import bpy

current_obj = bpy.context.active_object
verts_l = [v.co for v in current_obj.data.vertices.values()]
verts_w = [current_obj.matrix_world * v_local for v_local in verts_l]

vert_list = [[None, None, None]] * vert_count

def build_vert_list ():
    for i, vert in enumerate(verts_w):
        vert_list[i][0] = verts_w[i][0]
        vert_list[i][1] = verts_w[i][1]
        vert_list[i][2] = verts_w[i][2]
    print(vert_list)

However, if I instead print out the vert_list during the loop, it prints out the correct data:

def build_vert_list ():
    for i, vert in enumerate(verts_w):
        vert_list[i][0] = verts_w[i][0]
        vert_list[i][1] = verts_w[i][1]
        vert_list[i][2] = verts_w[i][2]
        print(vert_list[i])

If I do instead:

def build_vert_list ():
    for i, vert in enumerate(verts_w):
        vert_list[i] = verts_w[i]

it works properly, and I can access the values in the list with vert_list[1][0] for example, however every member of list is headed with some weird nonsense, which prevents me from doing round() functions on the data.

Can anyone help me? This has me pretty stumped.

(I only started learning Python a few days ago, so go easy on the technical stuff please.)

$\endgroup$
0
$\begingroup$

List comprehension

Not sure why you are getting the issue. For Mesh.vertices make sure you are running code in object mode.

Question code can IMO be simplified to

import bpy
context = bpy.context

ob = context.object
mw = ob.matrix_world
me = ob.data

vert_list = [list(mw @ v.co) for v in me.vertices] # 2.80
#vert_list = [(mw @ v.co)[:] for v in me.vertices] # 2.80 alternate

#vert_list = [list(mw * v.co) for v in me.vertices] # 2.7x

print(vert_list)

in this case see no need to predefine vert_list or enumerate to index.

Alternatively using bmesh in edit mode

import bpy
import bmesh

context = bpy.context

ob = context.edit_object
mw = ob.matrix_world
me = ob.data

bm = bmesh.from_edit_mesh(me)
bmesh.ops.transform(bm,
        verts=bm.verts,
        matrix=mw
        )
vert_list = [v.co[:] for v in bm.verts]
print(vert_list)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.