Joining 'Neighboring' Meshes / Connected Graphs to Meshes

If anyone is interested, I’ve done it by using the networkx library and building on some sample mesh to graph script to identify connected graphs, and weave back to mesh from line segments output through weaverbird:

import Rhino as rc
import networkx as nx
import clr
clr.AddReference("Grasshopper")
from Grasshopper.Kernel.Data import GH_Path
from Grasshopper import DataTree

# [function] Mesh to networkx graph
def meshVertexGraph(mesh,weightMode):
    """ Make a networkx graph with mesh vertices as nodes and mesh edges as edges """
    # Create graph
    g = nx.Graph()
    for i in range(mesh.Vertices.Count):
        # Get vertex as point3D
        pt3D = rc.Geometry.Point3d(mesh.Vertices.Item[i])
        # Add node to graph and get its neighbours
        g.add_node(i,point=pt3D)
        neighbours = mesh.Vertices.GetConnectedVertices(i)
        # Add edges to graph
        for n in neighbours:
            if n > i:
                line = rc.Geometry.Line(mesh.Vertices.Item[i],mesh.Vertices.Item[n])
                if weightMode == "edgeLength":
                    w = line.Length
                elif weightMode == "sameWeight":
                    w = 1
                g.add_edge(i,n,weight=w,line=line)
    return g


# Initiate DataTree
p_all = DataTree[rc.Geometry.Point3d]()
ln_all = DataTree[rc.Geometry.Line]()

for m_cnt in range(len(mesh)):
    m = mesh[m_cnt]
    # mesh to networkx graph
    g = meshVertexGraph(m,"edgeLength")

    # create graphs of connected edges
    subg = nx.connected_component_subgraphs(g)
    subg_list = list(subg)

    #output
    for mgraph in range(len(subg_list)):
        n_list = [subg_list[mgraph].node[i]["point"] for i in subg_list[mgraph].nodes()]
        e_list = [e[2]["line"] for e in subg_list[mgraph].edges(data=True)]
        p_all.AddRange(n_list, GH_Path(mgraph))
        ln_all.AddRange(e_list, GH_Path(mgraph))

pt = p_all
ln = ln_all
1 Like