Forgot password?

Create an account!


« back

RhinoScript – Some Advice with L-system


Please log in to write a message.

  • 3. Diazo (Oct 17, 2009 02.09):

    Thank you, that helps tremendously!
    I do have one more problem, and I'm sure it's a noob one at that.

    Here is a portion of the code

    Option Explicit
    'Original Script found at McNeel Destroyed by Diaz-Oh
    'Script version 10/09

    Call RecursiveTree()
    Sub RecursiveTree()
        Dim arrPlantingPoint, X
        X = (Rnd + 2)
        arrPlantingPoint = Rhino.GetPoint( "Select an Origin Point" )
        Dim strTrunk
        'Justin mess with array
        strTrunk = Rhino.AddLine( arrPlantingPoint, Rhino.PointAdd( arrPlantingPoint, Array(Rnd,Rnd,15) ) )
        Rhino.EnableRedraw False ' Forces the system to wait to draw all curves at the end of script
        ' Edit  the Generations, Angle of branches, Number of Splitting
        RecursiveBranches strTrunk, 8, 45, 2
        Rhino.EnableRedraw True ' Redraws everything.
    End Sub

    Function RecursiveBranches( strLine, intGenerations, intBranchAngle, intNumBranches )
        ' This makes sure all generations are created
        ' remaining generations (i.e. branches) equals 0.
        If intGenerations <= 0 Then Exit Function
        ' Create a vector that describes the direction of the branch that we're
        ' drawing from, the branch (or trunk) that was passed in strLine.
        Dim vecDir
        Dim a
        vecDir = Rhino.VectorCreate( Rhino.CurveEndPoint(strLine), Rhino.CurveStartPoint(strLine) )
        'Justin This next Array controls the lengths of the branches
        'It's set to pick 1 of 3 numbers randomly
        a = Array(1.2, 1.3, 1.4)
        vecDir = Rhino.VectorScale( vecDir, a (Rnd*2))

        ' To rotate the new branch into place, we need axes to rotate about. The
        ' easiest way is to create a plane and use the axes it calculates. To
        ' create a plane, we need a point and a vector, so here we grab the
        ' endpoint of the line that was passed to the function.
        Dim ptEnd
        ptEnd = Rhino.CurveEndPoint(strLine)
        ' Create the plane.
        Dim arrPlane
        arrPlane = Rhino.PlaneFromNormal( ptEnd, vecDir )
        ' Variables we need for iteration. i is the counter, theta and phi are
        ' angles used for rotates, vecNew is the new rotated vector (i.e. the new
        ' branch)
        Dim i, theta, phi, vecNew
        For i = 1 To intNumBranches
            ' Randomly create the two angles for rotation.
            theta = Rnd(3) * intBranchAngle ' Drops the branch down.
            phi = Rnd() * 45 ' Spins the branch around the original axis.
            ' Create the new vector by rotating the original vector.
            vecNew = Rhino.VectorRotate( vecDir, theta, arrPlane(1) )
            vecNew = Rhino.VectorRotate( vecNew, phi, arrPlane(3) )
            ' Draw the new branch.
            Dim newBranch
            newBranch = DrawVector( arrPlane(0), vecNew )
            ' Draw another tree at the end of the branch we just drew. This is
            ' the recursive part of the function...
            RecursiveBranches newBranch, intGenerations - 1, intBranchAngle, intNumBranches
    End Function

    ' This is a convenience method for drawing vectors. You give it an
    ' origin point (the end of the vector), and it draws a line towards
    ' the tip of the vector, given as the second argument.
    Function DrawVector( arrOrigin, arrVector )
        ' We need a variable that contains the tip of the vector.
        Dim arrVectorTip
        ' Get the vector's tip by adding the given vector to the
        ' origin point. Remember, the vector given only describes
        ' direction and magnitude.
        arrVectorTip = Rhino.PointAdd( arrOrigin, arrVector )
        ' Draw the vector's stem and keep track of the object id.
        Dim strVectorStem
        strVectorStem = Rhino.AddLine( arrOrigin, arrVectorTip )
        ' Return the drawn line.
        DrawVector = strVectorStem
    End Function

    This was pieced together from the McNeel script. Everything works fine with the exception of the

    vecDir = Rhino.VectorCreate( Rhino.CurveEndPoint(strLine), Rhino.CurveStartPoint(strLine) )
        'Justin This next Array controls the lengths of the branches
        'It's set to pick 1 of 3 numbers randomly
        a = Array(1.2, 1.3, 1.4)
        vecDir = Rhino.VectorScale( vecDir, a (Rnd*2))

    This is scaling each new generation of branches, however I don't want it to scale them, just keep redrawing the same 3 random lengths until the number of specified generations is reached...but when I try and replace the

    Rhino.VectorScale( vedDir, a (Rnd*2))[\code] with something that won't scale the branches, the script dies!

    How would I organize this?

    I hope this makes sense,  if not I will try and explain everything better.

    Thanks again for all your help, you have made live easier!
  • 2. Johannes (Oct 16, 2009 13.38):


    here ist just a short script how to get a random value out of an array:

    Dim a   : a = array(5,8,12)     
    Msgbox a(Rnd*2)

    The Randomize function acts between 0 and X. If you write your code like Rnd*X ... then you will get an result between 0 and X.

    You can save all your new branches (identifier) of each generation to change their layer:

    Rhino.ObjectLayer (arrResult, "Layer03")

    Or you work with the currentLayer:

    Rhino.CurrentLayer ("Layer03")


  • 1. Diazo (Oct 16, 2009 12.38):


    My name is Diazo, nice to meet you all! I just joined here a couple of days ago, and as I traverse this new world of scripting, I have already found a great deal of help just creeping around this site. There is however something I'm having trouble with and was wondering if you guys could lead me in the proper direction.

    I'm attempting to write a L-system script. I have been searching for random scripts online and modifying them to fit what I'm trying to do just to help me understand how everything works. However I have hit a bit of a roadblock.

    What I'm trying to do now is upon each new generation, have the script randomly pick 1 of 3 lengths for the branches it generates. As of right now I have the line:

    vecDir = Rhino.VectorScale( vecDir, Rnd + 0.9 )

    If I'm not mistaken, this is scaling the previous vector randomly but adding .9 to make sure it doesn't fall off into minuscule segments.

    Is there a way to define 3 parameters and then let the script randomly pick between those parameters for the length of the vector branches? Would it also be possible to organize each different vector length to it's on separate layer.

    For example:

    The next branch length could be 5, 8, or 12. All 5 branches would end up on Layer 2, all 8 branches would end up on Layer 3, etc.

    Is this possible?

    Any help, no matter what it is would be greatly appreciated. I also apologize in advance if some of this doesn't make sense, I'm still rather new at this scripting stuff and I also haven't slept in almost 2 days, thanks to midterms!

    Thanks again!


Why are these buttons gray?