def crackpolygon(meshes, count):
    tempMeshes = meshes
    newMeshes = []
    if count == 0:
        return 1
    else:
        for mesh in tempMeshes:

            if rs.MeshVertexCount(mesh) != 3 and rs.MeshVertexCount(mesh) != 4:
                countV = rs.MeshVertexCount(mesh)
                print "mesh has too many vertices"
            else:
                #print "Cool"
                centroid = rs.MeshAreaCentroid(mesh)
                normals = rs.MeshFaceNormals(mesh)
                centroid = rs.PointAdd(centroid, normals[0] * (count / 5))
                vertices = rs.MeshVertices(mesh)
                for i in range(1, len(vertices)):
                    newVertices = []
                    newVertices.append(vertices[i])
                    newVertices.append(centroid)
                    newVertices.append(vertices[i - 1])
                    newFaces = [[0, 1, 2]]
                    newMesh = rs.AddMesh(newVertices, newFaces)
                    newMeshes.append(newMesh)

                newVertices = []
                newVertices.append(vertices[0])
                newVertices.append(centroid)
                newVertices.append(vertices[len(vertices) - 1])
                newFaces = [[0, 1, 2]]
                newMesh = rs.AddMesh(newVertices, newFaces)
                newMeshes.append(newMesh)

        return crackpolygon(newMeshes, count - 1)
Beispiel #2
0
def get_motion_vectors(mesh_id, step):
    """Returns a list of motion vectors (for face flow, they are just normals)
    in the same order as the faces in the Rhino representation of the input mesh.
    """
    normals = rs.MeshFaceNormals(mesh_id)
    for i in xrange(len(normals)):
        normals[i] = vu.VectorResize(normals[i], -step)
    return normals
def Main():
    mesh=rs.GetObjects("select mesh",rs.filter.mesh)
    start=rs.GetObject("select start points",rs.filter.point)
    refVec = rs.GetObject("select crv direction",rs.filter.curve)
    ang=rs.GetReal("enter branching angle",30)
    length=rs.GetReal("enter branch length",15)
    gen=rs.GetInteger("enter number of generations",3)
    lines=rs.AddLayer("branch",[255,0,0])
    rs.EnableRedraw(False)
    rs.CurrentLayer(lines)
    vec=tanVec(refVec,start)
    centers=rs.MeshFaceCenters(mesh)
    index=rs.PointArrayClosestPoint(centers,start)
    norm=rs.MeshFaceNormals(mesh)[index]
    vec=rs.VectorRotate(vec,0,norm)
    branches=baseBranch(mesh,start,vec,ang,length,gen)
    return branches
def baseBranch(mesh,start,vec,ang,length,gen):
    end=rs.PointAdd(start,vec*length)
    end=rs.MeshClosestPoint(mesh,end)[0]
    branch1=rs.AddLine(start,end)
    projBranch1=rs.PullCurveToMesh(mesh,branch1)
    branches=[projBranch1]
    stored=branches
    newBranches=[]
    i=0
    count=0
    while i < gen:
        i=i+1
        for branch in branches:
            end=rs.CurveEndPoint(branch)
            param=rs.CurveClosestPoint(branch,end)
            vec=rs.CurveTangent(branch,param)
            vec=rs.VectorUnitize(vec)
            index=rs.MeshClosestPoint(mesh,end)[1]
            norm=rs.MeshFaceNormals(mesh)[index]
            vec=rs.VectorRotate(vec,ang,norm)
            start=rs.PointAdd(end,vec*length)
            start=rs.MeshClosestPoint(mesh,start)[0]
            newBranch=rs.AddLine(end,start)
            pulledBranch=rs.PullCurveToMesh(mesh,newBranch)
            rs.DeleteObject(newBranch)
            newBranches.append(pulledBranch)
            vec=rs.VectorRotate(vec,-2*ang,norm)
            start=rs.PointAdd(end,vec*length)
            start=rs.MeshClosestPoint(mesh,start)[0]
            newBranch=rs.AddLine(end,start)
            pulledBranch=rs.PullCurveToMesh(mesh,newBranch)
            rs.DeleteObject(newBranch)
            if rs.Distance(start,rs.CurveEndPoint(pulledBranch))<length/2:
                newBranches.append(pulledBranch)
        branches=newBranches
        stored.extend(branches)
        newBranches=[]
        count=count+1
    #for i in range(len(stored)):
    #    squareSect(stored[i],1,1)
    print count
    return stored
