Skip to content

Instantly share code, notes, and snippets.

@yone80
Last active April 9, 2016 17:36
Show Gist options
  • Save yone80/bcf592fe2557201d03e7148ec74b0935 to your computer and use it in GitHub Desktop.
Save yone80/bcf592fe2557201d03e7148ec74b0935 to your computer and use it in GitHub Desktop.
Houdini Python SOP
def checkConnectivity(e1, e2):
chk = e1.points()[0].number == e2.points()[0].number
chk = chk or e1.points()[0].number == e2.points()[1].number
if chk: return 1
chk = e1.points()[1].number == e2.points()[0].number
chk = chk or e1.points()[1].number == e2.points()[1].number
if chk: return 2
return 0
def isNext(e1, e2):
chk = e1.points()[0].number == e2.points()[1].number
if chk: return 1
chk = e1.points()[1].number == e2.points()[1].number
if chk: return 2
return 0
node = hou.pwd()
geo = node.geometry()
prefix = node.parm('prefix').eval().strip()
if prefix == '':
prefix = 'edge'
else:
prefix = prefix.replace(' ', '_')
ptn = node.parm('group').eval().strip()
#if ptn == '': ptn = '*'
if ptn != '':
edges = set( geo.globEdges(ptn) )
oneside_edges = set()
for e in edges:
sideone = False
sidetwo = False
for testedge in edges:
if e is testedge: continue
chk = checkConnectivity(e, testedge)
if chk == 1: sideone = True
if chk == 2: sidetwo = True
if sideone and sidetwo: break
if sideone + sidetwo == 1:
if sideone: oneside_edges.add( (e, 1) )
if sidetwo: oneside_edges.add( (e, 2) )
used_edges = set()
def recursiveConnectedEdges(current, grp):
unused = edges - used_edges # update unused for recursive
for e in unused:
unused = edges - used_edges # update unused for loop
if e in unused:
chk = isNext(e, current)
if chk > 0:
used_edges.add(e)
next_edge = e
if chk == 2:
next_edge = geo.findEdge(e.points()[1], e.points()[0])
grp.add(next_edge)
recursiveConnectedEdges(next_edge, grp)
i = 1
for e in oneside_edges:
unused = edges - used_edges
if e[0] in unused:
used_edges.add(e[0])
cur_edge = e[0]
if e[1] == 1:
cur_edge = geo.findEdge(cur_edge.points()[1], cur_edge.points()[0])
grp = geo.createEdgeGroup( prefix + str(i) )
grp.add(cur_edge)
recursiveConnectedEdges(cur_edge, grp)
i += 1
rem = edges - used_edges
for e in rem:
unused = edges - used_edges
if e in unused:
used_edges.add(e)
grp = geo.createEdgeGroup( prefix + str(i) )
grp.add(e)
recursiveConnectedEdges(e, grp)
i += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment