logo

BUNDLING_SET

"""
Script written by Luis Quinones
www.luisquinonesdesign.com
www.computationalmatter.com
Script version Tuesday, 28 June 2011 20:18:46
"""

import rhinoscriptsyntax as rs
import math
from System.Drawing import Color

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-1):                

                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.VectorAdd(vec,gripPos)                    
                    rs.ObjectGripLocation(crvArr[i],j,newPos2)

def ColorAction(centroid,gripPos,ave):
    index = rs.PointArrayClosestPoint(centroid,gripPos)
    closestTest = centroid[index]
    result = ave[index]
    #print result
    return result

def Main():

    rs.MessageBox("SelfOrganizing Network_Built in Options for Attractors or Mesh Paint Attraction",0,"BUNDLING ACTION")
    arrItems = [("AttractorPoints",False),("Want Some Mesh Action",False),("Wanna Pull It to Something",False)]
    #arrDefaults = [False,False,False]
    arrOptionsBln = rs.CheckListBox(arrItems,"CHOOSE","Attraction_Type")    
    if not arrOptionsBln:        
        print "Options not array"    
    AttractorPoints = arrOptionsBln[0]
    print AttractorPoints[1]
    MeshAction = arrOptionsBln[1]
    print MeshAction
    pullGeom = arrOptionsBln[2]
    print pullGeom

    Gens = rs.GetInteger("Number of Generations",6,1)
    Curves = rs.GetObjects("Select Base Crvs",rs.filter.curve)
    AttractorPts = rs.GetObjects("Attractor Points",rs.filter.point)
    Attract = rs.GetReal("CurvePointAttractionThreshold")
    attPtThresh = rs.GetReal("AttractorPointThreshold")
    dblRatio = rs.GetReal("Distance For Vector To Move")
    Rebuild = rs.GetInteger("Degree To Rebuild Curves")
    strLayerNew = rs.AddLayer("NEWCURVES")

    if bool(pullGeom[1]) == True :  
        srfArr = rs.GetObjects("Meshes to pull to",0)
        pullthreshold = rs.GetReal("Set Thresh for mesh attractor")
        snapThreshold = rs.GetReal("Set Snap Threshold")
        ratio = rs.GetReal("Ratio of Distance to move")
    if bool(MeshAction[1]) == True :        
        strLayer = rs.AddLayer("ptStore", Color.White)
        strObject = rs.GetObject("ColorMesh",filter = 32,select = False)

    if bool(AttractorPoints[1]) == False:
        print "ATTRACTOR False"
        attPtThresh = 0    
    else:   

        print "ATTRACTOR True"        

    if bool(MeshAction[1]) == True:    
        arrFaces = rs.MeshFaces(strObject,True)    
        color = rs.MeshVertexColors(strObject)    
        faceVerts = [0]    
        centroid = []   
        ave = []    
        if arrFaces:    
            i = 0   
            count2 = 0      
            while(i < len(arrFaces)):        

                arrFace = arrFaces[i],arrFaces[i+1],arrFaces[i+2],arrFaces[i+3]            
                if arrFace[2][0] == arrFace[3][0] and arrFace[2][1] == arrFace[3][1] and arrFace[2][2] == arrFace[3][2] :                   

                    arrHLS = rs.ColorRGBToHLS(color[count2]),rs.ColorRGBToHLS(color[count2+1]),rs.ColorRGBToHLS(color[count2+2]),rs.ColorRGBToHLS(color[count2+2])                  
                    sum = arrHLS[0][2] + arrHLS[1][2] + arrHLS[2][2]     
                    ave2 = (sum/3)    
                    if ave2 < 0.2:   
                        ave2 = 0    
                    ave.append(ave2)                    
                    faceVerts[0] = [0,1,2,2]    
                    meshVerts = arrFace                    
                    mesh = rs.AddMesh(meshVerts,faceVerts)                   
                    if mesh :    
                        centroid.append(rs.MeshAreaCentroid(mesh))    
                    else:    
                        print "did not make mesh"                  
                    rs.DeleteObject(mesh)                   
                    count2 += 3                

                else:                   
                    arrHLS = rs.ColorRGBToHLS(color[count2]),rs.ColorRGBToHLS(color[count2+1]),rs.ColorRGBToHLS(color[count2+2]),rs.ColorRGBToHLS(color[count2+3])                    
                    sum = arrHLS[0][2] + arrHLS[1][2] + arrHLS[2][2] + arrHLS[3][2]   
                    ave2 = (sum/4)    

                    if ave2 < 0.2:  
                        ave2 = 0    
                    ave.append(ave2)                   
                    faceVerts[0] = [0,1,2,3]    
                    meshVerts = arrFace   
                    mesh = rs.AddMesh(meshVerts,faceVerts)                   
                    if mesh:   
                        centroid.append(rs.MeshAreaCentroid(mesh))    
                    else:   
                        print "did not make mesh"                   
                    rs.DeleteObject(mesh)                
                    count2 += 4  
                i +=4               
        if len(centroid) != len(ave):    
            print "# of aves does != # of points"          

    AttractorPtsList = []    
    for z in range(len(AttractorPts)):        
        AttractorPtsList.append(rs.PointCoordinates(AttractorPts[z]))        
    for i in range(Gens):        
            for j in range(len(Curves)):                
                arrNewPos = []
                PointCount = rs.CurvePointCount(Curves[j])
                arrCrvDiv = rs.CurvePoints(Curves[j])                
                if arrCrvDiv:
                    print "BOOM"
                else: 
                    print "Nope"                   
                for k in range(PointCount):                    

                    arrCrvPts = []                    
                    if bool(MeshAction[1]) == True:                  
                        threshMult = ColorAction(centroid,arrCrvDiv[k],ave)               
                        if threshMult == 0 :                       
                            threshMult == 0 + 0.01
                    else:                  
                        threshMult = 1                   
                    if not AttractorPtsList:                      
                        print "really"                      
                    indexClosestAtt = rs.PointArrayClosestPoint(AttractorPtsList,arrCrvDiv[k])
                    attPt = AttractorPtsList[indexClosestAtt]
                    dist2 = rs.Distance(attPt,arrCrvDiv[k])                    
                    if dist2 < attPtThresh :                        
                        NewPosition = attPt                        
                    else:                       
                        for L in range(len(Curves)):                            
                            newThreshold = Attract * threshMult                            
                            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])
                        #print "NextClosestIndex = ",IndClosestCrv
                        crvPt = arrCrvPts[IndClosestCrv]
                        #print crvPt                        
                        dist = rs.Distance(arrCrvDiv[k],crvPt)                        
                        if dist < newThreshold :                            
                            vector = rs.VectorCreate(crvPt,arrCrvDiv[k])
                            vector = rs.VectorScale(vector,dblRatio)
                            NewPosition = rs.VectorAdd(vector,arrCrvDiv[k])                           
                        else:                            
                            NewPosition = arrCrvDiv[k]                                                      
                    arrNewPos.append(NewPosition)                
                newcurve = rs.AddCurve(arrNewPos,Rebuild)
                rs.ObjectLayer(newcurve,strLayerNew)
                rs.DeleteObject(Curves[j])
                #rs.ObjectColor(newcurve,[j,j,j])
                Curves[j] = newcurve               
    basecrvs = rs.ObjectsByLayer(strLayerNew)
    if bool(pullGeom[1]) == True:       
        PullAction(srfArr,basecrvs,pullthreshold,snapThreshold,ratio)               
Main()         
Update
  • Share