Thank you everyone, but Iâm still lost and essentially locked out of creating data trees. The reason I want to use datatrees is that I create stuff with my functions, that I want to later be able to call upon and handle separately.
A cumbersome way without trees would be to simply use lists, and append to the lists, and have a second list variable with items containing the length of each unit, and from that calculate the item number where unit X starts by adding the lengths of all previous units and subtracting 1. But since Grasshopper has trees, it feels silly for me not to use it.
I am going to present a simplified ghpython code example below, creating simple vertical lines. We will have one simple single line for easier understanding, as my issue is that I fail right out of the gate, but just know that in reality each unit will be an assembly of all sorts of things.
import Rhino.Geometry as rg
import rhinoscriptsyntax as rs
import math
import System
import ghpythonlib.treehelpers as th
import ghpythonlib.components as ghcomp
import re
COMMENT the one below I canât get to work because âModuleNotFoundError: No module named âGrasshopper.DataTreeââ
COMMENT import Grasshopper.DataTree as DataTree
COMMENT the one below I canât get to work because âModuleNotFoundError: No module named âGrasshopper.Kernel.Data.GH_Pathââ
COMMENT import Grasshopper.Kernel.Data.GH_Path as GH_Path
COMMENT This one I use as a replacement for the previous two, but canât get it to work:
import Grasshopper as gh
COMMENT Tree attempt 1 does not work because âNameError: name âDataTreeobjectâ is not definedâ and because I cannot import Grasshopper.DataTree and Grasshopper.Kernel.Data.GH_path:
COMMENT T = DataTreeobject
COMMENT Tree attempt 2 does not work because even though importing Grasshopper as gh works, it simply does nothing and results in âAttributeError: DataTreeobjectâ on the following line:
COMMENT T = gh.DataTreeobject
COMMENT Tree attempt 3 also doesnât work, but without producing an actual error.
T = {} #initialising the data tree.
A = [ ] # a simple list for comparison, but this is not my preferred solution for the reasons explained earlier.
for i in range(1,15):
unit_input = INPUT.Branch(i) # working through one branch of excel data at a time.
x = float(unit_input[3])
y = float(unit_input[4])
z1 = float(unit_input[5])
z2 = float(unit_input[7])
start_point = rg.Point3d(x , y, z1)
end_point = rg.Point3d(x , y, z2)
a = rg.Line(start_point, end_point) # a simple line that we will use as our example
A.append(a) # the list approach that works but is lacking for the reasons explained earlier.
T[i] = a
print(T[i]) # printing T[i] provides start and end coordinates.
print(T)
printing T provides {1: <Rhino.Geometry.Line object at 0x000001F8877618C0>, 2: <Rhino.Geometry.Line object at 0x000001F887761980>, 3: <Rhino.Geometry.Line object at 0x000001F887761BC0>, 4: <Rhino.Geometry.Line object at 0x000001F88776DA80>, 5: <Rhino.Geometry.Line object at 0x000001F8889753C0>, 6: <Rhino.Geometry.Line object at 0x000001F88776C3C0>, 7: <Rhino.Geometry.Line object at 0x000001F88776C180>, 8: <Rhino.Geometry.Line object at 0x000001F8885BB240>, 9: <Rhino.Geometry.Line object at 0x000001F8885BBE40>, 10: <Rhino.Geometry.Line object at 0x000001F8885BBD00>, 11: <Rhino.Geometry.Line object at 0x000001F8885BB980>, 12: <Rhino.Geometry.Line object at 0x000001F8877619C0>, 13: <Rhino.Geometry.Line object at 0x000001F8877611C0>, 14: <Rhino.Geometry.Line object at 0x000001F887761B40>}
However, T fails to be a meaningful result usable by grasshopper or Rhino:
I hope the above clarifies my question. I quite simply need the correct syntax needed to initialise and add to trees. All of my attempts, such as âT = DataTreeobjectâ simply fails. I have not been able to find a solution on my own, and as far as I can see neither the Youtube playlist (which adresses trees in general) nor the Youtube video actually look at ghpython. I have also failed to do it in grasshopper without python except through a convoluted process shown in the image in the introductory picture, but since the required Tekla component appears to confuse, and since Iâd prefer a coded solution that would allow me to loop through a variable number of loops, letâs focus on the code approach.
EDIT: I was required to replace # at the beginning of code lines with COMMENT in the code I posted above because # simply results in the text being reformatted into bold and big letters. If you throw it into a python module of your own, you should replace it back with #