Skip to content

Instantly share code, notes, and snippets.

@sbaer
Created April 27, 2012 20:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbaer/2512920 to your computer and use it in GitHub Desktop.
Save sbaer/2512920 to your computer and use it in GitHub Desktop.
Recursive Panelization translation into python
"""recursively subdivide surface based on curvature
author: robert stuart-smith | 2008 | www.kokkugia.com
translated to python by S. Baer (2012)"""
import rhinoscriptsyntax as rs
import scriptcontext
def PanelizeSurface(surface, subdivisions, generation):
#conditional statement to stop subdividing infinitely
#(as we will Call the Function recursively)
if generation<=0:
return
#extract edge curves
edge_curves = rs.DuplicateEdgeCurves(surface)
#lists for storing curves in both directions of surface; u & v directions
arrCrvsU = []
arrCrvsV = []
#do for 2 curves one in both directions of surface:
for i in [0,1]:
#divide curve by number of points (using variable subD)
arrCrvPts = rs.DivideCurve(edge_curves[i], subdivisions, False, True)
for curve_point in arrCrvPts:
#extract isocurves using points in the array above
arrPara = rs.SurfaceClosestPoint(surface, curve_point)
#use case statement to seperate isocurves into their direction
#(u or v) and store in u or v array
if i==0: arrCrvsU.append(rs.ExtractIsoCurve(surface, arrPara, 1))
elif i==1: arrCrvsV.append(rs.ExtractIsoCurve(surface, arrPara, 0))
#loop through curves to create surfaces
arrSrf = []
for i in range(len(arrCrvsU)-1):
for j in range(len(arrCrvsV)-1):
edges = [arrCrvsU[i][0], arrCrvsV[j][0], arrCrvsU[i+1][0], arrCrvsV[j+1][0]]
arrSrf.append(rs.AddEdgeSrf(edges))
#delete the original surface
rs.DeleteObject(surface)
#loop through created panels
for srf in arrSrf:
#get surface centre point
centroid, error = rs.SurfaceAreaCentroid(srf)
#get surface parameter at the ctr point
arrPara = rs.SurfaceClosestPoint(srf, centroid)
#get the surface curvature at the surface parameter position
arrCurvature = rs.SurfaceCurvature(srf, arrPara)
#size up and simplify curvature result for legibility
curvature = abs(arrCurvature[7] * 1000)
#colour surface based on mean curvature
color = curvature*2
if color>125: color=125
rs.ObjectColor(srf, (color*2,0,color))
#based on curvature choose to subdivide further or not
#ie. recursively call this function
if curvature > 100: PanelizeSurface(srf, 2, generation-1)
if generation>3:
scriptcontext.escape_test()
rs.EnableRedraw(True)
rs.Redraw()
rs.EnableRedraw(False)
if __name__=="__main__":
#get surface
strSrf = rs.GetObject("pick a surface", rs.filter.surface)
#call function to subdivide
rs.EnableRedraw(False)
PanelizeSurface(strSrf, 10, 5)
rs.EnableRedraw(True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment