# How do I Iterate over vertices to perform operation on them and connected verts

In my object I have a number of those kind of objects that represent road intersections.

1. I want a way to iterate through the vertices and find all intersections (any vertex that has 3 or more other vertices connected to it. All these vertices will go into "Intersections" vertex group.
import bpy

obj = bpy.context.active_object
mesh = obj.data
bpy.ops.object.mode_set(mode='OBJECT')

vg = obj.vertex_groups.new(name="Intersection_Points")

# Loop through all vertices in the mesh
for vert in mesh.vertices:
connected_verts = set()

# Loop through all edges connected to the vertex
for edge in mesh.edges:
if vert.index in edge.vertices:
connected_verts.update(edge.vertices)

if len(connected_verts) >= 4:
1. Out of each Intersection, I want to separate each possible set of 3 vertices, in this example - I want it to end up with those in the picture below and then be able to perform an operation on each of those, or put each one into its own vertex group.
• Separate your question into two :P One is how to find intersections, another is... I'm not sure... Looks like you want to separate each possible path between two vertices within a single group of vertices. Commented Aug 5, 2023 at 14:43
• Got part 1 done. Commented Aug 5, 2023 at 14:58
• Have you tested that it works correctly? I don't think so, as for each vert you set connected_verts to an empty list, discarding data obtained in previous iterations… Commented Aug 5, 2023 at 15:59

The first question
As Markus von Broady said, it's more convenient to keep the data so you can use it later. So I suggest to use a dictionary with an intersection vertex as key and a set of linked ones as value.

intersection_dic = {}

#do loop
connected_verts.remove(vert.index)
intersection_dic[vert.index] = connected_verts

import bpy

bpy.ops.object.mode_set(mode='OBJECT')

obj = bpy.context.active_object
mesh = obj.data

vg = obj.vertex_groups.new(name="Intersection_Points")

intersection_dic = {}

# Loop through all vertices in the mesh
for vert in mesh.vertices:
connected_verts = set()

# Loop through all edges connected to the vertex
for edge in mesh.edges:
if vert.index in edge.vertices:
connected_verts.update(edge.vertices)

if len(connected_verts) >= 4:

connected_verts.remove(vert.index)
intersection_dic[vert.index] = connected_verts

The seconde question
You can use the itertools module to create sets of 3 vertex indices like your drawing. That module contains itertools.combinations.

import itertools

for vc, value in intersection_dic.items():
print(f"[{vc}]")
for va, vb in itertools.combinations(value, 2):
print(f"{va} - {vc} - {vb}")

outcome

[4]
8 - 4 - 1
8 - 4 - 2
1 - 4 - 2
[5]
0 - 5 - 1
0 - 5 - 8
1 - 5 - 8
[6]
0 - 6 - 8
0 - 6 - 3
8 - 6 - 3
[7]
8 - 7 - 2
8 - 7 - 3
2 - 7 - 3
[8]
4 - 8 - 5
4 - 8 - 6
4 - 8 - 7
5 - 8 - 6
5 - 8 - 7
6 - 8 - 7

You can easily get connected_verts using using bmesh.

intersection_dic = {}

for vert in bm.verts: