0
$\begingroup$

My goal is to add every created vertice to a list.

import bpy
import bmesh
def add_vert(loc):
    bm = bmesh.from_edit_mesh(bpy.context.edit_object.data)
    v = bm.verts.new(loc)
    bm.verts.index_update()
    bm.verts.ensure_lookup_table()
    vertlist = []
    vertlist.extend([v])
    print(vertlist)
    return {v}
add_vert((1,1,1))

This is what I get as output: [<BMVert(0x0000020E20C3CF20), index=12>] [<BMVert(0x0000020E20C3CF58), index=13>] [<BMVert(0x0000020E20C3CF90), index=14>]

This is what I would like: [<BMVert(0x0000020E20C3CF20), index=12>,<BMVert(0x0000020E20C3CF20), index=13>,<BMVert(0x0000020E20C3CF20), index=14>]

$\endgroup$
4
  • $\begingroup$ Loading an edit mode bmesh, updating and reindexing it every time a new vertex is added is complete overkill. Which is why I pointed out making only one bmesh in this answer as a class property ... or pass the bm to the method. $\endgroup$
    – batFINGER
    Commented May 2, 2018 at 12:04
  • $\begingroup$ What I want to do is change the color of each vertex in the list using Bgl and so I have to make and update the list while in edit mode. $\endgroup$
    – sajdnv
    Commented May 2, 2018 at 19:14
  • $\begingroup$ Ok let's put it another way. Let's say you add 1000 verts to your list to display in bgl. I contend you can create the bmesh once, add 1000 verts then update once. via your method above you create 1000 bmeshes and update a 1000 times. $\endgroup$
    – batFINGER
    Commented May 2, 2018 at 20:14
  • $\begingroup$ I’m just using this example of what I want to do so I can get right to the point. I plan on implementing this into what u showed me. $\endgroup$
    – sajdnv
    Commented May 2, 2018 at 20:28

1 Answer 1

1
$\begingroup$

You need to define your vertlist variable outside of the function. Currently, vertlist exists only within the scope of the add_vert function. You can either define it before hand:

vertlist = []
def add_vert(loc):
    ...

or add a try statement to check if vertlist already exists:

def add_vert(loc):
    ...
    try:
        vertlist
    except NameError:
        global vertlist = []
    ...
$\endgroup$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .