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)
    vertlist = []
    return {v}

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>]

  • $\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 May 2 '18 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 May 2 '18 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 May 2 '18 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 May 2 '18 at 20:28

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):
    except NameError:
        global vertlist = []

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.