1
$\begingroup$

I am running the following code to generate vertices with 4 and 8 edges (plus the vertices of the outer loop):

import bpy
import bmesh
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False, confirm=False)

bpy.ops.mesh.primitive_plane_add(size=11.95, enter_editmode=False, location=(0, 0, 0))

bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.subdivide(number_cuts=29)
bpy.ops.mesh.poke()

Now I would like to select all vertices that have 4 edges connected. 'Shift-G - Amount of connected edges' works, but I would like a) get the distribution of vertices with 2/3/4/5/6/... edges, and then to automatically select vertices based on this criterion.

$\endgroup$
3
$\begingroup$

Use bmesh

Strongly suggest ditching bpy.ops.mesh operators for bmesh.

Test script, run in object mode, creates the grid mesh and pokes it. The return of the poke operator gives the new faces as well as the new "poke" vertices. For example sake have added to a vertex group named "Poke".

enter image description here

Select the vertex group "Poke" and will see that all newly poked, (linked to 4 edges) verts are in it. Alternatively see edit mode answer below.

Object Mode Script

Create a new mesh, load up the newly created bmesh, add a new object with the mesh and link to scene. No operators, no edit mode toggling, no selecting....

import bpy
import bmesh
context = bpy.context
collection = context.collection

bm = bmesh.new()
bmesh.ops.create_grid(bm,
        size=11.95,
        x_segments=30,
        y_segments=30
        )

ret = bmesh.ops.poke(bm, faces=bm.faces)
verts = [v.index for v in ret['verts']]

me = bpy.data.meshes.new("PokedGrid")
bm.to_mesh(me)
ob = bpy.data.objects.new("PokedGrid", me)
vg = ob.vertex_groups.new(name="Poke")
vg.add(verts, 1.0, 'ADD')
context.collection.objects.link(ob)

Edit mode.

Run this test script in edit mode. Selects all vertices with 8 linked edges.

enter image description here

import bpy
import bmesh

context = bpy.context
me = context.edit_object.data

bm = bmesh.from_edit_mesh(me)
for v in bm.verts:
    v.select = len(v.link_edges) == 8

bmesh.update_edit_mesh(me)

Lastly on distribution, recommend collections.defaultdict

import bpy
import bmesh
from collections import defaultdict

context = bpy.context
me = context.edit_object.data

bm = bmesh.from_edit_mesh(me)

verts = defaultdict(list)

for v in bm.verts:
    verts[len(v.link_edges)].append(v.index)

print("Total Verts", len(bm.verts))
for k, v in verts.items():
    print(k, "linked edges", len(v))

Output.

Total Verts 1741
3 linked edges 4
5 linked edges 112
8 linked edges 784
4 linked edges 841
| improve this answer | |
$\endgroup$
  • $\begingroup$ This answer was especially helpful with pointing towards bmesh.ops - I have not seen these in any of the tutorials I've been watching. I will keep an eye on this. $\endgroup$ – Geo Vogler Mar 9 at 18:01

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.