0
$\begingroup$

enter image description here

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:
        vg.add([vert.index], 1.0, 'ADD')
  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. enter image description here
$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$ Aug 5, 2023 at 14:43
  • $\begingroup$ Got part 1 done. $\endgroup$
    – Spearhead
    Aug 5, 2023 at 14:58
  • $\begingroup$ 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… $\endgroup$ Aug 5, 2023 at 15:59

1 Answer 1

4
$\begingroup$

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:
        vg.add([vert.index], 1.0, 'ADD')
        
        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

About BMesh
You can easily get connected_verts using using bmesh.

intersection_dic = {}

for vert in bm.verts:
    if len(vert.link_edges) < 3:
        continue

    connected_verts = [e.other_vert(vert).index for e in vert.link_edges]

    intersection_dic[vert.index] = connected_verts
    dvert = vert[dvert_lay]
    dvert[group_index] = 1.0
$\endgroup$
1
  • $\begingroup$ I already got to that same answer, but thanks! $\endgroup$
    – Spearhead
    Aug 7, 2023 at 4:52

You must log in to answer this question.

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