I want to add vertices to a face. Can I only do this by redrawing the whole face with bmesh.faces.new(loop_vertices)
or can I also just add some to an existing face? The documentation states that the BMVert sequence of a face is read-only.
1 Answer
Subdivide an edge or edges.
Just as with using the UI to add another vertex to a face would require subdividing an edge.
The loop vertices that define a face, will also be a part of another faces loop verts, if connected via an edge. Bmesh gives us a quick API into all the loops and linked geometry of each element. In most cases these are read only.
To add a single vert to an existing faces verts, requires subdividing one edge. Using the bmesh subdivide edges operator will ensure that the links and loops are correctly maintained.
Simple example, using active face and subdividing the zeroth edge.
f = bmesh.faces.active # the active face
bmesh.ops.subdivide_edges(
bm, # the bmesh
edges=[f.edges[0]] if f else [], # zeroth edge if face
)
This will ofcourse add an edge also. If another face shares the edge it will also share the newly created geometry.
How to subdivide mesh with Python and Blender 2.8?
How to use "bmesh.ops.subdivide_edges" on selected edges
import bpy
import bmesh
ob = bpy.context.object
me = ob.data
bm = bmesh.from_edit_mesh(me)
f = bm.faces.active
e = f.edges[0]
bmesh.ops.subdivide_edges(
bm,
edges=[e],
cuts=1,
edge_percents={e : 0.33},
)
bmesh.update_edit_mesh(me)
-
$\begingroup$ Thank you very much that is very helpful! Is it somehow possible to control the position of the newly created vertex also? Or do I have to do this after the fact? $\endgroup$– gladesCommented Jul 29, 2020 at 16:38
-
$\begingroup$ For single edge after fact is pretty simple. However Have added example that subs a vert at 33% along edge instead of default 50. Look to see which is edges first vert for which end. $\endgroup$ Commented Jul 29, 2020 at 17:04
-
$\begingroup$ Thanks a lot! The first vertex would be the one with the lower index or the one appearing first in BMEdgeSeq? $\endgroup$– gladesCommented Jul 29, 2020 at 17:06
-
$\begingroup$ IIRC
edge.verts[0]
. First in edges BMVertSeq. Easy enough to test... $\endgroup$ Commented Jul 29, 2020 at 17:08 -
$\begingroup$ That concludes my question. Thanks you! $\endgroup$– gladesCommented Jul 29, 2020 at 17:09