Advanced Scripting: 01 Branching

Software: 
Tag: 
Video Duration: 
5 minutes
Author: 
Zach Downey

This is the first in a series of advanced scripting tutorials.  Before taking this one on, make sure you check out the intro tutorials first.  This is a really short video that just describes how the component operates.  I'll spare you all the pain of whatching me type out all the code.  Instead, check out the code below, or download the .gh file.

//Set the branch angle by converting it from degs to radians
    double bAngRad = ((Math.PI / 180) * branchAngle);
    List<Line> lines = new List<Line>();
    //Create a new line in the yDirection from the origin and add it to the list
    Point3d stPt = new Point3d(0, 0, 0);
    Vector3d unitYvect = new Vector3d(0, 1, 0);
    Line ln0 = new Line(stPt, unitYvect, length);
    lines.Add(ln0);
    //Create two temporary lists and set the first equal to the lines list
    List<Line> tempList = new List<Line>();
    List<Line> tempList2 = new List<Line>();
    tempList = lines;
 
    int i = 0;
    while(i < num){
      tempList2 = CreateBranch(tempList, branchScale, bAngRad);
      tempList = tempList2;
      lines.AddRange(tempList2);
      i++;
    }
    A = lines;
//<Custom additional code> 
 
 
  public List<Line> CreateBranch(List<Line> lines, double bScale, double bAng)
  {
    List<Line> newLines = new List<Line>();
    foreach(Line ln in lines){
      //Get the length of the current trunk and create a new scaled branch
      double newLength = ln.Length * bScale;
      //Get the endPt of the trunk and it's tangent vector
      Point3d endPt = ln.To;
      Vector3d unitTan = ln.UnitTangent;
      //Create two new lines and make the second line have a negative rotation angle
      Line ln1 = new Line(endPt, unitTan, newLength);
      ln1.Transform(Rhino.Geometry.Transform.Rotation(bAng, endPt));
      Line ln2 = new Line(endPt, unitTan, newLength);
      ln2.Transform(Rhino.Geometry.Transform.Rotation(bAng * -1, endPt));
      //Add these new branches to the list
      newLines.Add(ln1);
      newLines.Add(ln2);
    }
    return newLines;
  }
Tutorial Files: 

Rating

Please rate this tutorial below. Thanks.

5
Average: 5 (3 votes)

Comments

Hi, Zach. Nice job with the script. I have a question I want to ask you. Is there any way to delete the branches which were previously created and leave only the last ones? I want to test a theory about the golden ratio. I'm trying to go as far as possible but it takes 5 minutes to generate the 24-th set of branches. After all there are over 32 million lines. Thank you for your time. Best Regards, Alex

Want to Contribute?

Want to be an author? Drop us a line here we'd love to have you.

Already have a video you'd like to post? Send us a link and we'll get you going.

:)