I’m new to python scripting but I’m starting to realize it might be the way to go to quickly process data without the need of cumbersome net of grasshopper components. Even though I already programmed a little bit in python in my student years (a while ago…), I’m a bit rusty. But, above all, I’m having trouble accessing the correct libraries when there is need to “read” through a grasshopper component.
First off, could you share some tutorials/docs for grasshopper-applied python?
Secondly, would you be kind enough to share a simple example script? What I need is this:
“Read” the tree as input
Check, for each branch, if on i=1 reads “W” of “HP”
Perform If / For cycle:
If it reads “W”, then the output should be the computation of 285x12 (i=2) + 200x15 (i+4)
If it reads “HP”, then the output should be the computation of 75x8 (i=2)
Make sure your x input is set to list access, and use this script:
if x[1] == "W":
n1, n2 = [int(n) for n in x[2].split('x')]
n3, n4 = [int(n) for n in x[4].split('x')]
a = n1*n2 + n3*n4
elif x[1] == "HP":
n1, n2 = [int(n) for n in x[2].split('x')]
a = n1*n2
Thanks @Measure, the “List Access” option was really the key. Is it too difficult to output a as a tree as well?
if x[1] == "W":
n1, n2 = [int(n) for n in x[2].split('x')]
n3, n4 = [int(n) for n in x[4].split('x')]
a[0] = n1*n2 + n3*n4
a[1] = n1+n2 + n3+n4
elif x[1] == "HP":
n1, n2 = [int(n) for n in x[2].split('x')]
a[0] = n1*n2
a[1] = n1+n2
if you assign a[0] and a[1] then a[0] and a[1] must already exist
just for the sake of explanation, this should work, but it’s not what you want to use:
a = [None, None] #a[0] and a[1] are declared
if x[1] == "W":
n1, n2 = [int(n) for n in x[2].split('x')]
n3, n4 = [int(n) for n in x[4].split('x')]
a[0] = n1*n2 + n3*n4 #overwrites a[0]
a[1] = n1+n2 + n3+n4 #overwrites a[1]
elif x[1] == "HP":
n1, n2 = [int(n) for n in x[2].split('x')]
a[0] = n1*n2 #overwrites a[0]
a[1] = n1+n2 #overwrites a[1]
this is what I would do, where List a is populated while being created:
if x[1] == "W":
n1, n2 = [int(n) for n in x[2].split('x')]
n3, n4 = [int(n) for n in x[4].split('x')]
a = [n1*n2 + n3*n4, n1+n2 + n3+n4]
elif x[1] == "HP":
n1, n2 = [int(n) for n in x[2].split('x')]
a = [n1*n2, n1+n2]