Grasshopper

algorithmic modeling for Rhino

Python Basics. Lists in GH to Python? and advice on a recursive function?

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

(lji-1i + ξ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.

lis 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? ;)

Views: 17128

Replies to This Discussion

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".

Thanks Anders, have a look at this:

is there a way to get to the value from the list ? 

These lists are 2- dimensional arrays. You have to access each specific item and not the list itself.

Best,

M.

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. 

Just to add my 2 cents... Python is a wonderfully flexible language. For example the built-in 'len()' function and indexed retrieval can be applied to either a list or a string – in the latter case a string is treated as a list of characters. This is what you're seeing in your original post. Because the input parameter is only expecting a single value, not a list, the script is actually running three times; you're seeing the output from the third and final instance only. You can set the script input parameters to your expected input type (I.e. int, float, Curve) and data structure (value, list, tree).

Now, with regard to the equation you're implementing, why do you mention recursion? The equation you describe is iterative, with each subsequent step depending on the previous. Are you sure that 'lj' has a tree-like structure? As the comments below describe, a tree is similar to a multidimensional list, however the branches are stored in a single, flat, list-like structure with the 'paths' declaring their location within the data structure. This subtlety is worth exploring if you want to interact with them inside a script component.

Try doing the math by hand for the first few iterations. Make the lists/trees short and work it through before coding it up. This gives you an answer to aim for which will tell you if your script is working correctly and also helps to iron out any bugs in the problem description before you add the challenge of learning the syntactic subtleties that will allow you to implement it in your chosen language.

Good luck!

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

Attachments:

RSS

About

Translate

Search

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service