"""
Script written by <Luis Quinones>
Script copyrighted by <complicitMatter><computationalMatter.com>
"""
import rhinoscriptsyntax as rs
import math
from System.Drawing import Color
def PointList(outputSelfORG,arrRebuildCount):
arrCrvPts = []
for i in range(len(outputSelfORG)):
rebuild = rs.RebuildCurve(outputSelfORG[i],2,arrRebuildCount)
arrCrvPtCount = rs.CurvePointCount(outputSelfORG[i])
samPoints = rs.CurvePoints(outputSelfORG[i])
for j in range(0,arrCrvPtCount):
arrCrvPts.append(samPoints[j])
return arrCrvPts
def MainSelfORG(Gens,Curves,Rebuild,arrRebuildCount,WELDACTION,MeshAction,dblRatio,Attract,AttractorPtsList,attPtThresh,dblRatioATT,strLayerNew,strObject,AttractorPoints):
if bool(MeshAction[1]) == True:
arrItems2 = [("Attraction Threshold",True),("Vector Move Scale Factor",False), ("Paint Affects Both",False), ("Rebuild_CURVES",False)]
arrOptionsBln2 = rs.CheckListBox(arrItems2,"CHOOSE","Paint_Influence")
if not arrOptionsBln2:
print "Options not array"
AttractorThreshold = arrOptionsBln2[0]
VectorTranslationScaling = arrOptionsBln2[1]
DoubleActionPaint = arrOptionsBln2[2]
CRVBUILD = arrOptionsBln2[3]
arrVerts = rs.MeshVertices(strObject)
color = rs.MeshVertexColors(strObject)
for x in range(len(Curves)):
rs.RebuildCurve(Curves[x],Rebuild,arrRebuildCount)
count = 0
for i in range(Gens):
arrTOPCRVS = []
arrBOTTCRVS = []
for j in range(len(Curves)):
arrNewPos = []
PointCount = rs.CurvePointCount(Curves[j])
arrCrvDivMain = rs.CurvePoints(Curves[j])
if bool(WELDACTION[1]) == True:
PointCount = PointCount - 2
arrCrvDiv = arrCrvDivMain[1:-1]
else:
arrCrvDiv = arrCrvDivMain
arrCrvStartPt = rs.CurveStartPoint(Curves[j])
arrCrvEndPt = rs.CurveEndPoint(Curves[j])
for k in range(PointCount):
arrCrvPts = []
if bool(MeshAction[1]) == True:
threshMult = ColorAction(arrVerts,color,arrCrvDiv[k])
if threshMult == 0 :
threshMult == 0 + 0.01
else:
threshMult = 1
if bool(MeshAction[1]) == True:
if bool(VectorTranslationScaling[1]) == True:
dblRatio2 = dblRatio*threshMult
newThreshold = Attract
if bool(MeshAction[1]) == True:
if bool(DoubleActionPaint[1]) == True:
dblRatio2 = dblRatio*threshMult
newThreshold = Attract * threshMult
if bool(AttractorPoints[1]) == True:
if not AttractorPtsList:
print "really"
indexClosestAtt = rs.PointArrayClosestPoint(AttractorPtsList,arrCrvDiv[k])
if not indexClosestAtt:
bambam = 0
attPt = AttractorPtsList[indexClosestAtt]
dist2 = rs.Distance(attPt,arrCrvDiv[k])
else:
dist2 = 1000000
if dist2 < attPtThresh :
attvector = rs.VectorCreate(attPt,arrCrvDiv[k])
attvector = rs.VectorScale(attvector,dblRatioATT)
NewPosition = rs.PointAdd(attvector,arrCrvDiv[k])
else:
for L in range(len(Curves)):
if bool(MeshAction[1]) == True and bool(AttractorThreshold[1]) == True:
newThreshold = Attract * threshMult
dblRatio2 = dblRatio
else:
newThreshold = Attract
dblRatio2 = dblRatio
if j != L :
arrCrvParam = rs.CurveClosestPoint(Curves[L],arrCrvDiv[k])
crvPt = rs.EvaluateCurve(Curves[L],arrCrvParam)
arrCrvPts.append(crvPt)
IndClosestCrv = rs.PointArrayClosestPoint(arrCrvPts,arrCrvDiv[k])
crvPt = arrCrvPts[IndClosestCrv]
dist = rs.Distance(arrCrvDiv[k],crvPt)
if dist < newThreshold :
vector = rs.VectorCreate(crvPt,arrCrvDiv[k])
vector = rs.VectorScale(vector,dblRatio2)
NewPosition = rs.PointAdd(vector,arrCrvDiv[k])
else:
NewPosition = arrCrvDiv[k]
arrNewPos.append(NewPosition)
if bool(WELDACTION[1]) == True:
arrNewPos.insert(0,arrCrvStartPt)
arrNewPos.insert(PointCount+1,arrCrvEndPt)
newcurve = rs.AddCurve(arrNewPos,Rebuild)
rs.ObjectLayer(newcurve,strLayerNew)
rs.DeleteObject(Curves[j])
Curves[j] = newcurve
return Curves
def PullAction(srfArr,crvArr,threshold,snapThreshold,ratio):
for i in range(len(crvArr)):
arrSrfPts = []
rs.EnableObjectGrips(crvArr[i],enable = True)
gripCount = rs.ObjectGripCount(crvArr[i])
for j in range(gripCount):
gripPos = rs.ObjectGripLocation(crvArr[i],j)
count = 0
for k in range(len(srfArr)):
if rs.IsMesh(srfArr[k]):
meshPt = rs.MeshClosestPoint(srfArr[k],gripPos)
srfPt = meshPt[0]
else:
srfUV = rs.SurfaceClosestPoint(srfArr[k],gripPos)
srfPt = rs.EvaluateSurface(srfArr[k],srfUV)
arrSrfPts.append(srfPt)
count += 1
closeSrfInd = rs.PointArrayClosestPoint(arrSrfPts,gripPos)
srfPt = arrSrfPts[closeSrfInd]
dist3 = rs.Distance(gripPos,srfPt)
if dist3 < threshold :
vec = rs.VectorCreate(srfPt,gripPos)
if dist3 > snapThreshold :
vec = rs.VectorScale(vec,ratio)
newPos2 = rs.PointAdd(vec,gripPos)
rs.ObjectGripLocation(crvArr[i],j,newPos2)
def ColorAction(arrVerts,color,pos):
index = rs.PointArrayClosestPoint(arrVerts,pos)
result = color[index]
colorR = rs.ColorRedValue(result)
lum = colorR/255
return lum
def Main():
rs.MessageBox("SelfOrganizing Network_Built in Options for Attractors or Mesh Paint Attraction",0,"BUNDLING ACTION")
arrItems = [("Attractor Points?",False),("Use Mesh Vertex Colors as Driver?",True),("Weld Start & End Points?",True),("Pull Geo?",True)]
arrOptionsBln = rs.CheckListBox(arrItems,"CHOOSE","Attraction_Type")
AttractorPoints = arrOptionsBln[0]
MeshAction = arrOptionsBln[1]
WELDACTION = arrOptionsBln[2]
pullGeom = arrOptionsBln[3]
if not arrOptionsBln:
print "Options not array"
#//////////////////////////////////////////////////////////////////////////////////////////////////////////////
#INPUTS
#//////////////////////////////////////////////////////////////////////////////////////////////////////////////
Gens = rs.GetInteger("Number of Generations",2,2,14)
Curves = rs.GetObjects("Select Base Crvs",rs.filter.curve)
textLocPt = rs.GetPoint("Pick location to create settings text")
Attract = rs.GetReal("CurvePointAttractionThreshold", 150)
dblRatio = rs.GetReal("Distance For Vector To Move", 0.75)
arrRebuildCount = rs.GetInteger("Curve Rebuild CP Count", 40)
Rebuild = rs.GetInteger("Degree To Rebuild Curves", 2)
strLayerNew = rs.AddLayer("NEWCURVES", Color.Red)
#//////////////////////////////////////////////////////////////////////////////////////////////////////////////
#//////////////////////////////////////////////////////////////////////////////////////////////////////////////
if bool(pullGeom[1]) == True :
srfArr = rs.GetObjects("Meshes to pull to",0)
pullthreshold = 150
snapThreshold = 90
ratio = 1
if bool(MeshAction[1]) == True :
strLayer = rs.AddLayer("ptStore", Color.White)
strObject = rs.GetObject("ColorMesh",filter = 32,select = False)
else:
strObject = 0
if bool(AttractorPoints[1]) == False:
attPtThresh = 0
dblRatioATT = 0
AttractorPtsList = []
else:
AttractorPts = rs.GetObjects("Attractor Points",rs.filter.point)
attPtThresh = rs.GetReal("AttractorPointThreshold", 40)
dblRatioATT = rs.GetReal("JUMP For Vector Towards ATTPT MOVE", .25)
AttractorPtsList = []
for z in range(len(AttractorPts)):
AttractorPtsList.append(rs.PointCoordinates(AttractorPts[z]))
#//////////////////////////////////////////////////////////////////////////////////////////////////////////////
#TEXT OUTPUT VARIABLES
#//////////////////////////////////////////////////////////////////////////////////////////////////////////////
text = rs.AddText( " Gens = "+str(Gens)+ " AttTHRESH = " + str(Attract) + " PTTHRESH = " + str(attPtThresh) +
" VECTORMOVE = " + str(dblRatio) + " ATTPT VECTOR JUMP = " + str(dblRatioATT), textLocPt, height = 2, justification = 2)
outputSelfORG = MainSelfORG(Gens,Curves,Rebuild,arrRebuildCount,WELDACTION,MeshAction,dblRatio,Attract,AttractorPtsList,attPtThresh,dblRatioATT,strLayerNew,strObject,AttractorPoints)
if bool(pullGeom[1]) == True:
PullAction(srfArr,outputSelfORG,pullthreshold,snapThreshold,ratio)
Main()