Beispiel #5
0
def reflectLight():
    """
    
    This script will place a (pre)selected light on a surface, polysurface, subd or mesh face by reflection
    After the script has run, it will select the modified light, so you can quickly repeat
    the placement with this light
    script by Gijs de Zwart
    www.studiogijs.nl
    
    """

    object = rs.GetObject("select light", preselect=True, filter=262400)

    if not object:
        return
    light = rs.coercerhinoobject(object)
    loc = light.LightGeometry.Location
    dir = light.LightGeometry.Direction

    obj = rs.GetObject("select surface to reflect light on",
                       filter=40,
                       subobjects=True)

    if not obj:
        return
    if type(rs.coercerhinoobject(obj)) == Rhino.DocObjects.BrepObject or type(
            rs.coercerhinoobject(obj)) == Rhino.DocObjects.SubDObject:
        pt = rs.GetPointOnSurface(obj)
        if not pt:
            return
        pt_srf = rs.SurfaceClosestPoint(obj, pt)
        if not pt_srf:
            print "could not find surface point"
            return
        normal = rs.SurfaceNormal(obj, pt_srf)
        if not normal:
            print "could not calculate surface normal"
            return

    else:
        pt = rs.GetPointOnMesh(obj)
        if not pt:
            return
        pt_mesh, index = rs.MeshClosestPoint(obj, pt)
        if not pt_mesh:
            print "could not find mesh point"
        normals = rs.MeshFaceNormals(obj)
        if normals:
            normal = normals[index]
        if not normal:
            print "could not calculate surface normal"
            return

    camLoc = sc.doc.Views.ActiveView.ActiveViewport.CameraLocation
    camTar = sc.doc.Views.ActiveView.ActiveViewport.CameraTarget
    camDir = sc.doc.Views.ActiveView.ActiveViewport.CameraDirection

    plane = Rhino.Geometry.Plane(pt, normal, -camDir)
    #sc.doc.Views.ActiveView.ActiveViewport.SetConstructionPlane(plane)
    #line = Rhino.Geometry.Line(camLoc, pt)
    #trans = Rhino.Geometry.Transform(angleRadians,rotationAxis, rotationCenter)
    trans = Rhino.Geometry.Transform.Rotation(math.pi, normal, pt)
    #rs.RotateObject(line, pt, 180, plane.XAxis, copy=True)

    #line.Transform(trans)
    camLoc.Transform(trans)

    newdir = rs.VectorCreate(pt, camLoc)
    newdir.Unitize()
    newplane = rs.PlaneFromNormal(camLoc, newdir)
    if light.LightGeometry.IsRectangularLight:

        lightX = light.LightGeometry.Width
        lightWidth = lightX.Length
        lightY = light.LightGeometry.Length
        lightHeight = lightY.Length
        lightO = light.LightGeometry.Location

        lightX = lightWidth * newplane.XAxis
        lightY = lightHeight * newplane.YAxis
        light.LightGeometry.Width = lightX
        light.LightGeometry.Length = lightY

        camLoc -= lightX / 2
        camLoc -= lightY / 2
        light.LightGeometry.Location = camLoc

        #mid = Rhino.Geometry.Vector3d(lightWidth/2, lightHeight/2, 0)
        #trans = Rhino.Geometry.Transform.ChangeBasis(Rhino.Geometry.Plane.WorldXY, lightPlane)
        #mid.Transform(trans)
        #light.LightGeometry.Location -=mid
    if light.LightGeometry.IsSpotLight:
        light.LightGeometry.Location = camLoc
        newdir *= rs.VectorLength(dir)
    light.LightGeometry.Direction = newdir

    #sc.doc.Objects.AddLine(line)

    sc.doc.Lights.Modify(object, light.LightGeometry)
    rs.SelectObject(object)