Hello,
I’ve found quite a few topics on datatrees aka lists of lists in Python and stumbled across the tree helper functions which are great!
However, I’m stuck on understanding how to loop within a tree in a basic sense…
Here’s the example files I’m working in and the python script I have thus far:
Currently my component outputs (B) as a data tree how I want it, my next step is to loop through (B) and add additional functionality to the script at those locations based on certain criteria.
If I return (B) as a list instead of a data tree, the data isn’t nested how I need it. (See A) but I think this is okay because in python when I print this list it appears to contain nested lists as expected (like the data tree)
How do I iterate through the list in the list though? I thought a nested for loop inside another for loop but that (perhaps user error) didn’t seem to return the results I expected with the if and elif statements (see definition unit_check(B))
Is the key to success working only with data trees or am i just missing the fundamentals of iterating and appending nested lists?
Perhaps I’m overthinking all of this but I feel like this is my biggest hurdle to moving forward in GH Python.
Thank you all for any help and pointers!
Graph Space:
Code Thus Far:
Functions Remaining(psuedo code)(sharing to help explain my intent):
#Get_Frac_Values
If “/” in List
Get Item before and after “/” and divide them by one another to get the fraction decimal value as a single number
#Sum_Secondary_Values
(index 1 and 2 in each data tree branch of (B) are secondary values (think 1 3/4" inches) so I will sum the 1 + .75 (decimal equivalent)
#Convert_To_Document_Units
(since index 0 (Primary Value) and index 1+2 (Secondary Value) can be different units (think 3’ 7cm) I want to keep index 0 seperate until I convert it to the document units, convert the secondary values to document units, and then sum the result of both converted values to finally return a single value)
example: branch {0;3} 0. 35’,1. 3, 2. 3/4cm] (35’ 3 3/4cm)(stupid value i know…)
in this example i will see that 35 contains " ’ " meaning it is unit system index 9 (feet) and index 1 and 2 contain “cm” meaning it is unit system index 3 (centimeters) therefore I will get the UnitScale of the systems against my model units and multiply the results leaving me with all 3 items in this branch converted to feet, then i can sum the result
Naughty Strings To Parse (Some naughtier than others):
1515/8
15-1 5/8
1'-0"
15 1 5/8
15 .75
15 3/4
15 3
15
15" 6.75
15 6.75
15m 6.75mm
15m 6.75
15m6.75cm
15 6.75cm
15 3/4 cm
15 -3/4 cm
15ft6.75"
15ft 6.75"
15 ft 6.75"
12' 3 3/4
12' 3 3/4"
12ft 3 3/4"
12 feet 3 3/4cm
12 feet 3 3/4centimeters
Code Thus Far:
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino.RhinoDoc as rd
import re
import ghpythonlib.treehelpers as th
#Parse String & Return Number Values Converted To Document Units
#Get Document Units
sc.doc = rd.ActiveDoc
model_units = rs.UnitSystem()
model_units_name = rs.UnitSystemName(True,False)
ghenv.Component.Message = str(model_units_name)
#Split Input String With Spaces or Hyphens
def split_string(V):
results = []
for item in V:
if "-" in item:
values = re.split(r"[- ]", item)
results.append(values)
elif " " in item:
values = re.split(r"\s+", item.strip())
results.append(values)
else:
results.append([item])
return results
#Result A As Data Tree
A = th.list_to_tree(split_string(V))
#Result B As Python List Of Lists
B = (split_string(V))
def unit_check(B):
#Create New Blank Unit List As Ul
Ul = []
#Search Keys For Each Unit
ft_list = ["'","ft","feet"]
in_list = ['"',"in","inches"]
mm_list = ["mm","millimeters"]
cm_list = ["cm","centimeters"]
m_list = ["m","meters"]
#If Value Contains Keys, Return Unit System, Else Return Default Unit System
for values in [B]:
for value in values:
# print value
for i in value:
# print i
if any(map(i.__contains__, ft_list)):
Ul.append(9)
elif any(map(i.__contains__, in_list)):
Ul.append(8)
elif any(map(i.__contains__, mm_list)):
Ul.append(2)
elif any(map(i.__contains__, cm_list)):
Ul.append(3)
elif any(map(i.__contains__, m_list)):
Ul.append(4)
elif value.index(i) == 0:
Ul.append(model_units)
elif value.index(i) > 0:
Ul.append(int(model_units)-1)
return Ul
Vu = th.list_to_tree(unit_check(B))