Thursday, April 26, 2012

BubbleTree



import rhinoscriptsyntax as rs
import random
import math as mth


surface=rs.GetObject("get surface",8)

Udivisions=2
Vdivisions=20

domainU=rs.SurfaceDomain(surface,0)
domainV=rs.SurfaceDomain(surface,1)
floatUdivisions=(domainU[1]-domainU[0])/Udivisions
floatVdivisions=(domainV[1]-domainV[0])/Vdivisions

normalscale=10

leafList=[]
normallist=[]
templist0=[] 
ptList=[]
srf=[]


def centerPts(ptList):
    xList=[]
    yList=[]
    zList=[]
    for i in range(0,len(ptList)):
        pts=rs.PointCoordinates(ptList[i])
        x=pts[0]
        xList.append(x)
        y=pts[1]
        yList.append(y)
        z=pts[2]
        zList.append(z)
    centerPts=rs.AddPoint(mth.fsum(xList)/len(xList),mth.fsum(yList)/len(yList),mth.fsum(zList)/len(zList))
    return centerPts

rs.EnableRedraw(False)
for f in range(0,14):
    rotateSurface=rs.RotateObject(surface,[0,0,0],30*f,None,copy=True)
    vct=rs.VectorCreate([mth.sin(((2*mth.pi)/14)*f),mth.cos(((2*mth.pi)/14)*f),0],[0,0,0])
    rotateSurface=rs.RotateObject(rotateSurface,[0,0,0],random.randint(0,30)*f,vct,copy=False)
    leafList.append(rotateSurface)
    
    
for k in range(0,len(leafList)):
    leafPoints=[]
    normalList=[]
    centerPts0=[]
    centerPts1=[]
    crvList=[]
    for i in rs.frange(domainU[0],domainU[1]+0.001,floatUdivisions):
        templist=[]
        normaltemplist=[]
        for j in rs.frange(domainV[0],domainV[1]+0.001,floatVdivisions):
            pt=rs.EvaluateSurface(leafList[k],i,j)
            normalvector=rs.SurfaceNormal(leafList[k],[i,j])
            normalvector=rs.VectorUnitize(normalvector)
            normalvector=rs.VectorScale(normalvector,normalscale)
            normalpt=rs.VectorAdd(pt,normalvector)
            templist.append(pt)
            normaltemplist.append(normalpt)
            normalList.append(normalpt)
            leafPoints.append(pt)
    for a in range(0,len(leafPoints)-43):
        rs.AddSrfPt([leafPoints[a],leafPoints[a+21],leafPoints[a+1],leafPoints[a+22]])
        vector1=rs.VectorCreate(leafPoints[a],leafPoints[a+1])
        vector1=rs.VectorScale(vector1,-0.5)
        vector1=rs.VectorAdd(vector1,leafPoints[a])
        vector2=rs.VectorCreate(leafPoints[a+1],leafPoints[a+22])
        vector2=rs.VectorScale(vector2,-0.5)
        vector2=rs.VectorAdd(vector2,leafPoints[a+1])
        vector3=rs.VectorCreate(leafPoints[a+22],leafPoints[a+21])
        vector3=rs.VectorScale(vector3,-0.5)
        vector3=rs.VectorAdd(vector3,leafPoints[a+22])
        vector4=rs.VectorCreate(leafPoints[a+21],leafPoints[a])
        vector4=rs.VectorScale(vector4,-0.5)
        vector4=rs.VectorAdd(vector4,leafPoints[a+21])
        pt1=rs.AddPoint(vector1)
        pt2=rs.AddPoint(vector2)
        pt3=rs.AddPoint(vector3)
        pt4=rs.AddPoint(vector4)
        srf0=rs.AddSrfPt([pt1,pt2,pt3,pt4])
        srf.append(srf0)
        centerPoints=centerPts([pt1,pt2,pt3,pt4])
        crv2=rs.AddPolyline([pt1,pt2,pt3,pt4,pt1])
        centerPts0.append(centerPoints)
        crvList.append(crv2)
    for b in range(0,len(normalList)-43):
        pt5=rs.AddPoint(normalList[b])
        pt6=rs.AddPoint(normalList[b+21])
        pt7=rs.AddPoint(normalList[b+1])
        pt8=rs.AddPoint(normalList[b+22])
        srf1=rs.AddSrfPt([normalList[b],normalList[b+21],normalList[b+1],normalList[b+22]])
        centerPoints1=centerPts([pt5,pt6,pt7,pt8])
        centerPts1.append(centerPoints1)
    for c in range(0,len(centerPts0)):
        line=rs.AddLine(centerPts0[c],centerPts1[c])
        circle=rs.AddCircle(centerPts0[c],0.5)
        crv1=rs.ExtrudeCurve(circle,line)
        eval=rs.SurfaceClosestPoint(srf[c],centerPts0[c])
        normalvector1=rs.SurfaceNormal(srf[c],eval)
        normalvector1=rs.VectorUnitize(normalvector1)
        normalvector1=rs.VectorScale(normalvector1,random.randint(0,60))
        normalpt1=rs.VectorAdd(centerPts0[c],normalvector1)
        crv2=rs.AddPolyline([pt1,pt2,pt3,pt4,pt1])
        crv2=rs.ExtrudeCurvePoint(crvList[c],normalpt1)
        spheres=rs.AddSphere(normalpt1,random.random()*3)
    templist0.append(templist)
    normallist.append(normaltemplist)
rs.EnableRedraw(True)

1 comment: