algorithmic modeling for Rhino
Hi,
this is my first post. I've reached the point of grasshopper knowledge where I need to build a recursive function and decided to go with Python as my language.
I understand the basic principals of recursive functions, but I get stuck on the basics of python in grasshopper.
First, how do I manage a GH list input as a list in Python?
I would believe x[0] would retrive the first item in the list, not the first position/letter of the last item in the GH list..
Studied atlv.org, code.algorithmicdesign.net, designalyze.com without success.
(Copy&paste-GH-functions, HoopSnake nor Loop won't solve my recursive problem)
Last... My end goal is in the form below:
α0x = ξ0
(lj-ξi-1)αi + ξi-1= ξi
Where α is a GH list of floats (Galapagos intervalls of 0.00-1.00),
the size α of will govern my end condition.
x is one value, integer.
lj is a GH tree with 4 levels {0,0,0,0} but only one value in the perimeter branch.
To the more experianced out there, do you foresee obstacles with the above? Any inital tip? or a complete solution? ;)
Tags:
Found:
http://www.grasshopper3d.com/forum/topics/incremental-addition-of-v...
and need to change the input X to get list access, this is done by right-clicking the input on the component.
Moving on to adapting my recursive function for Data tree input, I found: http://www.grasshopper3d.com/forum/topics/data-tree-access-and-disp...
In my case lj is a component input containing a Data tree of 4 levels:
import Grasshopper.DataTree as ghdt
# Main
newTree = ghdt[object]()
branchN = lj.BranchCount
#convert input data tree to python list of lists
listOfLists = []
for i in range(branchN):
listOfLists.append(lj.Branch(i))
print lj.Branch(i)
I receive:
List[object]([6.0])
Now i would like to extract the value 6.0 to do math with it, but recieve the error msg unsupported operand type ... 'DataTree[object]' and 'int'
Ideas?
any help is appreciated
Thanks!
-Peter
Found:
http://www.grasshopper3d.com/forum/topics/parse-data-tree-branches-...
Where they tackle this using Python list() function. However, i recieve Runtime error, ArgumentTypeException: List[object] is not callable
#convert input data tree to python list of lists
listOfLists = []
for i in range(branchN):
listOfLists.append(lj.Branch(i))
someList = listOfLists[i] #List[object]
pythonList = list(someList) #Error msg, Line not callable
Any Ideas?
Are you trying to "convert" a DataTree to a nested Python list? If so, I find this to be the simplest solution:
pyList = [list(i) for i in nameOfDataTree.Branches]
Remember to set the parameter input to "Tree Access".
Thank you Marios, this was essential input
I think wiring your floating point numbers through a panel turns them into a string. Python is pretty relaxed about type, but won't let you multiply text by 10!
Well... it will, but you won't like the result:
>>> a = '15.0'
>>> print(a*10)
15.015.015.015.015.015.015.015.015.015.0
>>>
Thanks Josef,
it turns them into a string but Python seems manage that anyway. Multiplying the 2-dimensional array by ten changes the length of the array ten times, not the value. Had to access each item as above.
Thank you Will.
You made me rethink the problem. I could manage some "tree"/path operations pre and post script with match tree component. At start alpha is as you say not a tree but later in my algorithm it's ordered in such a way I have to consider the tree structure.
Pre simplifcation
Post.
I think this solved all my issues, implementing it now.
Thanks for all contributions! End result:
pylist = [list(i) for i in alpha.Branches]
outputList = []
for j in range(pylist.Count):
for k in range(len(pylist[j])):
if k == 0:
p = pylist[j][k]*X
outputList.append(p)
if k != 0:
p = (lj[j]-outputList[j-1])*pylist[j][k]+outputList[j-1]
outputList.append(p)
a = outputList
print a
and .GH file
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by