Skip to content

Instantly share code, notes, and snippets.

@NSDesign
Created October 13, 2015 00:13
Show Gist options
  • Save NSDesign/7ff354187c791c6992b2 to your computer and use it in GitHub Desktop.
Save NSDesign/7ff354187c791c6992b2 to your computer and use it in GitHub Desktop.
Carve polygon curves by attribute or expression
node = hou.pwd()
geo = node.geometry()
u_parm = []
for pr in geo.prims():
node.setCurPrim(pr)
u_parm.append(node.evalParm("u_carve_start"))
# Reference u parameter used as the carve parametric location on the curve
# u = hou.ch("../null2/parm")
# Create u attribute used to test against u parameter
vu = geo.addAttrib(hou.attribType.Vertex, "u", -1.0)
# Create storage for the original primitives
prims_orig = []
# Loop over the input primitives
for prim in geo.prims():
# Store primitve 0 for each primitive instead of each primitive
# This is due the primitive count updating as each primitive is deleted
prims_orig.append(geo.prims()[0])
# Get the position(x,y,z) from the u location parameter on the curve
u = u_parm[prim.number()]
p_u = prim.positionAtInterior(u, 0, 0)
# Set the vertex attribute vu - 0-1 over each primitive
nv = prim.numVertices() - 1
for v in prim.vertices():
val = float(v.number()) / nv % nv
v.setAttribValue(vu, val)
# Store the points of each primitve above and below the u paramete(user defined)
pts_below_u = []
pts_above_u = []
for v in prim.vertices():
if u > v.attribValue(vu):
pts_below_u.append(v.point())
elif u < v.attribValue(vu):
pts_above_u.append(v.point())
# For each prim create 2 new points - one for the above u prim and one for below prim
# Set both point positio to the u parameter position
new_pts = []
for i in range(2):
point = geo.createPoint()
point.setPosition(p_u)
new_pts.append(point)
pts_below_u.append(new_pts[0])
pts_above_u.insert(0, new_pts[1])
# Now to create the new prims for above and below the u parameter based on the points stored
# in pts_above_u and pts_below_u resp.
new_polys = []
for i in range(2):
poly = geo.createPolygon()
poly.setIsClosed(False)
new_polys.append(poly)
for i in pts_below_u:
new_polys[0].addVertex(i)
for i in pts_above_u:
new_polys[1].addVertex(i)
# Refernce toggle parameters that define whether to keep primitives above or below
keep_above = hou.ch("keep_above")
if keep_above == 0:
geo.deletePrims([new_polys[0]])
keep_below = hou.ch("keep_below")
if keep_below == 0:
geo.deletePrims([new_polys[1]])
# Delete the original primitves
for pr in prims_orig:
geo.deletePrims([pr])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment